-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.l
executable file
·128 lines (124 loc) · 2.31 KB
/
token.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
%{
#include "const_record.h"
#include "string_data.h"
#include "type_record.h"
#include "var_record.h"
#include "stmt.h"
#include "common.h"
#include "expr.h"
#include "routine.h"
#include "y.tab.h"
extern int lineno = 0;
extern bool flag = false;
%}
const "const"
var "var"
lp "("
rp ")"
lb "["
rb "]"
dot "."
comma ":"
colon ","
mul "*"
div "/"
plus "+"
minus "-"
id [a-zA-Z][a-zA-Z0-9_]*
ge ">="
gt ">"
le "<="
lt "<"
mod "%"
writeln "writeln"
sys_proc read|write
function "function"
equal "="
assign ":="
integer [0-9]+
real [0-9]+"."[0-9]+
char "'"."'"
string "'".*"'"
semi ";"
program "program"
type "type"
sys_type integer|string|real|char
record "record"
end "end"
array "array"
begin "begin"
casee "case"
iff "if"
doo "do"
to "to"
downto "downto"
until "until"
for "for"
while "while"
elsee "else"
repeat "repeat"
of "of"
goto "goto"
then "then"
procedure "procedure"
%%
{const} {return CONST;}
{function} {return FUNCTION;}
{writeln} {return WRITELN;}
{program} {return PROGRAM;}
{sys_type} {yylval._str = strdup(yytext);return SYS_TYPE;}
{record} {return RECORD;}
{casee} {return CASEE;}
{mod} {return MOD;}
{iff} {return IFF;}
{doo} {return DOO;}
{elsee} {return ELSEE;}
{procedure} {return PROCEDURE;}
{of} {return OF;}
{repeat} {return REPEAT;}
{goto} {return GOTO;}
{then} {return THEN;}
{to} {return TO;}
{while} {return WHILE;}
{for} {return FOR;}
{until} {return UNTIL;}
{downto} {return DOWNTO;}
{end} {return END;}
{begin} {return BEGINN;}
{type} {return TYPE;}
{array} {return ARRAY;}
{var} {return VAR;}
{plus} {return PLUS;}
{minus} {return MINUS;}
{lp} {return LP;}
{rp} {return RP;}
{lb} {return LB;}
{rb} {return RB;}
{dot} {return DOT;}
{comma} {return COMMA;}
{colon} {return COLON;}
{mul} {return MUL;}
{div} {return DIV;}
{id} {yylval._str = strdup(yytext);return ID;}
{ge} {return GE;}
{gt} {return GT;}
{le} {return LE;}
{lt} {return LT;}
{equal} {return EQUAL;}
{assign} {return ASSIGN;}
{integer} {yylval._int = atoi(yytext);return INTEGER;}
{real} {yylval._double = atof(yytext);return REAL;}
{char} {yylval._char = yytext[1];return CHAR;}
{string} {
int l = strlen(yytext);
yylval._str = new char[l - 2];
strncpy(yylval._str, yytext + 1, l - 2);return STRING;}
{semi} {return SEMI;}
\n {lineno++;}
\t {}
" " {}
. {return yytext[0];}
%%
int yywrap(){
return 1;
}