-
Notifications
You must be signed in to change notification settings - Fork 1
/
pascal.grammar~
222 lines (181 loc) · 7.33 KB
/
pascal.grammar~
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
Package pascal;
Helpers
a = ['a' + 'A'];
b = ['b' + 'B'];
d = ['d' + 'D'];
e = ['e' + 'E'];
f = ['f' + 'F'];
g = ['g' + 'G'];
h = ['h' + 'H'];
i = ['i' + 'I'];
k = ['k' + 'K'];
l = ['l' + 'L'];
m = ['m' + 'M'];
n = ['n' + 'N'];
o = ['o' + 'O'];
p = ['p' + 'P'];
r = ['r' + 'R'];
s = ['s' + 'S'];
t = ['t' + 'T'];
u = ['u' + 'U'];
v = ['v' + 'V'];
w = ['w' + 'W'];
x = ['x' + 'X'];
ascii_char = [32 .. 127] ;
letter = [['a' .. 'z'] + ['A' .. 'Z']];
digit = ['0' .. '9'] ;
whitespace = ' '|9 | 13 | 10 ;
Tokens
end = e n d ;
div = d i v ;
var = v a r ;
begin = b e g i n ;
program = p r o g r a m ;
writeln = w r i t e l n ;
integer = i n t e g e r ;
boolean = b o o l e a n ;
true = t r u e ;
false = f a l s e ;
if = i f ;
while = w h i l e ;
break = b r e a k ;
do = d o ;
then = t h e n ;
else = e l s e ;
and = a n d ;
or = o r ;
xor = x o r ;
not = n o t ;
plus = '+' ;
minus = '-' ;
mult = '*' ;
mod = '%';
assign = ':=' ;
equal = '=' ;
less = '<' ;
greater = '>' ;
less_eq = '<=' ;
greater_eq = '>=' ;
unequal = '<>' ;
comma = ',' ;
colon = ':' ;
semicolon = ';' ;
dot = '.' ;
l_par = '(' ;
r_par = ')' ;
identifier = letter (letter | digit | '_')* ;
number = digit+ ;
comment = '{' [ascii_char - ['{' + '}']]*
'}' ;
whitespace = whitespace;
Ignored Tokens
comment,
whitespace ;
Productions
program_start {-> ast} = program identifier semicolon declarations* body dot {-> New ast.program_start(identifier, [declarations.ast], body.ast)};
declarations{->ast} = var identifier_more colon type semicolon{-> New ast.declarations(identifier_more.ast, type.ast)};
identifier_more{->ast} = {multiple}identifier_more comma identifier {->New ast.identifier_more(identifier_more.ast, identifier)}|
{single} identifier {->New ast.identifier(identifier)};
type{-> ast} =
{integer}integer {->New ast.integer(integer)} | {boolean}boolean {->New ast.boolean(boolean)} ;
body{-> ast} =
begin
statement_sequence
end {->New ast.body(statement_sequence.ast)};
statement_sequence{-> ast} =
{single} statement {->statement.ast}|
{multiple} statement_sequence semicolon statement {->New ast.statement_sequence(statement_sequence.ast, statement.ast)};
statement{-> ast} = {open} open_statement {-> open_statement.ast}|
{closed} closed_statement {-> closed_statement.ast};
open_statement{-> ast} = {open_if} open_if_statement {->New ast.open_statement(open_if_statement.ast)}|
{open_while} open_while_statement {->New ast.open_statement(open_while_statement.ast)};
closed_statement{-> ast} = {simple} simple_statement {->New ast.closed_statement(simple_statement.ast)}|
{closed_if} closed_if_statement {->New ast.closed_statement(closed_if_statement.ast)}|
{closed_while} closed_while_statement {->New ast.closed_statement(closed_while_statement.ast)};
open_if_statement{-> ast} = {open_if} if expr_top then statement {->New ast.open_if_statement(expr_top.ast, statement.ast)}|
{open_if_else} if expr_top then closed_statement else open_statement {->New ast.open_if_else_statement(expr_top.ast, closed_statement.ast, open_statement.ast)};
closed_if_statement{-> ast} = if expr_top then [do]:closed_statement else [do_else]:closed_statement {->New ast.closed_if_statement(expr_top.ast, do.ast, do_else.ast)};
open_while_statement{-> ast} = {while_o} while expr_top do open_statement {->New ast.open_while_statement(expr_top.ast, open_statement.ast)};
closed_while_statement{-> ast} = {while_c} while expr_top do closed_statement {->New ast.closed_while_statement(expr_top.ast, closed_statement.ast)};
simple_statement{-> ast} =
{writeln} writeln l_par expr_top r_par {->New ast.writeln(expr_top.ast)}|
{assignment} identifier assign expr_top {->New ast.assignment(identifier, expr_top.ast)}|
{block} begin statement_sequence end {->New ast.block(statement_sequence.ast)}|
{empty} {->New ast.empty()};
if_statement{-> ast} = {if_then} if expr_top then {->New ast.if_statement(expr_top.ast)};
compare{-> ast} = {equal} equal {->New ast.equal(equal)}|
{less} less {->New ast.less(less)}|
{greater} greater {->New ast.greater(greater)}|
{greater_eq} greater_eq {->New ast.greater_eq(greater_eq)}|
{less_eq} less_eq {->New ast.less_eq(less_eq)}|
{unequal} unequal {->New ast.unequal(unequal)};
expr_top{-> ast} =
{single} expr {-> expr.ast} |
{comparison} expr compare expr_top {->New ast.comparison(expr_top.ast,compare.ast, expr.ast)};
expr{-> ast} = {or} expr or factor {->New ast.or(expr.ast, factor.ast)}|
{xor} expr xor factor {->New ast.xor(expr.ast, factor.ast)}|
{plus} expr plus factor {->New ast.plus(expr.ast, factor.ast)}|
{minus} expr minus factor {->New ast.minus(expr.ast, factor.ast)}|
{factor} factor {-> factor.ast};
factor{-> ast} = {and} factor and term {->New ast.and(factor.ast, term.ast)}|
{mod} factor mod term {->New ast.mod(factor.ast, term.ast)}|
{mult} factor mult term {->New ast.mult(factor.ast, term.ast)}|
{div} factor div term {->New ast.div(factor.ast, term.ast)}|
{term} term {-> term.ast};
term{-> ast} = {not} not term {->New ast.not(term.ast)}|
{number} number {->New ast.number(number)}|
{identifier} identifier {->New ast.identifier(identifier)}|
{true} true {->New ast.true(true)}|
{false} false {->New ast.false(false)}|
{unaryminus_identifier} minus term {->New ast.un_minus(term.ast)}|
{unaryplus_identifier} plus term {->New ast.un_plus(term.ast)}|
{braces} l_par expr_top r_par {-> expr_top.ast} |
{break} break {-> New ast.break(break)};
Abstract Syntax Tree
ast = {program_start} identifier [declarations]:ast* [body]:ast|
{program_header} identifier |
{declarations} [identifier]:ast [type]:ast |
{identifier_more} ast identifier |
{identifier_d} identifier |
{integer} integer |
{boolean} boolean |
{body} ast |
{statement_sequence} [left]:ast [right]:ast | /*statement_sequence statement*/
{statement} ast |
{open_statement} ast |
{closed_statement} ast |
{open_if_statement} [left]:ast [right]:ast | /*expr_top open_statement*/
{open_if_else_statement} [left]:ast [mid]:ast [right]:ast | /*expr_top open_statement closed_statement*/
{closed_if_statement} [left]:ast [mid]:ast [right]:ast | /*expr_top then else */
{open_while_statement} [left]:ast [right]:ast | /*expr_top open_statement */
{closed_while_statement} [left]:ast [right]:ast | /*expr_top closed_statement */
{simple_statement} ast |
{writeln} ast |
{assignment} identifier ast |
{block} ast |
{empty} |
{if_statement} ast |
{equal} equal |
{less} less |
{greater} greater |
{greater_eq} greater_eq |
{less_eq} less_eq |
{unequal} unequal |
{comparison} [left]:ast [mid]: [right]:ast |
{expr_top} [left]:ast [right]:ast | /*expr_top expr*/
{or} [left]:ast [right]:ast | /*expr factor */
{xor} [left]:ast [right]:ast |
{plus} [left]:ast [right]:ast |
{minus} [left]:ast [right]:ast |
{and} [left]:ast [right]:ast | /*factor term */
{mod} [left]:ast [right]:ast |
{mult} [left]:ast [right]:ast |
{div} [left]:ast [right]:ast |
{not} ast |
{number} number |
{identifier} identifier |
{true} true |
{false} false |
{un_minus} ast |
{un_plus} ast |
{break} break ;