-
Notifications
You must be signed in to change notification settings - Fork 1
/
decaf.l
149 lines (137 loc) · 4.71 KB
/
decaf.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "decaf.h"
#define TOKEN(t) return t
#define SAVE {yylval = malloc(sizeof(struct semantics)); yylval->text = malloc(strlen(yytext)+1); \
strcpy(yylval->text, yytext);}
#define YY_USER_ACTION { if (yylineno != lineno) \
{ col = 1;\
lineno = yylineno; \
}}
int lineno=1, col=1;
%}
%option yylineno
STRINGCONST \"[^(\"|\n)]*\"
COMMENT (\/\/.*)|(\/\*(([^\*]*(\*[^\/])?)*)\*\/)
KEYWORD void|int|double|bool|string|class|interface|null|this|extends|implements|for|while|if|else|return|break|New|NewArray|Print|ReadInteger|ReadLine
KVOID void
KINT int
KDOUBLE double
KBOOL bool
KSTRING string
KCLASS class
KINTERFACE interface
KNULL null
KTHIS this
KEXTENDS extends
KIMPLEMENTS implements
KFOR for
KWHILE while
KIF if
KELSE else
KRETURN return
KBREAK break
KNEW New
KNEWARRAY NewArray
KPRINT Print
KREADINTEGER ReadInteger
KREADLINE ReadLine
BOOLCONST true|false
OPERATOR \+|\-|\*|\/|\%|\<|\<\=|\>|\>\=|\=|\=\=|\!\=|\&\&|\|\||\!|\(|\)
OPLUS <AMOP>\+
OMINUS <AMOP>\-
OMUL \*
ODIV \/
OIDIV \%
OLT \<
OLE \<\=
OGT \>
OGE \>\=
OEQU \=\=
ONE \!\=
OASSIGN \=
OAND \&\&
OOR \|\|
ONOT \!
OLSB \[
ORSB \]
OLB \(
ORB \)
PUNCTUATION [\.\;\,\[\]\{\}]
PPOINT \.
PSEP \;
PCOMMA \,
PLBB \{
PRBB \}
LETTER [A-Za-z]
DIGITI [0-9]
IDENTIFIER {LETTER}[\_0-9a-zA-Z]*
INTCONSTHEX ([\+\-]?0[xX][0-9a-fA-F]+)
INTCONSTDEC ([\+\-]?[0-9]+)
ERROR_ID [0-9]+[A-Za-z][0-9A-Za-z]
DOUBLECONST [\+\-]?{DIGITI}+\.{DIGITI}*([eE]{INTCONSTDEC})?
%s AMOP
%%
[\ \t\n] {col += yyleng;}
{STRINGCONST} { BEGIN(AMOP); col += yyleng; SAVE;TOKEN(TSTRINGCONST);}
{COMMENT} { col += yyleng;}
{KVOID} { col += yyleng; TOKEN(TKVOID);}
{KINT} { col += yyleng; TOKEN(TKINT);}
{KDOUBLE} { col += yyleng; TOKEN(TKDOUBLE);}
{KBOOL} { col += yyleng; TOKEN(TKBOOL);}
{KSTRING} { col += yyleng; TOKEN(TKSTRING);}
{KCLASS} { col += yyleng; TOKEN(TKCLASS);}
{KINTERFACE} { col += yyleng; TOKEN(TKINTERFACE);}
{KNULL} { col += yyleng; TOKEN(TKNULL);}
{KTHIS} { col += yyleng; TOKEN(TKTHIS);}
{KEXTENDS} { col += yyleng; TOKEN(TKEXTENDS);}
{KIMPLEMENTS} { col += yyleng; TOKEN(TKIMPLEMENTS);}
{KFOR} { col += yyleng; TOKEN(TKFOR);}
{KWHILE} { col += yyleng; TOKEN(TKWHILE);}
{KIF} { col += yyleng; TOKEN(TKIF);}
{KELSE} { col += yyleng; TOKEN(TKELSE);}
{KRETURN} { col += yyleng; TOKEN(TKRETURN);}
{KBREAK} { col += yyleng; TOKEN(TKBREAK);}
{KNEW} { col += yyleng; TOKEN(TKNEW);}
{KNEWARRAY} { col += yyleng; TOKEN(TKNEWARRAY);}
{KPRINT} { col += yyleng; TOKEN(TKPRINT);}
{KREADINTEGER} { col += yyleng; TOKEN(TKREADINTEGER);}
{KREADLINE} { col += yyleng; TOKEN(TKREADLINE);}
{BOOLCONST} { col += yyleng; SAVE; TOKEN(TBOOLCONST);}
<AMOP>\+ { BEGIN(0); col += yyleng; TOKEN(TOPLUS);}
<AMOP>\- { BEGIN(0); col += yyleng; TOKEN(TOMINUS);}
{OMUL} { BEGIN(0); col += yyleng; TOKEN(TOMUL);}
{ODIV} { BEGIN(0); col += yyleng; TOKEN(TODIV);}
{OIDIV} { BEGIN(0); col += yyleng; TOKEN(TOIDIV);}
{OLT} { BEGIN(0); col += yyleng; TOKEN(TOLT);}
{OLE} { BEGIN(0); col += yyleng; TOKEN(TOLE);}
{OGT} { BEGIN(0); col += yyleng; TOKEN(TOGT);}
{OGE} { BEGIN(0); col += yyleng; TOKEN(TOGE);}
{OEQU} { BEGIN(0); col += yyleng; TOKEN(TOEQU);}
{ONE} { BEGIN(0); col += yyleng; TOKEN(TONE);}
{OASSIGN} { BEGIN(0); col += yyleng; TOKEN(TOASSIGN);}
{OAND} { BEGIN(0); col += yyleng; TOKEN(TOAND);}
{OOR} { BEGIN(0); col += yyleng; TOKEN(TOOR);}
{ONOT} { BEGIN(0); col += yyleng; TOKEN(TONOT);}
{OLSB} { BEGIN(0); col += yyleng; TOKEN(TOLSB);}
{ORSB} { BEGIN(0); col += yyleng; TOKEN(TORSB);}
{OLB} { BEGIN(0); col += yyleng; TOKEN(TOLB);}
{ORB} { BEGIN(AMOP); col += yyleng; TOKEN(TORB);}
{PPOINT} { BEGIN(0); col += yyleng; TOKEN(TPPOINT);}
{PSEP} { BEGIN(0); col += yyleng; TOKEN(TPSEP);}
{PCOMMA} { BEGIN(0); col += yyleng; TOKEN(TPCOMMA);}
{PLBB} { BEGIN(0); col += yyleng; TOKEN(TPLBB);}
{PRBB} { BEGIN(0); col += yyleng; TOKEN(TPRBB);}
{IDENTIFIER} { BEGIN(AMOP); col += yyleng; SAVE; TOKEN(TIDENT);}
<INITIAL>([\+\-]?0[xX][0-9a-fA-F]+) { BEGIN(AMOP); col += yyleng; SAVE; TOKEN(TINTCONST);}
<INITIAL>([\+\-]?[0-9]+) { BEGIN(AMOP); col += yyleng; SAVE; TOKEN(TINTCONST);}
<INITIAL>[\+\-]?{DIGITI}+\.{DIGITI}*([eE]{INTCONSTDEC})? { BEGIN(AMOP); col += yyleng; SAVE; TOKEN(TDOUBLECONST);}
.|{ERROR_ID} {}
%%
int yywrap()
{
return 1;
}