-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.lex
50 lines (44 loc) · 1.29 KB
/
scanner.lex
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
%{
#include "ast.H"
#include "parser.H"
#include "grammar.h"
#include <iostream>
#include <iomanip>
%}
DIGIT [0-9]
ID [a-zA-Z_][a-zA-Z0-9_]*
NUMBER {DIGIT}+
%%
{NUMBER} {
yylval->val = atoi(yytext);
return NUMBER;
}
== { return TEQ;}
\< { return '<';}
\> { return '>';}
\<= { return LEQ;}
\>= { return GEQ;}
!= { return NEQ;}
&& { return TAND;}
\|\| { return TOR;}
\[\] { return TDB;}
(\[|\]|\(|\)|\+|\-|\*|\/|;|\{|\}|=|\.|,|!) { return yytext[0];}
if { return TIF;}
else { return TELSE;}
class { return TCLASS;}
extends { return TEXTENDS;}
while { return TWHILE;}
return { return TRETURN;}
this { return THIS;}
true { return TRUE;}
false { return FALSE;}
int { return TINT;}
bool { return TBOOL;}
void { return TVOID;}
new { return TNEW;}
{ID} {
yylval->id = strdup(yytext); // must copy the string. Can't use the constant.
return TID;
}
[ \t\n]* /* ignore ws */;
\/\/.*\n {}