forked from Joisha02/Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LexicalAnalyzerWithFiles.c
160 lines (138 loc) · 4.39 KB
/
LexicalAnalyzerWithFiles.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//make a file lex.txt containing a small c prog and write the name "lex.txt"
/* Algorithm:
Initialize the file pointers, predefined keywords, operators in separate
arrays.
Read every line of the input file by reading each character and
appending it to another string “line” till it encounters ‘\n’.
Now check whether line is a comment or not.
Run a loop through the line string and extract words from it using
special characters as delimiters.
For each special character encountered, check whether the character
is an operator or not and then send the extracted word into the
function that will decide what category of Lexeme it is.
If it is one of the keywords, then the output is 1, if it is a valid identifier
then output is 2, otherwise output is 0.
Write onto the respective files according to the classified data.
Print the number of lines as output.*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
char keywords[32][10] = {"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register",
"return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while"};
char operators[4] = {'+', '-', '/', '*'};
FILE *inputFile, *keywordsFile, *operatorsFile, *identifiersFile, *commentsFile;
int is_alpha(int c)
{
return (c >= 65 && c <= 90) || (c >= 97 && c <= 122);
}
int isLexeme(char word[20])
{
int length = strlen(word), i;
if (length < 1)
return 0;
for (i = 0; i < 32; i++)
if (!strcmp(keywords[i], word))
return 1;
if (word[0] != '_' && !is_alpha(word[0]))
return 0;
return 2;
}
int isOperator(char op)
{
int i;
for (i = 0; i < 4; i++)
if (op == operators[i])
return 1;
return 0;
}
int isSpecial(int c)
{
if (c == '_')
return 0;
return (c >= 32 && c <= 47) || (c >= 58 && c <= 64) || (c >= 91 && c <= 96) || (c >= 123 && c <= 127);
}
char *trim(char *s)
{
int i;
while (isspace(*s))
s++;
for (i = strlen(s) - 1; (isspace(s[i])); i--)
;
s[i + 1] = '\0';
return s;
}
int isComment(char str[100])
{
strcpy(str, trim(str));
int length = strlen(str);
if (length < 2)
return 0;
else if ((str[0] == str[1]) && (str[0] == '/'))
return 1;
else if ((str[0] == str[length - 1]) && (str[1] == str[length - 2]))
{
if (str[0] == '/' && str[1] == '*')
return 1;
}
else
return 0;
}
int main()
{
printf("\nEnter the filename of a C program: ");
char filename[20];
gets(filename);
inputFile = fopen(filename, "r");
keywordsFile = fopen("Keywords.txt", "w");
operatorsFile = fopen("Operators.txt", "w");
commentsFile = fopen("Comments.txt", "w");
identifiersFile = fopen("Identifiers.txt", "w");
if (inputFile == NULL)
{
printf("Error while opening the file.\n");
return 0;
}
int lines = 0, length, i, lex = 0;
char c, word[20] = "", line[1000] = "", lastChar;
while ((c = getc(inputFile)) != EOF)
{
if (c == '\n')
{
lines++;
if (isComment(line))
{
fputs(line, commentsFile);
putc('\n', commentsFile);
strncpy(line, "", sizeof(line));
continue;
}
length = strlen(line);
for (i = 0; i < length; i++)
if (isSpecial(line[i]))
{
if (isOperator(line[i]))
putc(line[i], operatorsFile);
int lex = isLexeme(word);
if (lex == 1)
{
fputs(word, keywordsFile);
putc('\n', keywordsFile);
}
else if (lex == 2)
{
fputs(word, identifiersFile);
putc('\n', identifiersFile);
}
strncpy(word, "", sizeof(word));
}
else
strncat(word, line + i, 1);
strncpy(line, "", sizeof(line));
}
else
strncat(line, &c, 1);
lastChar = c;
}
printf("Number of lines: %d\n", lines);
}