forked from tushar-jain26/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex_module_part_1.java
277 lines (215 loc) · 5.48 KB
/
lex_module_part_1.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
void Open_File();
void Demage_Lexeme();
int Search(char[256],int);
void analyze();
void Skip_Comment();
void Read_String();
void Is_Keyword_Or_Not();
void Is_Identifier_Or_Not();
void Is_Operator_Or_Not();
void Read_Number();
void Is_Special_Or_Not();
void Is_Comparison_Or_Not();
void Add_To_Lexical (char[256],int,char[256]);
void Print_ST();
void Print_TOKEN();
void Token_Attribute();
struct lexical
{
char data[256]; //Value of token.
int line[256]; //Line # which token appear in input
file.
int times; //# of times that token appear in input
file.
char type[256]; //Type of each token.
struct lexical *next;
};
typedef struct lexical Lex;
typedef Lex *lex;
/**********************************************************
File pointer for accessing the file.
***********************************************************/
FILE *fp;
FILE *st;
FILE *token;
char lexeme[256],ch;
int f,flag,line=1,i=1;
lex head=NULL,tail=NULL;
/***********************************************************
Array holding all keywords for checking.
***********************************************************/
char
*keywords[]={"procedure","is","begin","end","var","cin","cout","if",
"then","else","and","or","not","loop","exit","when",
"while","until"};
/************************************************************
Array holding all arithmetic operations for checking.
************************************************************/
char arithmetic_operator[]={'+','-','*','/'};
/************************************************************
Array holding all comparison operations for checking.
************************************************************/
char *comparison_operator[]={"<",">","=","<=","<>",">="};
/************************************************************
Array holding all special for checking.
************************************************************/
char special[]={'%','!','@','~','$'};
/************************************************************
**************
*MAIN PROGRAM*
**************
************************************************************/
void main()
{
Open_File();
analyze();
fclose(fp);
Print_ST();
Print_TOKEN();
}
/***********************************************************
This function open input sourse file.
************************************************************/
void Open_File()
{
fp=fopen("source.txt","r"); //provide path for source.txt here
if(fp==NULL)
{
printf("!!!Can't open input file - source.txt!!!");
getch();
exit(0);
}
}
/***********************************************************
Function to add item to structure of array to store data and
information of lexical items.
***********************************************************/
void Add_To_Lexical (char value[256],int line,char type[256])
{
lex new_lex;
if (!Search(value,line)) //When return 1 the token not found.
{
new_lex=malloc(sizeof(Lex));
if (new_lex!=NULL)
{
strcpy(new_lex->data,value);
new_lex->line[0]=line;
new_lex->times=1;
strcpy(new_lex->type,type);
new_lex->next=NULL;
if (head==NULL)
head=new_lex;
else
tail->next=new_lex;
tail=new_lex;
}
}
}
/***********************************************************
Function to search token.
***********************************************************/
int Search (char value[256],int line)
{
lex x=head;
int flag=0;
while (x->next!=NULL && !flag)
{
if (strcmp(x->data,value)==0)
{
x->line[x->times]=line;
x->times++;
flag=1;
}
x=x->next;
}
return flag;
}
/************************************************************
Function to print the ST.TXT .
*************************************************************/
void Print_ST()
{
lex x=head;
int j;
if ((st=fopen("ST.TXT","w"))==NULL)
printf("The file ST.TXT cat not open.
");
else
{
System.out.println(st," %s %s %s
","Line#","Lexeme","Type");
System.out.println(st," ---- ------ ----
");
while (x!=NULL)
{
if ((strcmp(x->type,"num")==0) ||
(strcmp(x->type,"keyword")==0) ||
(strcmp(x->type,"identifier")==0))
{
System.out.println(st," ");
for (j=0;j<x->times;j++)
{
System.out.println(st,"%d",x->line[j]);
if (j!=x->times-1) //This condition to prevent the comma
System.out.println(st,",",x->line[j]); //"," to not print after last line #.
}
System.out.println(st," %-6s %-6s
",x->data,x->type);
}
x=x->next;
}
fclose(st);
}
}
/***********************************************************
Function to print the TOKENS.TXT .
***********************************************************/
void Print_TOKEN()
{
int flag=0;
fp=fopen("source.txt","r");
if(fp==NULL)
{
printf("!!!Can't open input file - source.txt!!!");
getch();
exit(0);
}
else
{
if ((token=fopen("TOKENS.TXT","w"))==NULL)
printf("The file ST.TXT cat not open.
");
else
{
ch=fgetc(fp);
while (!(feof(fp)))
{
if (ch==' ' && !flag)
{
do
ch=fgetc(fp);
while (ch==' ');
fseek(fp,-2,1);
ch=fgetc(fp);
flag=1;
}
if (ch!='
' && ch!=' ')
System.out.println(token,"%c",ch);
if (ch=='
')
{
System.out.println(token,"
");
Token_Attribute();
i++;
flag=0;
}
ch=fgetc(fp);
}
}
}
fclose(fp);
fclose(token);
}
/*End of lexical module part-I*/