-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanner.l
90 lines (73 loc) · 2.95 KB
/
scanner.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
%option noyywrap
%option header-file="lex.yy.h"
%{
#include "parser.tab.h"
#include <iostream>
#define YY_DECL int yylex()
%}
%x COMMENT
WHITESPACE ([[:space:]]+)
COMMENT_LINE ("//"[^\n]*)
COMMENT_START ("/*")
COMMENT_END ("*/")
ID ([A-Za-z][A-Za-z0-9_]*)
DEC_INTEGER ([0-9]+)
HEX_INTEGER (0[xX][0-9A-Fa-f]+)
INTEGER ({DEC_INTEGER}|{HEX_INTEGER})
STRING (\"[^"\n]*\")
EXPONENT ([Ee][+-]?{DEC_INTEGER})
DOUBLE ([0-9]+\.[0-9]*{EXPONENT}?)
SYMBOLS ([-+/*%=.,;!<>()[\]{}])
%%
{WHITESPACE} {;}
{COMMENT_LINE} {;}
{COMMENT_START} { BEGIN(COMMENT); }
<COMMENT>{COMMENT_END} { BEGIN(0); }
<COMMENT>. {;}
"void" { return T_Void;}
"int" { return T_Int; }
"double" { return T_Double; }
"bool" { return T_Bool; }
"string" { return T_String; }
"class" { return T_Class; }
"interface" { return T_Interface; }
"null" { return T_Null; }
"this" { return T_This; }
"extends" { return T_Extends; }
"implements" { return T_Implements; }
"for" { return T_For; }
"while" { return T_While; }
"if" { return T_If; }
"else" { return T_Else; }
"return" { return T_Return; }
"break" { return T_Break; }
"continue" {return T_Continue;}
"new" { return T_New; }
"NewArray" { return T_NewArray; }
"Print" { return T_Print; }
"ReadInteger" { return T_ReadInteger; }
"ReadLine" { return T_ReadLine; }
"dtoi" {return T_Dtoi;}
"itod" {return T_Itod;}
"btoi" {return T_Btoi;}
"itob" {return T_Itob;}
"private" {return T_Private;}
"protected" {return T_Protected;}
"public" {return T_Public;}
"<=" { return T_LessEqual; }
">=" { return T_GreaterEqual; }
"==" { return T_Equal; }
"!=" { return T_NotEqual; }
"&&" { return T_And; }
"||" { return T_Or; }
"[]" { return T_Dims; }
{SYMBOLS} { return yytext[0];} //no TokenType for these
"true" { yylval.booleanLiteral = (yytext[0]=='t'); return T_BOOLEANLITERAL; }
"false" { yylval.booleanLiteral = (yytext[0]=='t'); return T_BOOLEANLITERAL; }
{DEC_INTEGER} { yylval.integerLiteral = strtol(yytext, NULL, 10); return T_INTLITERAL; }
{HEX_INTEGER} { yylval.integerLiteral = strtol(yytext, NULL, 16); return T_INTLITERAL; }
{DOUBLE} { yylval.doubleLiteral = strtod(yytext, NULL); return T_DOUBLELITERAL; }
{STRING} { yylval.stringLiteral = strdup(yytext); return T_STRINGLITERAL; }
{ID} { yylval.id = strdup(yytext); return T_ID; }
. { return UNDEFINED_TOKEN; }
%%