-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcse.lex
More file actions
113 lines (93 loc) · 3.2 KB
/
Acse.lex
File metadata and controls
113 lines (93 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
/***************************************************************************
Scanner
***************************************************************************/
%option noyywrap
%{
/*
* Andrea Di Biagio
* Politecnico di Milano, 2007
*
* Axe.lex
* Formal Languages & Compilers Machine, 2007/2008
*
*/
#include <string.h>
#include "axe_struct.h"
#include "collections.h"
#include "Acse.tab.h"
#include "axe_constants.h"
/* Variables declared in the lexer for error tracking */
extern int line_num;
extern int num_error;
/* extern declaration of function yyerror */
extern int yyerror(const char* errmsg);
%}
/*=========================================================================
TOKEN DEFINITIONS
=========================================================================*/
DIGIT [0-9]
ID [a-zA-Z_][a-zA-Z0-9_]*
/*=========================================================================
TOKENS
=========================================================================*/
%option noyywrap
%x comment
%%
"\r\n" { ++line_num; }
"\n" { ++line_num; }
[ \t\f\v]+ { /* Ignore whitespace. */ }
"//"[^\n]* { ++line_num; /* ignore comment lines */ }
"/*" BEGIN(comment);
<comment>[^*\n]*
<comment>[^*\n]*\n { ++line_num; }
<comment>"*"+[^*/\n]*
<comment>"*"+[^*/\n]*\n { ++line_num; }
<comment>"*"+"/" BEGIN(INITIAL);
"{" { return LBRACE; }
"}" { return RBRACE; }
"[" { return LSQUARE; }
"]" { return RSQUARE; }
"(" { return LPAR; }
")" { return RPAR; }
";" { return SEMI; }
":" { return COLON; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MUL_OP; }
"/" { return DIV_OP; }
"%" { return MOD_OP; }
"&" { return AND_OP; }
"|" { return OR_OP; }
"!" { return NOT_OP; }
"=" { return ASSIGN; }
"<" { return LT; }
">" { return GT; }
"<<" { return SHL_OP; }
">>" { return SHR_OP; }
"==" { return EQ; }
"!=" { return NOTEQ; }
"<=" { return LTEQ; }
">=" { return GTEQ; }
"&&" { return ANDAND; }
"||" { return OROR; }
"," { return COMMA; }
"do" { return DO; }
"else" { return ELSE; }
"for" { return FOR; }
"if" { return IF; }
"int" { yylval.intval = INTEGER_TYPE; return TYPE; }
"while" { return WHILE; }
"return" { return RETURN; }
"read" { return READ; }
"write" { return WRITE; }
"define" { return DEFINE; }
"foreach" { return FOREACH; }
"in" { return IN; }
"every" { return EVERY; }
{ID} { yylval.svalue=strdup(yytext); return IDENTIFIER; }
{DIGIT}+ { yylval.intval = atoi( yytext );
return(NUMBER); }
. { yyerror("Error: unexpected token");
num_error++;
return (-1); /* invalid token */
}