-
Notifications
You must be signed in to change notification settings - Fork 1
/
assign3.l
112 lines (100 loc) · 3.41 KB
/
assign3.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
D [0-9]
NZ [1-9]
L [a-zA-Z_]
A [a-zA-Z_0-9]
WS [ \t\v\n\f]
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "helplexer.cpp"
#include "y.tab.h"
int oldstate;
%}
%x COMMENT
%option yylineno
%%
"break" { return(BREAK); }
"case" { return(CASE); }
"char" { yylval.Node = makeNodeInTree("CHAR","char");return(CHAR);return(CHAR); }
"continue" { return(CONTINUE); }
"do" { return(DO); }
"else" { return(ELSE); }
"float" { yylval.Node = makeNodeInTree("FLOAT","float");return(FLOAT);return(FLOAT); }
"for" { return(FOR); }
"if" { return(IF); }
"int" { yylval.Node = makeNodeInTree("INT","int");return(INT); }
"return" { return(RETURN); }
"sizeof" { return(SIZEOF); }
"struct" { yylval.Node = makeNodeInTree("STRUCT","struct");return(STRUCT); }
"typedef" { return(TYPEDEF); }
"void" { yylval.Node = makeNodeInTree("VOID","void");return(VOID);return(VOID); }
"while" { return(WHILE); }
[a-zA-Z_]+[a-zA-Z0-9]* {yylval.Node = makeNodeInTree("IDENTIFIER",yytext);return IDENTIFIER;}
{NZ}+{D}* {yylval.Node = makeNodeInTree("INTNUM",yytext);return INTNUM;}
"0" {yylval.Node = makeNodeInTree("INTNUM","0");return INTNUM;}
{D}+"."{D}+ { return FLOATNUM;}
/* strings */
'.' {return CHARLITERAL;}
\"(\\.|\"\"|[^"\n])*\" { return STRING; }
'(\\.|[^'\n])*$ { return *yytext; }
\"(\\.|[^"\n])*$ { return *yytext ;}
(?i:COMMENT) { return COMMENT; }
">>=" { return RIGHT_ASSIGN; }
"<<=" { return LEFT_ASSIGN; }
"+=" { return ADD_ASSIGN; }
"-=" { return SUB_ASSIGN; }
"*=" { return MUL_ASSIGN; }
"/=" { return DIV_ASSIGN; }
"%=" { return MOD_ASSIGN; }
"&=" { return AND_ASSIGN; }
"^=" { return XOR_ASSIGN; }
"|=" { return OR_ASSIGN; }
">>" { return RIGHT_OP; }
"<<" { return LEFT_OP; }
"++" { return INC_OP; }
"--" { return DEC_OP; }
"->" { return PTR_OP; }
"&&" { return AND_OP; }
"||" { return OR_OP; }
"<=" { return LE_OP; }
">=" { return GE_OP; }
"==" { return EQ_OP; }
"!=" { return NE_OP; }
";" { return ';'; }
("{"|"<%") { return '{'; }
("}"|"%>") { return '}'; }
"," { return ','; }
":" { return ':'; }
"=" { yylval.Node = makeNodeInTree("ADD","=");return '='; }
"(" { yylval.Node = makeNodeInTree("ADD","(");return '('; }
")" { yylval.Node = makeNodeInTree("ADD",")");return ')'; }
("["|"<:") { return '['; }
("]"|":>") { return ']'; }
"." { return '.'; }
"&" { return '&'; }
"!" { return '!'; }
"~" { return '~'; }
"-" { yylval.Node = makeNodeInTree("ADD","-");return '-'; }
"+" { yylval.Node = makeNodeInTree("ADD","+");return '+'; }
"*" { yylval.Node = makeNodeInTree("ADD","*");return '*'; }
"/" { yylval.Node = makeNodeInTree("ADD","/");return '/'; }
"%" { return '%'; }
"<" { return '<'; }
">" { return '>'; }
"^" { return '^'; }
"|" { return '|'; }
"?" { return '?'; }
"//".* ;
"/*" { oldstate = YY_START; BEGIN COMMENT; }
<COMMENT>"*/" { BEGIN oldstate; }
<COMMENT>.|\n ;
<COMMENT><<EOF>> {printf("unclosed comment"); return -1; }
{WS}+ { /* whitespace separates tokens */ }
. { return *yytext; }
%%
int yywrap(void) /* called at end of input */
{
return 1; /* terminate now */
}