/
main.l
65 lines (53 loc) · 994 Bytes
/
main.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
%option noyywrap
%{
#include"common.h"
#include"main.tab.hh"
%}
INTEGER [0-9]+
ID [[:alpha:]_][[:alpha:][:digit:]_]*
EOL (\r\n|\n)
WHITE [\t ]
%%
"true" {
TreeNode *node = new TreeNode(NODE_BOOL);
node->bool_val = true;
yylval = node;
return TRUE;
}
"false" {
TreeNode *node = new TreeNode(NODE_BOOL);
node->bool_val = false;
yylval = node;
return FALSE;
}
"int" return INT;
"void" return VOID;
"if" return IF;
"while" return WHILE;
"else" return ELSE;
"printf" return PRINTF;
"scanf" return SCANF;
"=" return ASSIGN;
"+" return ADD;
"!" return NOT;
"==" return EQUAL;
";" return SEMICOLON;
"(" return LPAREN;
")" return RPAREN;
"{" return LBRACE;
"}" return RBRACE;
{INTEGER} {
TreeNode *node = new TreeNode(NODE_CONST);
node->int_val = atoi(yytext);
yylval = node;
return INTEGER;
}
{ID} {
TreeNode *node = new TreeNode(NODE_VAR);
node->var_name = string(yytext);
yylval = node;
return ID;
}
{EOL}
{WHITE}
%%