-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.lex
109 lines (80 loc) · 2.18 KB
/
compiler.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
%{
#include "y.tab.h"
// int yylineno=1;
%}
%%
[\n] { yylineno = yylineno + 1;}
"/*" { comment(); }
"//"[^\n]* { /* consume //-comment */ }
[ \t] {}
"fname" { return FILENAME;}
"auto"|"register"|"static"|"extern"|"typedef" { return STORAGE_CLASS_SPECIFIER;}
"void"|"char"|"short"|"int"|"long"|"float"|"double"|"signed"|"unsigned" { return TYPE_SPECIFIER;}
"const"|"volatile" { return TYPE_QUALIFIER;}
"struct"|"union" { return STRUCT_OR_UNION;}
"enum" { return ENUM;}
"case" { return CASE;}
"default" { return DEFAULT;}
"if" { return IF;}
"else" { return ELSE;}
"switch" { return SWITCH;}
"while" { return WHILE;}
"do" { return DO;}
"for" { return FOR;}
"goto" { return GOTO;}
"continue" { return CONTINUE;}
"break" { return BREAK;}
"return" { return RETURN;}
"||" {return OR_OP;}
"&&" {return AND_OP;}
"..." { return ELLIPSIS; }
">>" { return RIGHT_OP; }
"<<" { return LEFT_OP; }
"++" { return INC_OP; }
"--" { return DEC_OP; }
"->" { return PTR_OP; }
"==" {return EQ_OP;}
"<=" { return LE_OP; }
">=" { return GE_OP; }
"!=" { return NE_OP; }
%Assignment
"*=" { return MUL_ASSIGN; }
"/=" { return DIV_ASSIGN; }
"%=" { return MOD_ASSIGN; }
"+=" { return ADD_ASSIGN; }
"-=" { return SUB_ASSIGN; }
"<<=" { return LEFT_ASSIGN; }
">>=" { return RIGHT_ASSIGN; }
"&=" { return AND_ASSIGN; }
"^=" { return XOR_ASSIGN; }
"|=" { return OR_ASSIGN; }
[,{}()[\]#=*+-/<>!|&%~^;:] { return yytext[0];}
"sizeof" { return SIZEOF;}
"define" { return DEFINE;}
"undef" { return UNDEF;}
"include" { return INCLUDE;}
"line" { return LINE;}
"error" { return ERROR;}
"pragma" { return PRAGMA;}
"ifdef" { return IFDEF;}
"ifndef" { return IFNDEF;}
"elif" { return ELIF;}
[_a-zA-Z][_a-zA-Z0-9]* { return IDENTIFIER;}
[+-]?[0-9]+ { return INTEGER_CONSTANT;}
[+-]?([0-9]*[.])?[0-9]+ { return FLOATING_CONSTANT;}
\"(\\.|[^\\"])*\" { return STRING;}
\'(\\.|[^\\'])\' { return CHARACTER_CONSTANT;}
%[^\n \t]+ { return TOKEN_SEQUENCE;}
%%
void comment(void)
{
char c, prev = 0;
while ((c = input()) != 0) /* (EOF maps to 0) */
{
if( c == '\n') { yylineno += 1;}
if (c == '/' && prev == '*')
return;
prev = c;
}
error("unterminated comment");
}