-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlex.l
154 lines (147 loc) · 4.14 KB
/
lex.l
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
%{
#include<stdio.h>
#include<string.h>
#include"symbol.h"
#include"y.tab.h"
int scope_count = -1;
/*struct token {
char variable_name[10];
char type;
int value;
int line_no;
int scope_count;
char scope[10];
};*/
struct token *symbol_table[100];
int ind =-1;
int type = 0;
int line_no = 1;
int getPosition(char *);
void print_symbols();
%}
identifier [a-zA-Z][_a-zA-Z0-9]*
number [0-9]+|([0-9]*\.[0-9]+)
%%
\/\/(.*) {}
\/\*(.*\n)*.*\*\/ {}
"#include<stdio.h>" {return HEADER;}
"#include<iostream>" {return HEADER;}
"while" {printf("while: %s\n",yytext);;return WHILE;}
"if" {printf("if %s in line %d\n",yytext,line_no);return IF;}
"else" {printf("else %s in line %d\n",yytext,line_no);return ELSE;}
"for" {printf("for %s in line %d\n",yytext,line_no);return FOR;}
"main" { printf("main %s in line %d\n",yytext,line_no);strcpy(yylval.var,yytext);return MAIN;}
"int"|"char"|"float" { type=1; printf("Keyword %s in line %d\n",yytext,line_no);strcpy(yylval.var,yytext);return TYPE;}
{number}+{identifier} {printf("Error %s in line %d\n",yytext,line_no);}
{identifier} {
printf("Identifier %s in line %d\n",yytext,line_no);
if(type > 0){
type = 0;
//int pos = getPosition(yytext);
/*if(pos < 0)
{
symbol_table[++index] = (token*)malloc(sizeof(token));
strcpy(symbol_table[index].variable_name,yytext);
symbol_table[index].line_no = line_no;
symbol_table[index].scope_count = scope_count;
if(scope_count == 0)
{
strcpy(symbol_table[index],"GLOBAL");
}
else{
strcpy(symbol_table[index],"LOCAL");
}
}
else
{
}*/
int pos = getPosition(yytext);
if(pos >= 0){
if(symbol_table[pos]->scope_count != scope_count)
{
ind++;
symbol_table[ind] = (struct token*)malloc(sizeof(struct token));
strcpy(symbol_table[ind]->variable_name,yytext);
symbol_table[ind]->line_no = line_no;
symbol_table[ind]->scope_count = scope_count;
if(scope_count == 0)
{
strcpy(symbol_table[ind]->scope,"GLOBAL");
}
else
{
strcpy(symbol_table[ind]->scope,"LOCAL");
}
}
} else{
ind++;
symbol_table[ind] = (struct token*)malloc(sizeof(struct token));
strcpy(symbol_table[ind]->variable_name,yytext);
symbol_table[ind]->line_no = line_no;
symbol_table[ind]->scope_count = scope_count;
if(scope_count == 0)
{
strcpy(symbol_table[ind]->scope,"GLOBAL");
}
else
{
strcpy(symbol_table[ind]->scope,"LOCAL");
}
}
}
//print_symbols();
strcpy(yylval.var,yytext);
return VAR;
}
{number} {printf("Number %s in line %d\n",yytext,line_no);strcpy(yylval.var,yytext);return NUM;}
\<|\>|\>=|\<=|==|!= {printf("Relational Operator %s in line %d\n",yytext,line_no);return RELOP;}
(\+\+)|(--) {printf("Increment Operator %s in line %d\n",yytext,line_no);return *yytext;}
= {printf("Assignment operator %s in line %d\n",yytext,line_no);return '=';}
"{" { scope_count++; printf("Opening braces %s in line %d\n",yytext,line_no); return '{';}
"}" { scope_count--; printf("Closing braces %s in line %d\n",yytext,line_no);return '}';}
"(" {printf("Opening brackets %s in line %d\n",yytext,line_no); return '(';}
")" {printf("Closing brackets %s in line %d\n",yytext,line_no); return ')';}
[ \t] ;
\n {line_no++;}
; {type=0; printf("SEMI %s in line %d\n",yytext,line_no);return ';';}
. {printf("Error %s in line %d\n",yytext,line_no);return *yytext;}
%%
int getPosition(char* string)
{
for(int i=0;i<=ind;i++)
{
if(strcmp(symbol_table[i]->variable_name,string) == 0)
{
return i;
}
}
return -1;
}
void print_symbols()
{
printf("Symbol Table\n\n\n\n");
//printf("Value of ind: %d\n",ind);
for(int i=0;i<=ind;i++)
{
printf("Varible name: %s\n",symbol_table[i]->variable_name);
printf("Line no: %d\n",symbol_table[i]->line_no);
printf("Type: %s\n",symbol_table[i]->type);
printf("Value: %d\n",symbol_table[i]->value);
printf("\n\n");
}
}
/*
int main(int argc, char* argv[])
{
if(argc > 1)
{
FILE *fp = fopen(argv[1], "r");
if(fp)
{
yyin = fp;
}
}
yylex();
print_symbols();
return 1;
}*/