This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c6.l
78 lines (66 loc) · 1.66 KB
/
c6.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
%{
#include <stdlib.h>
#include "calc3.h"
#include "y.tab.h"
void yyerror(char *);
%}
%%
0 { yylval.iValue = atol(yytext);
return INTEGER;
}
[1-9][0-9]* { yylval.iValue = atol(yytext);
return INTEGER;
}
/* character */
'.' { yylval.iValue = (long) yytext[1];
return INTEGER;
}
/* operator */
">=" { return GE; }
"<=" { return LE; }
"==" { return EQ; }
"!=" { return NE; }
"&&" { return AND; }
"||" { return OR; }
/* keyword */
"for" { return FOR; }
"while" { return WHILE; }
"if" { return IF; }
"else" { return ELSE; }
"print" { return PRINT; }
"puti" { return PRINT; }
"puti_" { return PUTI_; }
"puts" { return PUTS; }
"puts_" { return PUTS_; }
"putc" { return PUTC; }
"putc_" { return PUTC_; }
"read" { return READ; }
"geti" { return READ; }
"getc" { return GETC; }
"gets" { return GETS; }
"array" { return ARRAY; }
"func" { return FUNC; }
"return" { return RETURN; }
[-()<>=+*/;{}.%\[\],@] { return *yytext; }
/* string */
\".+\" { yytext[yyleng-1]=0;
yylval.iValue = (long) yytext+1;
return STRING;
}
/* identifier */
[[:alpha:]][[:alnum:]]* { yylval.iValue = (long) yytext;
return VARIABLE;
}
\/\/.*\n { /* skip comments */ }
[ \t\n\r]+ { /* ignore whitespace */ } /* Pay attention to `\r` on Windows. */
. { yyerror("unknown character");
// printf("ASCII of unknown character:");
// for (int i = 0; i < yyleng; i++) {
// printf(" %d", yytext[i]);
// }
// printf("\n");
}
%%
int yywrap(void) {
return 1;
}