-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.l
executable file
·80 lines (55 loc) · 1.66 KB
/
parser.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.tab.h"
#include "symTable.h"
char dtype[20];
%}
%option yylineno
%%
("/*"(.|\n)*"*/") {;}
("//"(.)*) {;}
[0-9]+ {printf("%s\n",yytext);return NUMBER;}
"int"|"char"|"void"|"string" {printf("%s\n",yytext);strcpy(dtype,yytext);return DTYPE;}
"cout" {printf("%s\n",yytext);return COUT;}
"cin" {printf("%s\n",yytext);return CIN;}
\"(.)*\" {printf("%s\n",yytext);return LITERAL;}
"main" {printf("%s\n",yytext);return MAIN;}
"class" {printf("%s\n",yytext);strcpy(dtype,yytext);return CLASS;}
"struct" {printf("%s\n",yytext);strcpy(dtype,yytext);return STRUCT;}
"private"|"public"|"protected" {printf("%s\n",yytext); return SPECIFIER;}
"while" {printf("%s\n",yytext); return WHILE;}
"for" {printf("%s\n",yytext); return FOR;}
"if" {printf("%s\n",yytext); return IF;}
"else if" {printf("%s\n",yytext); return ELIF;}
"else" {printf("%s\n",yytext); return ELSE;}
"switch" {printf("%s\n",yytext); return SWITCH;}
"case" {printf("%s\n",yytext); return CASE;}
"break" {printf("%s\n",yytext);return BREAK;}
"default" {printf("%s\n",yytext); return DEFAULT;}
"return" {printf("%s\n",yytext);return RETURN;}
[a-zA-Z][a-zA-Z0-9_]* {printf("%s \n",yytext);
int index = searchTable(yytext);
if(index==-1) {
TOKEN* tk = newToken();
index = hashFunction();
tk->index = index;
strcpy(tk->name,yytext);
strcpy(tk->datatype,dtype);
if(strcmp(dtype,"class")==0 || strcmp(dtype,"struct")==0) {
tk->scope=0;
} else {
tk->scope=1;
}
SYMTABLE[index]=tk;
}
return ID;
}
"," {printf("%s\n",yytext); return COMMA;}
" "|\t|\n {;}
. {return yytext[0];}
%%
int yywrap() {
return 1;
}