-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.l
141 lines (133 loc) · 4.68 KB
/
source.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
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
/****************************************************/
/*File: jason.l */
/*Lexical Analysis for Jason Compiler */
/*Yueyang (Jason) Pan */
/****************************************************/
%{
#include "global.h"
#include "source.tab.hpp"
#include "driver.h"
#include <algorithm>
#include <stdlib.h>
#include <cerrno>
#include <climits>
#include <cstdlib>
%}
%option debug noinput
%option noyywrap
/*
%{
yy::parser::symbol_type
make_OCT_NUM(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type
make_DEC_NUM(const std::string &s, const yy::parser::location_type& loc);
yy::parser::symbol_type
make_HEX_NUM(const std::string &s, const yy::parser::location_type& loc);
%}*/
dec_digit [0-9]
oct_digit [0-7]
hex_digit [0-9a-fA-F]
nonzero_digit [1-9]
oct_num 0{oct_digit}*
hex_num 0[xX]{hex_digit}+
dec_num {nonzero_digit}{dec_digit}*
identifier_nondigit [a-zA-Z_]
identifier_all [a-zA-Z0-9_]
identifier {identifier_nondigit}{identifier_all}*
whitespace [ \t\r]+
newline \n
%{
#define YY_USER_ACTION loc.columns (yyleng);
%}
%%
%{
yy::location& loc = drv.location;
loc.step ();
%}
"if" {return yy::parser::make_IF(loc);}
"then" {return yy::parser::make_THEN(loc);}
"else" {return yy::parser::make_ELSE(loc);}
"while" {return yy::parser::make_WHILE(loc);}
"break" {return yy::parser::make_BREAK(loc);}
"continue" {return yy::parser::make_CONTINUE(loc);}
"return" {return yy::parser::make_RETURN(loc);}
"const" {return yy::parser::make_CONST(loc);}
"int" {return yy::parser::make_INT(loc);}
"void" {return yy::parser::make_VOID(loc);}
"=" {return yy::parser::make_ASSIGN(loc);}
";" {return yy::parser::make_SEMI(loc);}
"," {return yy::parser::make_COMMA(loc);}
"{" {return yy::parser::make_LLPAREN(loc);}
"}" {return yy::parser::make_LRPAREN(loc);}
"[" {return yy::parser::make_MLPAREN(loc);}
"]" {return yy::parser::make_MRPAREN(loc);}
"(" {return yy::parser::make_SLPAREN(loc);}
")" {return yy::parser::make_SRPAREN(loc);}
"!" {return yy::parser::make_NOT(loc);}
"==" {return yy::parser::make_EQ(loc);}
"!=" {return yy::parser::make_NEQ(loc);}
"<" {return yy::parser::make_LT(loc);}
">" {return yy::parser::make_GT(loc);}
"<=" {return yy::parser::make_LTE(loc);}
">=" {return yy::parser::make_GTE(loc);}
"+" {return yy::parser::make_PLUS(loc);}
"-" {return yy::parser::make_MINUS(loc);}
"*" {return yy::parser::make_TIMES(loc);}
"/" {return yy::parser::make_OVER(loc);}
"%" {return yy::parser::make_MOD(loc);}
"&&" {return yy::parser::make_AND(loc);}
"||" {return yy::parser::make_OR(loc);}
{whitespace} { loc.step (); }
{newline} { loc.lines (yyleng); loc.step (); }
"//"[^\n]*\n { loc.lines (yyleng); loc.step (); }
"/*"([^*]|\*+[^/*])*"*/" { loc.lines (yyleng); loc.step (); }
{oct_num} {
errno = 0;
long n = strtol(yytext, NULL, 8);
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
throw yy::parser::syntax_error (loc, "integer is out of range: "
+ std::string(yytext));
return yy::parser::make_OCT_NUM (n, loc);
}
{dec_num} {
errno = 0;
long n = strtol(yytext, NULL, 10);
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
throw yy::parser::syntax_error (loc, "integer is out of range: "
+ std::string(yytext));
return yy::parser::make_DEC_NUM (n, loc);
}
{hex_num} {
errno = 0;
long n = strtol(yytext, NULL, 16);
if (! (INT_MIN <= n && n <= INT_MAX && errno != ERANGE))
throw yy::parser::syntax_error (loc, "integer is out of range: "
+ std::string(yytext));
return yy::parser::make_HEX_NUM (n, loc);
}
{identifier} {
return yy::parser::make_ID (yytext, loc);
}
. {
throw yy::parser::syntax_error
(loc, "invalid character: " + std::string(yytext));
}
<<EOF>> return yy::parser::make_END (loc);
%%
void
driver::scan_begin ()
{
yy_flex_debug = trace_scanning;
if (file.empty () || file == "-")
yyin = stdin;
else if (!(yyin = fopen (file.c_str (), "r")))
{
std::cerr << "cannot open " << file << ": " << strerror(errno) << '\n';
exit (EXIT_FAILURE);
}
}
void
driver::scan_end ()
{
fclose (yyin);
}