-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmm.l
94 lines (74 loc) · 4.05 KB
/
cmm.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
%{
#include <stdio.h>
#include <stdlib.h>
#include "ast.h"
#include "utils.h"
#include "cmm.tab.h"
int mylineno = 1;
int mycolno = 1;
%}
%option nounput
%option noinput
%option noyywrap
%x comment
%%
/* LITERALS */
\"[[:print:]]{-}[\n\"]*\" { mycolno += strlen(yytext); return STRINGCON; }
\'(\\n|\\0|[[:print:]]{-}[\\\"])\' { mycolno += strlen(yytext); return CHARCON; }
[0-9]+ { mycolno += strlen(yytext); return INTCON; }
/* RESERVED KEYWORDS */
"for" { mycolno += strlen(yytext); return FOR; }
"while" { mycolno += strlen(yytext); return WHILE; }
"if" { mycolno += strlen(yytext); return IF; }
"return" { mycolno += strlen(yytext); return RETURN; }
"else" { mycolno += strlen(yytext); return ELSE; }
"char" { mycolno += strlen(yytext); return CHAR; }
"void" { mycolno += strlen(yytext); return VOID; }
"int" { mycolno += strlen(yytext); return INT; }
"extern" { mycolno += strlen(yytext); return EXTERN; }
/* EQUALITY OPERATORS */
"==" { mycolno += strlen(yytext); return EQ; }
"!=" { mycolno += strlen(yytext); return NEQ; }
"!" { mycolno += strlen(yytext); return NOT; }
/* LOGICAL OPERATORS */
"\&\&" { mycolno += strlen(yytext); return AND; }
"||" { mycolno += strlen(yytext); return OR; }
/* RELATIONAL OPERATORS */
">=" { mycolno += strlen(yytext); return GTE; }
"<=" { mycolno += strlen(yytext); return LTE; }
">" { mycolno += strlen(yytext); return GT; }
"<" { mycolno += strlen(yytext); return LT; }
/* RELATIONAL OPERATORS */
"+" { mycolno += strlen(yytext); return ADD; }
"-" { mycolno += strlen(yytext); return MINUS; }
"*" { mycolno += strlen(yytext); return MUL; }
"/" { mycolno += strlen(yytext); return DIV; }
"=" { mycolno += strlen(yytext); return ASSIGN; }
/* PARENTHESIS TOKENS */
"(" { mycolno += strlen(yytext); return LEFT_PAREN; }
")" { mycolno += strlen(yytext); return RIGHT_PAREN; }
"[" { mycolno += strlen(yytext); return LEFT_SQUARE_BRACKET; }
"]" { mycolno += strlen(yytext); return RIGHT_SQUARE_BRACKET; }
"{" { mycolno += strlen(yytext); return LEFT_CURLY_BRACKET; }
"}" { mycolno += strlen(yytext); return RIGHT_CURLY_BRACKET; }
/* Delimiters */
";" { mycolno += strlen(yytext); return SEMICOLON; }
"," { mycolno += strlen(yytext); return COMMA; }
/* IDENTIFIERS */
[a-zA-Z][a-zA-Z0-9\_]* { mycolno += strlen(yytext); yylval.string = strdup(yytext); return ID; }
/* WHITESPACE */
[[:space:]]{-}[\n]+ { mycolno += strlen(yytext); }
/* DISCARD COMMENTS */
"/*" { mycolno += strlen(yytext); BEGIN(comment); }
<comment><<EOF>> {
fprintf(stderr, "Unexpected end of file while parsing comment on line %d.\n", mylineno);
exit(1);
}
<comment>[^*\n]* { mycolno += strlen(yytext); }
<comment>"*"+[^*/\n]* { mycolno += strlen(yytext); }
<comment>\n { mylineno += 1; mycolno = 1;}
<comment>"*"+"/" { mycolno += strlen(yytext); BEGIN(INITIAL); }
\/\/.+\n { mycolno = 0; mylineno += 1; }
/* Newlines */
\n { mylineno += 1; mycolno = 1; }
%%