-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinyC.l
executable file
·87 lines (66 loc) · 1.36 KB
/
tinyC.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
%{
#include <stdlib.h>
#include "y.tab.h"
#include "table.h"
int numberLines = 1;
%}
DIGIT [0-9]|[1-9][0-9]*
EXPONENTIAL [0-9]*"."[0-9]+[e|E][-|+]?[0-9]+
HEX 0(x|X)[0-9A-Fa-f]+
OCTAL 0(c|C)[0-7]+
ID [A-Za-z_][A-Za-z0-9_]*
RELATIONAL <|<=|>|>=|!=|==
ASSIGNMENT :=
%x COMMENT
%%
"/*" BEGIN(COMMENT);
<COMMENT>{
"*/" BEGIN(INITIAL);
[^*\n]+
"*"
\n numberLines++;
}
"//".*
{DIGIT} {return INT_NUM; }
{DIGIT}"."{DIGIT}+ {
return FLOAT_NUM;
}
{HEX} { return INT_NUM; }
{OCTAL} { return INT_NUM; }
"if" { return IF; }
"then" { return THEN; }
"else" { return ELSE; }
"int" { return INT; }
"float" { return FLOAT; }
"while" { return WHILE; }
"do" { return DO; }
"read" { return READ; }
"write" { return WRITE; }
"{" { return LBRACE; }
"}" { return RBRACE; }
"(" { return LPAREN; }
")" { return RPAREN; }
";" { return SEMICOLON; }
","
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return TIMES; }
"/" { return DIV; }
{ASSIGNMENT} {
return ASSIGNMENT;
}
{RELATIONAL} {
return RELATIONAL;
}
{EXPONENTIAL} { return FLOAT_NUM; }
{ID} {
yylval.symp = symlook(yytext);
return ID;
}
[ \t]+
\n numberLines++;
. {
printf("Unknown token %s found in line %d \n\n", yytext, numberLines);
exit(-1);
}
%%