-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.l
164 lines (142 loc) · 2.72 KB
/
scanner.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
155
156
157
158
159
160
161
162
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifndef LEXONLY
#include "y.tab.h"
#else
typedef union { int ival; char* str; } yystype;
#define YYSTYPE yystype
yystype yylval;
#define KWINT 1
#define KWCHAR 2
#define KWWHILE 3 // Added KWWHILE
#define KWIF 4 // Added KWIF
#define KWELSE 5 // Added KWELSE
#define ID 6
#define STRING 7
#define LPAREN 8
#define RPAREN 9
#define LBRACE 10
#define RBRACE 11
#define SEMICOLON 12
#define NUMBER 13
#define COMMA 14
#define ADDOP 15 // Changed to ADDOP, used to be PLUS
#define EQUALS 16
#define RELOP 17
#define LBRACKET 18 // Added LBRACKET
#define RBRACKET 19 // Added RBRACKET
#endif
%}
%%
[ \t\n\r]* { /* Skip Whitespace */}
int {
yylval.ival = yytext[0];
return(KWINT);
}
char\* {
yylval.ival = yytext[0];
return(KWCHAR);
}
while {
yylval.ival = yytext[0];
return(KWWHILE);
}
if {
yylval.ival = yytext[0];
return(KWIF);
}
else {
yylval.ival = yytext[0];
return(KWELSE);
}
[a-zA-Z]+ {
// printf("ID: (%s)\n", yytext);
yylval.str = strdup(yytext);
return(ID);
}
\"[^\"]*\" {
// printf("STRING: (%s)\n", yytext);
yylval.str = strdup(yytext);
return(STRING);
}
\( {
// printf("LPAREN: (%s)\n", yytext);
yylval.ival = yytext[0];
return(LPAREN);
}
\) {
// printf("RPAREN: (%s)\n", yytext);
yylval.ival = yytext[0];
return(RPAREN);
}
\{ {
// printf("LBRACE: (%s)\n", yytext);
yylval.ival = yytext[0];
return(LBRACE);
}
\} {
// printf("RBRACE: (%s)\n", yytext);
yylval.ival = yytext[0];
return(RBRACE);
}
\; {
// printf("SEMICOLON: (%s)\n", yytext);
yylval.ival = yytext[0];
return(SEMICOLON);
}
[0-9]+ {
// printf("NUMBER: (%s)\n", yytext);
yylval.ival = strtol( yytext, 0,10);
return(NUMBER);
}
\, {
// printf("COMMA: (%s)\n", yytext);
yylval.ival = yytext[0];
return(COMMA);
}
\+ {
// printf("PLUS: (%s)\n", yytext);
yylval.ival = yytext[0];
return(ADDOP);
}
\- {
// printf("MINUS: (%s)\n", yytext);
yylval.ival = yytext[0];
return(ADDOP);
}
\= {
yylval.ival = yytext[0];
return(EQUALS);
}
[=!]= {
yylval.ival = yytext[0];
return(RELOP);
}
[<>] {
yylval.ival = yytext[0];
return(RELOP);
}
\[ {
printf("LBRACKET: (%s)\n", yytext);
yylval.ival = yytext[0];
return(LBRACKET);
}
\] {
printf("RBRACKET: (%s)\n", yytext);
yylval.ival = yytext[0];
return(RBRACKET);
}
%%
#ifdef LEXONLY
int main(int argc, char **argv){
do{
yylex();
} while(1);
return 0;
}
int yywrap(){
exit(0);
}
#endif