-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexicalAnalyser.c
159 lines (150 loc) · 4.51 KB
/
lexicalAnalyser.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
/* A program to read a source code and identify the tokens in it and the print the information, the program ignores single and multiline comments. */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
return 1;
}
return 0;
}
int isSpecialSymbol(char ch)
{
if (ch == '{' || ch == '}' || ch == '(' || ch == ')' || ch == ';' || ch == ':' || ch == '"' || ch == '<' || ch == '>' || ch == '#' || ch == '=')
{
return 1;
}
return 0;
}
int main()
{
FILE *input;
input = fopen("input.txt", "r");
int no_of_tokens = 1;
int line_number = 1;
int i = 0;
int is_division_operator = 1;
int isString = 0;
int flag = 0; // to check wether keyword or identifiers
char word[30];
char ch; // to read each charcter
char keywords[30][30] = {"int", "main", "stdio", "include", "printf", "while", "for", "return", "#"}; // # to mark the end of keyword array
printf("LineNo:\t\tTokenNo:\t\tToken\t\tLexeme\n");
while ((ch = fgetc(input)) != EOF)
{
i = 0;
if (isOperator(ch))
{
// to check for comments
if (ch == '/')
{
char next = fgetc(input);
// for single line comments
if (next == '/')
{
is_division_operator = 0;
while ((ch = fgetc(input)) != '\n' && ch != EOF)
{
// skip the characters in the comments
}
}
// for multine comments
else if(next == '*')
{
is_division_operator = 0;
while(1)
{
ch = fgetc(input);
if(ch == '*' && fgetc(input) == '/')
{
break;
}
if(ch == '\n')
{
line_number++;
}
}
}
else
{
ungetc(next,input);
printf("%d\t\t%d\t\tOperator\t\t%c \n", line_number, no_of_tokens, ch);
no_of_tokens++;
is_division_operator = 1;
}
}
else{
printf("%d\t\t%d\t\tOperator\t\t%c \n", line_number, no_of_tokens, ch);
no_of_tokens++;
is_division_operator = 1;
}
}
else if (isSpecialSymbol(ch))
{
// to notify a string has a started
if (ch == '"')
{
// if 1 it changes to 0 and viceversa
isString = (1 + isString) % 2;
}
printf("%d\t\t%d\t\tSpecial Symbol\t\t%c \n", line_number, no_of_tokens, ch);
no_of_tokens++;
}
else if (isdigit(ch))
{
printf("%d\t\t%d\t\tdigit\t\t\t%c \n", line_number, no_of_tokens, ch);
no_of_tokens++;
}
else if (isalpha(ch))
{
word[i] = ch;
while (isalnum(ch) && ch != ' ' && !isSpecialSymbol(ch))
{
word[i++] = ch;
ch = fgetc(input);
}
if (isSpecialSymbol(ch))
{
ungetc(ch, input);
}
word[i] = '\0';
char temp[30];
for (int i = 0; keywords[i] != '#'; i++)
{
if (strcmp(keywords[i], word) == 0)
{
flag = 1;
strcpy(temp, keywords[i]);
break;
}
}
if (flag)
{
printf("%d\t\t%d\t\tKeyword\t\t\t%s\n", line_number, no_of_tokens, temp);
no_of_tokens++;
flag = 0;
}
else
{
if (isString)
{
printf("%d\t\t%d\t\tString\t\t\t%s\n", line_number, no_of_tokens, word);
no_of_tokens++;
}
else
{
printf("%d\t\t%d\t\tIdentifier\t\t%s\n", line_number, no_of_tokens, word);
no_of_tokens++;
}
}
}
if (ch == '\n')
{
line_number++;
}
}
fclose(input);
return 0;
}