-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenout.l
More file actions
142 lines (115 loc) · 3.2 KB
/
tokenout.l
File metadata and controls
142 lines (115 loc) · 3.2 KB
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
%option yylineno
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "functions.h"
#include "clike.tab.h"
int lineNumber = 1;
int status = 0;
int yycolumn = 1;
#define YY_USER_ACTION yylloc.first_line = yylloc.last_line = yylineno; \
yylloc.first_column = yycolumn; yylloc.last_column = yycolumn+yyleng-1; \
yycolumn += yyleng;
%}
SPACE [ \t\v\f]
NEWLINE [\n\r]
KW1 auto|_Bool|break|case|char|_Complex|const|continue|default
KW2 restrict|do|double|else|enum|extern|float|for|goto|if
KW3 _Imaginary|inline|int|long|register|return|short|signed
KW4 sizeof|static|struct|switch|typedef|union|unsigned|void
KW5 volatile|while
KEYWORD {KW1}|{KW2}|{KW3}|{KW4}|{KW5}
SNGLCOMMENT \/\/.*[\r\n]
COMMENTSTART \/\*
COMMENTEND \*\/
ID [a-zA-Z_][0-9a-zA-Z_]*
STRING "[0-9a-zA-Z]
INTSUF (l|L|ll|LL)[uU]?|[uU](l|L|ll|LL)?
DEC [1-9][0-9]*{INTSUF}?
OCT 0[0-7]*{INTSUF}?
HEX 0[Xx][A-Fa-f0-9]+{INTSUF}?
DIGSEQ [0-9]+
DOTDIGS ({DIGSEQ}\.)|({DIGSEQ}\.{DIGSEQ})|(\.{DIGSEQ})
EXP [eE][\+-]?{DIGSEQ}
FLOATSUF [fFlL]
FLOAT ({DIGSEQ}{EXP}{FLOATSUF}?)|({DOTDIGS}{EXP}?{FLOATSUF}?)
CHARESCAPE [ntbrfv\\\'\"a\?]
OCTESCAPE [0-7]{1,3}
HEXESCAPE x[A-Fa-f0-9]+
ESCAPECODE \\({CHARESCAPE}|{OCTESCAPE}|{HEXESCAPE})
CHARSEQ ({ESCAPECODE}|[^\'])+
CHAR L?\'{CHARSEQ}\'
STRSEQ ({ESCAPECODE}|[^\"])+
STR L?\"{STRSEQ}?\"
SIMPOPS [%\^&~\|\.\?]
COMPOPS1 \+=|\-=|\*=|\/=|%=|<<=|>>=|&=|\^=|\|=
COMPOPS2 ->|\+\+|\-\-|<<|>>
SEPOPS \.\.\.
ALTOPS <%|%>|<:|:>|%:|%:%:
OPERATOR {SIMPOPS}|{COMPOPS1}|{COMPOPS2}|{SEPOPS}|{ALTOPS}
QUOTES [\'\"]
VALIDSINGLE [^\'\" \n\r\t\v\f]
%x MULTICOMMENT
%%
<INITIAL>{COMMENTSTART} { BEGIN(MULTICOMMENT); }
<MULTICOMMENT><<EOF>> {
fprintf( stderr, "%d, Unterminated Comment\n", lineNumber);
return(1);
}
<MULTICOMMENT>[\n\r] { yycolumn = 1; BEGIN(MULTICOMMENT); }
<MULTICOMMENT>. { BEGIN(MULTICOMMENT) ; }
<MULTICOMMENT>{COMMENTEND} { BEGIN(INITIAL); }
"void" { return VOID; }
"char" { return CHAR; }
"int" { return INT; }
"float" { return FLOAT; }
"double" { return FLOAT; }
"while" { return WHILE; }
"for" { return FOR; }
"return" { return RETURN; }
"if" { return IF; }
"else" { return ELSE; }
"=" { return ASSIGN; }
"-" { return MINUS; }
"!" { return NOT; }
"&&" { return AND; }
"||" { return OR; }
"==" { return EQ; }
"!=" { return NEQ; }
"+" { return PLUS; }
"*" { return STAR; }
"/" { return SLASH; }
"<=" { return LEQ; }
"<" { return LESS; }
">=" { return GEQ; }
">" { return GREATER; }
\( { return LPAREN; }
\) { return RPAREN; }
\[ { return LBRACK; }
\] { return RBRACK; }
\{ { return LCURLY; }
\} { return RCURLY; }
"," { return COMMA; }
";" { return SEMICOLON; }
{SNGLCOMMENT} { yycolumn = 1; }
{ID} { yylval.name = strdup(yytext); return ID; }
{DEC} { yylval.iVal = atoi(yytext); return INTCON; }
{OCT} { yylval.iVal = atoi(yytext); return INTCON; }
{HEX} { yylval.iVal = atoi(yytext); return INTCON; }
{FLOAT} { yylval.fVal = atof(yytext); return FLOATCON; }
{CHAR} { yyerror("Unknown token");
status = (1);
}
{STR} { yyerror("Unknown token");
status = (1);
}
{QUOTES} { yyerror("Unknown token");
status = (1);
}
{OPERATOR} { yyerror("Operator not in grammar");
status = (1);
}
{NEWLINE} { yycolumn = 1; }
{SPACE}
%%