-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtiger.lex
150 lines (132 loc) · 3.73 KB
/
tiger.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
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
150
%{
/* Lab2 Attention: You are only allowed to add code in this file and start at Line 26.*/
#include <string.h>
#include "util.h"
#include "symbol.h"
#include "errormsg.h"
#include "absyn.h"
#include "y.tab.h"
int charPos=1;
int yywrap(void)
{
charPos=1;
return 1;
}
void adjust(void)
{
EM_tokPos=charPos;
charPos+=yyleng;
}
/*
* Please don't modify the lines above.
* You can add C declarations of your own below.
*/
/* @function: getstr
* @input: a string literal
* @output: the string value for the input which has all the escape sequences
* translated into their meaning.
*/
char *getstr(const char *str)
{
//optional: implement this function if you need it
return NULL;
}
static int input(void);
char *stringToken() {
yyleng = 1;
int maxlen = 128;
char *s = (char *)malloc(maxlen);
int pos = 0;
char c;
while((c = (char)input()) != EOF) {
++yyleng;
if(c == '\"') {
break;
}
if(c == '\n') {
break;
}
if(c == '\\') {
c = (char)input();
++yyleng;
switch(c) { // still have other conditions
case 'n':
c = '\n';
break;
case 't':
c = '\t';
break;
default:
break;
}
}
if(pos >= maxlen) {
s = (char *)realloc(s, maxlen * 2);
maxlen = maxlen * 2;
}
s[pos] = (char)c;
++pos;
}
s[pos] = '\0';
return s;
}
int commentStartCount = 0;
%}
/* You can add lex definitions here. */
%Start COMMENT
%%
/*
* Below is an example, which you can wipe out
* and write reguler expressions and actions of your own.
*/
"\n" {adjust(); EM_newline(); continue;}
<INITIAL>(" "|"\t")+ {adjust();}
<INITIAL>while {adjust(); return WHILE;}
<INITIAL>for {adjust(); return FOR;}
<INITIAL>to {adjust(); return TO;}
<INITIAL>break {adjust(); return BREAK;}
<INITIAL>let {adjust(); return LET;}
<INITIAL>in {adjust(); return IN;}
<INITIAL>end {adjust(); return END;}
<INITIAL>function {adjust(); return FUNCTION;}
<INITIAL>var {adjust(); return VAR;}
<INITIAL>type {adjust(); return TYPE;}
<INITIAL>array {adjust(); return ARRAY;}
<INITIAL>if {adjust(); return IF;}
<INITIAL>then {adjust(); return THEN;}
<INITIAL>else {adjust(); return ELSE;}
<INITIAL>do {adjust(); return DO;}
<INITIAL>of {adjust(); return OF;}
<INITIAL>nil {adjust(); return NIL;}
<INITIAL>\x22 {yylval.sval=stringToken(); adjust(); return STRING;}
<INITIAL>"/*" {adjust(); ++commentStartCount; BEGIN COMMENT;}
<INITIAL>":=" {adjust(); return ASSIGN;}
<INITIAL>"," {adjust(); return COMMA;}
<INITIAL>":" {adjust(); return COLON;}
<INITIAL>";" {adjust(); return SEMICOLON;}
<INITIAL>"(" {adjust(); return LPAREN;}
<INITIAL>")" {adjust(); return RPAREN;}
<INITIAL>"[" {adjust(); return LBRACK;}
<INITIAL>"]" {adjust(); return RBRACK;}
<INITIAL>"{" {adjust(); return LBRACE;}
<INITIAL>"}" {adjust(); return RBRACE;}
<INITIAL>"." {adjust(); return DOT;}
<INITIAL>"+" {adjust(); return PLUS;}
<INITIAL>"-" {adjust(); return MINUS;}
<INITIAL>"*" {adjust(); return TIMES;}
<INITIAL>"/" {adjust(); return DIVIDE;}
<INITIAL>"=" {adjust(); return EQ;}
<INITIAL>"!="|"<>" {adjust(); return NEQ;}
<INITIAL>"<=" {adjust(); return LE;}
<INITIAL>"<" {adjust(); return LT;}
<INITIAL>">=" {adjust(); return GE;}
<INITIAL>">" {adjust(); return GT;}
<INITIAL>"&" {adjust(); return AND;}
<INITIAL>"|" {adjust(); return OR;}
<INITIAL>[A-Za-z][A-Za-z0-9_]* {adjust(); yylval.sval=String(yytext); return ID;}
<INITIAL>[0-9]+ {adjust(); yylval.ival=atoi(yytext); return INT;}
<INITIAL>. {adjust(); EM_error(charPos, "illegal character");}
<COMMENT>"/*" {adjust(); ++commentStartCount;}
<COMMENT>"*/" {adjust(); --commentStartCount; if(commentStartCount==0) {BEGIN INITIAL;}}
<COMMENT>(" "|"\t")+ {adjust();}
<COMMENT>. {adjust();}