-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocklexer.l
205 lines (169 loc) · 3.93 KB
/
blocklexer.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
%x COMMENT
int nl = 1;
int comment_nesting = 0;
#define MAX_DEPTH 72
#define T_eof 0
ID [a-zA-Z_][a-zA-Z0-9_]*
HEX [0-9|A-F|a-f]
ESCAPE \\(n|t|r|0|\\|\'|\"|x{HEX}{HEX})
%{
//'
#include <stdio.h>
#include "ast.h"
#include "blockparser.hpp"
#include <string.h>
char* escapeStr(char *s);
int fixChar (char *str, int *shift);
int fixHex (char *str);
int charToInt (char);
%}
%%
\n {nl++;}
\r ;
[ \t] ;
^[ ]*\n {/* Ignore blank lines. */}
[0-9][0-9]* {
//printf("Integer %s\n",yytext);
yylval.const_val = atoi(yytext);
return(CONST);
}
if {return(IF);}
else {return(ELSE);}
elif {return(ELIF);}
loop {return(LOOP);}
int {return(INT);}
byte {return(BYTE);}
as {return(AS);}
skip {return(SKIP);}
decl {return(DECL);}
begin {return(BEG);}
end {return(END);}
def {return(DEF);}
is {return(IS);}
var {return(VAR);}
not {return(NOT);}
and {return(AND);}
or {return(OR);}
true {return(TRUE);}
false {return(FALSE);}
break {return(BREAK);}
continue {return(CONT);}
exit {return(EXIT);}
return {return(RETURN);}
ref {return(REF);}
{ID} {
//printf("Identifier: %s\n",yytext);
yylval.idstring = strdup(yytext);
return(IDENTIFIER);
}
"(" {return('(');}
")" {return(')');}
"[" {return('[');}
"]" {return(']');}
"." {return('.');}
"," {return(',');}
"+" {return('+');}
"-" {return('-');}
"*" {return('*');}
"/" {return('/');}
":=" {return(ASSIGNMENT);}
"=" {return(EQ);}
"!" {return('!');}
[%] {return('%');}
"&" {return('&');}
"|" {return('|');}
"<" {return(SMALLER);}
"<=" {return(SMALLEREQ);}
">=" {return(LARGEREQ);}
"<>" {return(DIFF);}
">" {return(LARGER);}
":" {return(':');}
"\"" {printf("Unclosed String Error\n");exit(-1);}
\"([^\n\"\'\\]|{ESCAPE})*\" {
yylval.idstring = strdup(escapeStr(yytext));
return(STRINGLITERAL);
}
\'([^\"\'\\]|{ESCAPE})\' {
//"
yylval.const_val = fixChar(yytext+1,NULL);
return CHAR_CONST;
}
"#".* {;}
"(*" { ++comment_nesting; BEGIN(COMMENT); }
<COMMENT>{
"(*" { ++comment_nesting; }
"*"+")" { --comment_nesting; if(!comment_nesting) BEGIN(INITIAL);}
"*"+ ;
[^(*\n]+ ;
[(] ;
"\n" ; {nl++;}
<<EOF>> ; {if (comment_nesting>0) printf("error: unclosed comment in line %d\n",nl); return -1;}
}
. {
printf("Unexpected Character %d\n",yytext[0]);
printf("Line no: %d \n",nl);
exit(-1);
}
%%
int fixChar(char * str, int *shift)
{
int dummy;
if(shift==NULL) shift=&dummy;
*shift=1;
if(str[0]!='\\') return str[0];
*shift=2;
switch(str[1]){
case 'n': return '\n';
case 't': return '\t';
case 'r': return '\r';
case '0': return '\0';
case '\\': return '\\';
case '\'': return '\'';
case '\"': return '\'';
case 'x': {*shift=4; return fixHex(str+2); }
default : {printf("lexer: escape char, unmatched case"); exit(1);}
}
}
int fixHex(char * str)
{
int d1,d2,hex;
d1=charToInt(str[0]);
d2=charToInt(str[1]);
hex = d1*16 + d2;
return hex;
}
int charToInt(char c)
{
if(c>='0' && c<='9')
return c-'0';
else if(c>='a' && c<='f')
return c-'a'+10;
else if(c>='A' && c<='F')
return c-'A'+10;
else
printf("lexer: %s not a proper char expression",yytext);
return -1;
}
char* escapeStr(char *s) {
int len = strlen(s);
char c;
char* result;
result = (char *)malloc(len*sizeof(char));
int j = 0;
for (int i = 0; i < len; ++i){
c = s[i];
if(c=='\\'){
if(s[i+1] == 'x') {
c = fixChar(s+i,NULL);
i+=3;
} else {
c = fixChar(s+i,NULL);
i++;
}
}
result[j] = c;
j++;
}
result[len]='\0';
return result;
}