-
Notifications
You must be signed in to change notification settings - Fork 0
/
cx.l
executable file
·113 lines (98 loc) · 2.27 KB
/
cx.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
%{
#include "stdio.h"
#include "cx.tab.h"
int line = 1;
int yywrap();
void yyerror(char *);
char msg[100];
void comment();
%}
whitespace ([ \t\n])+
ID [A-Za-z][A-Za-z0-9]*
NUM ([1-9][0-9]*)|0
CHA '[A-za-z]'
%%
[ \t] {;}
[\n] {line++;}
"/*" {comment();}
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return TIMES;}
"/" {return SLASH;}
"<" {return LES;}
"<=" {return LEQ;}
">" {return GTR;}
">=" {return GEQ;}
"==" {return EQL;}
"!=" {return NEQ;}
"=" {return BECOMES;}
"||" {return OR;}
"&&" {return AND;}
"!" {return NOT;}
";" {return SEMICOLON;}
"(" {return LPAREN;}
")" {return RPAREN;}
"[" {return LBRACKET;}
"]" {return RBRACKET;}
"{" {return LBRACE;}
"}" {return RBRACE;}
"," {return COMMA;}
"++" {return SELFADD;}
"--" {return SELFMIUNS;}
"true" {return TRUE;}
"false" {return FALSE;}
"if" {return IF;}
"else" {return ELSE;}
"while" {return WHILE;}
"write" {return WRITE;}
"read" {return READ;}
"int" {return INT;}
"bool" {return BOOL;}
"const" {return CONST;}
"repeat" {return REPEAT;}
"until" {return UNTIL;}
"^" {return XOR;}
"%" {return MOD;}
"odd" {return ODD;}
"call" {return CALL;}
"do" {return DO;}
"func" {return FUNC;}
"exit" {return EXIT;}
"for" {return FOR;}
"char" {return CHAR;}
{ID} {
yylval.ident = (char *)malloc(strlen(yytext) + 1);
strcpy(yylval.ident, yytext);
return IDENT;
}
{NUM} {
yylval.number = atoi(yytext);
return NUMBER;
}
{CHA} {
yylval.ident = (char *)malloc(strlen(yytext) + 1);
strcpy(yylval.ident, yytext);
return CHARACTER;
}
. {
sprintf(msg, "yyerror: unexpected character %s", yytext);
yyerror(msg);
}
%%
void redirectInput(FILE *input) {
yyin = input;
}
int yywrap() {
return 1;
}
void comment() {
char c, c1;
loop:
while ((c = input()) != '*' && c != 0) {
if (c == '\n') {line++;}
}
if ((c1 = input()) != '/' && c != 0) {
unput(c1);
goto loop;
}
}