-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsablecc.scc
283 lines (248 loc) · 7.93 KB
/
sablecc.scc
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
Helpers
firstnum = ['1'..'9'];
number = ['0'..'9'];
a = ['a' + 'A']; // Create all the letters and accept them
b = ['b' + 'B']; // ignoring their case
c = ['c' + 'C'];
d = ['d' + 'D'];
e = ['e' + 'E'];
f = ['f' + 'F'];
g = ['g' + 'G'];
h = ['h' + 'H'];
i = ['i' + 'I'];
j = ['j' + 'J'];
k = ['k' + 'K'];
l = ['l' + 'L'];
m = ['m' + 'M'];
n = ['n' + 'N'];
o = ['o' + 'O'];
p = ['p' + 'P'];
q = ['q' + 'Q'];
r = ['r' + 'R'];
s = ['s' + 'S'];
t = ['t' + 'T'];
u = ['u' + 'U'];
v = ['v' + 'V'];
w = ['w' + 'W'];
x = ['x' + 'X'];
y = ['y' + 'Y'];
z = ['z' + 'Z'];
letter = (['a'..'z']|['A'..'Z']);
cr = 13;
lf = 10;
tab = 9;
// For comments
any = [0 .. 127]; // all characters
not_curly_left = [ any - '{' ];
not_curly = [ not_curly_left - '}' ];
not_lf = [any - lf]; // take away the linefeed and carriage return
not_crlf = [not_lf - cr];
blank = (' '| tab | lf | cr);
comment = '{' not_curly* blank* '}';
comment_line = '//' not_crlf*;
Tokens
// Datatypes
integer = i n t e g e r;
boolean = b o o l e a n;
var = v a r;
number = (firstnum number*)|'0';
// Commands
assign = ':=';
begin = b e g i n;
end = e n d;
program = p r o g r a m;
if = i f;
then = t h e n;
else = e l s e;
while = w h i l e;
do = d o;
writeln = w r i t e l n;
break = b r e a k;
// Operators
l_par = '(';
r_par = ')';
plus = '+';
minus = '-';
mult = '*';
eq = '=';
lt = '<';
gt = '>';
le = '<=';
ge = '>=';
ne = '<>';
div = d i v;
mod = m o d;
and = a n d;
xor = x o r;
or = o r;
not = n o t;
true = t r u e;
false = f a l s e;
// Other
colon = ':';
semicolon = ';';
dot = '.';
comma = ',';
blank_or_comment = (blank | comment | comment_line)*;
identifier = ('_' | letter)* (number | letter | '_' | '$')*;
Ignored Tokens
blank_or_comment;
Productions
start {-> expr} =
program identifier semicolon declaration* begin statement_list end dot {-> New expr.start(identifier, [declaration.expr], statement_list.expr)};
declaration {-> expr} =
var identifier_list colon type semicolon {-> New expr.declaration(identifier_list.expr, type.expr)};
identifier_list {-> expr} =
{mult} identifier_list comma identifier
{-> New expr.identifier_list(identifier_list.expr, identifier)}
| identifier
{-> New expr.identifier(identifier)};
statement_list {-> expr}=
{mult} statement_list semicolon statement?
{-> New expr.statement_list(statement_list.expr, [statement.expr])}
| statement?
{-> New expr.statement([statement.expr])};
statement {-> expr} =
{if_then_statement} if_then_statement {-> if_then_statement.expr}
| {if_then_else_statement} if_then_else_statement {-> if_then_else_statement.expr}
| {while} while_statement {-> while_statement.expr}
| {assign} assignment {-> assignment.expr}
| {print} print {-> print.expr}
| {nested} begin statement_list end {-> statement_list.expr}
| {break} break {-> New expr.break(break)};
statement_no_short_if {-> expr} =
{if} if_then_else_statement_no_short_if {-> if_then_else_statement_no_short_if.expr}
| {while} while_statement_no_short_if {-> while_statement_no_short_if.expr}
| {assign} assignment {-> assignment.expr}
| {print} print {-> print.expr}
| {nested} begin statement_list end {-> statement_list.expr}
| {break} break {-> New expr.break(break)};
while_statement_no_short_if {-> expr} =
{while} while expression do statement_no_short_if
{-> New expr.while(expression.expr, statement_no_short_if.expr)};
while_statement {-> expr} =
{while} while expression do statement
{-> New expr.while(expression.expr, statement.expr)};
/**
* Trivial stuff
*/
print {-> expr} =
writeln l_par expression r_par
{-> New expr.print(expression.expr)};
assignment {-> expr} =
identifier assign expression
{-> New expr.assignment(identifier, expression.expr)};
type {-> expr} =
{boolean} boolean
{-> New expr.boolean(boolean)}
| {integer} integer
{-> New expr.integer(integer)};
/**
* Boolean- and arithmetic expressions
*/
expression {-> expr} =
{comparison} [left]:expression [comparison]:comparison [right]:expression2
{-> New expr.comparison(left.expr, comparison.expr, right.expr)}
| expression2
{-> expression2.expr};
expression2 {-> expr} =
{plus} [left]:expression2 plus [right]:expression3
{-> New expr.plus(left.expr, right.expr)}
| {minus} [left]:expression2 minus [right]:expression3
{-> New expr.minus(left.expr, right.expr)}
| {or} [left]:expression2 or [right]:expression3
{-> New expr.or(left.expr, right.expr)}
| {xor} [left]:expression2 xor [right]:expression3
{-> New expr.xor(left.expr, right.expr)}
| expression3
{-> expression3.expr};
expression3 {-> expr} =
{mod} [left]:expression3 mod [right]:expression4
{-> New expr.mod(left.expr, right.expr)}
| {mult} [left]:expression3 mult [right]:expression4
{-> New expr.mult(left.expr, right.expr)}
| {div} [left]:expression3 div [right]:expression4
{-> New expr.div(left.expr, right.expr)}
| {and} [left]:expression3 and [right]:expression4
{-> New expr.and(left.expr, right.expr)}
| expression4
{-> expression4.expr};
expression4 {-> expr} =
{not} not expression4
{-> New expr.not(expression4.expr)}
| {brackets} l_par expression r_par
{-> expression.expr}
| {unary_minus} minus expression4
{-> New expr.unary_minus(expression4.expr)}
| {unary_plus} plus expression4
{-> New expr.unary_plus(expression4.expr)}
| {number} number
{-> New expr.number(number)}
| {identifier} identifier
{-> New expr.identifier(identifier)}
| {true} true
{-> New expr.true(true)}
| {false} false
{-> New expr.false(false)};
comparison {-> expr} =
{gt} gt
{-> New expr.gt(gt)}
| {ge} ge
{-> New expr.ge(ge)}
| {lt} lt
{-> New expr.lt(lt)}
| {le} le
{-> New expr.le(le)}
| {ne} ne
{-> New expr.ne(ne)}
| {eq} eq
{-> New expr.eq(eq)};
/**
* If expressions
*/
if_then_statement {-> expr} =
if [left]:expression then [right]:statement
{-> New expr.if_then(left.expr, right.expr)};
if_then_else_statement {-> expr} =
if [left]:expression then [mid]:statement_no_short_if else [right]:statement
{-> New expr.if_then_else(left.expr, mid.expr, right.expr)};
if_then_else_statement_no_short_if {-> expr} =
if [left]:expression then [mid]:statement_no_short_if else [right]:statement_no_short_if
{-> New expr.if_then_else(left.expr, mid.expr, right.expr)};
Abstract Syntax Tree
expr = {start} identifier [declaration]:expr* [statement_list]:expr
| {statement} expr*
| {statement_list} [list]:expr [single]:expr*
| {declaration} [left]:expr [right]:expr
| {print} expr
| {assignment} identifier expr
| {comparison} [left]:expr [comparison]:expr [right]:expr
| {plus} [left]:expr [right]:expr
| {minus} [left]:expr [right]:expr
| {or} [left]:expr [right]:expr
| {xor} [left]:expr [right]:expr
| {mod} [left]:expr [right]:expr
| {mult} [left]:expr [right]:expr
| {div} [left]:expr [right]:expr
| {and} [left]:expr [right]:expr
| {not} expr
| {unary_minus} expr
| {unary_plus} expr
| {while} [left]:expr [right]:expr
| {if_then} [left]:expr [right]:expr
| {if_then_else} [if]:expr [then]:expr [else]:expr
| {integer} integer
| {semicolon} semicolon
| {identifier} identifier
| {identifier_list} expr identifier
| {number} number
| {boolean} boolean
| {true} true
| {false} false
| {gt} gt
| {ge} ge
| {lt} lt
| {le} le
| {ne} ne
| {eq} eq
| {break} break;