-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBill_Grammar.grm
360 lines (277 loc) · 9.79 KB
/
Bill_Grammar.grm
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
!------------------
! Much of this grammar is heavily based on the C# Specification
! and an LALR grammar for it written by Devin Cook
!------------------
"Name" = 'TBD'
"Version" = '1.0'
"Author" = 'Andrew Glaude'
"About" = 'BILL was created as an ideal CS1 language at RIT for an MS Project.'
| 'Based heavily on other OOP languages like C# and Java.'
"Start Symbol" = <Compilation Unit>
! ----------------------------------------------------------------- Sets
{ID Head} = {Letter} + [_]
{ID Tail} = {AlphaNumeric} + [_]
{String Ch} = {Printable} - ["]
{Char Ch} = {Printable} - ['']
{Hex Digit} = {Digit} + [abcdef] + [ABCDEF]
! ----------------------------------------------------------------- Terminals
Identifier = {ID Head} {ID Tail}* !The @ is an override char
MemberName = '.' {ID Head} {ID Tail}*
DecLiteral = {Digit}+ ( [UuLl] | [Uu][Ll] | [Ll][Uu] )?
HexLiteral = '0'[xX]{Hex Digit}+ ( [UuLl] | [Uu][Ll] | [Ll][Uu] )?
RealLiteral = {Digit}*'.'{Digit}+
StringLiteral = '"'( {String Ch} | '\'{Printable} )* '"'
CharLiteral = '' ( {Char Ch} | '\'{Printable} )''
! ----------------------------------------------------------------- Comments
Comment Line = '//'
Comment Start = '/*'
Comment End = '*/'
! ===========================================================================
! Shared by multiple sections
! ===========================================================================
<Valid ID>
::= Identifier
| this
| <Type>
<Qualified ID>
::= <Valid ID> <Member List>
<Member List>
::= <Member List> MemberName
| !Zero or more
!------------------
<Literal>
::= true
| false
| DecLiteral
| HexLiteral
| RealLiteral
| CharLiteral
| StringLiteral
| null
<Type>
::= int
| double
| char
| String
| void
| bool
| List '<' <Qualified ID> '>'
!------------------
! Expressions
!------------------
<Expression Opt>
::= <Expression>
| !Nothing
<Expression List>
::= <Expression>
| <Expression> ',' <Expression List>
<Expression>
::= <Conditional Exp> '=' <Expression>
| <Conditional Exp> '+=' <Expression>
| <Conditional Exp> '-=' <Expression>
| <Conditional Exp> '*=' <Expression>
| <Conditional Exp> '/=' <Expression>
| <Conditional Exp>
<Conditional Exp>
::= <Or Exp> '?' <Or Exp> ':' <Conditional Exp>
| <Or Exp>
<Or Exp>
::= <Or Exp> '||' <And Exp>
| <And Exp>
<And Exp>
::= <And Exp> '&&' <Equality Exp>
| <Equality Exp>
<Equality Exp>
::= <Equality Exp> '==' <Compare Exp>
| <Equality Exp> '!=' <Compare Exp>
| <Compare Exp>
<Compare Exp>
::= <Compare Exp> '<' <Add Exp>
| <Compare Exp> '>' <Add Exp>
| <Compare Exp> '<=' <Add Exp>
| <Compare Exp> '>=' <Add Exp>
| <Add Exp>
<Add Exp>
::= <Add Exp> '+' <Mult Exp>
| <Add Exp> '-' <Mult Exp>
| <Mult Exp>
<Mult Exp>
::= <Mult Exp> '*' <Unary Exp>
| <Mult Exp> '/' <Unary Exp>
| <Mult Exp> '%' <Unary Exp>
| <Unary Exp>
<Unary Exp>
::= '!' <Unary Exp>
| '-' <Unary Exp>
| '++' <Unary Exp>
| '--' <Unary Exp>
| <Object Exp>
<Object Exp>
::= <Method Exp>
<Method Exp>
::= <Method Exp> <Method>
| <Primary Exp>
<Primary Exp>
::= new <Valid ID> '(' <Arg List Opt> ')' !Object creation
| <Primary>
| '(' <Expression> ')'
<Primary>
::= <Valid ID>
| <Valid ID> '(' <Arg List Opt> ')' !Current object method
| <Literal>
! ===========================================================================
! Arguments
! ===========================================================================
<Arg List Opt>
::= <Arg List>
| !Nothing
<Arg List>
::= <Arg List> ',' <Argument>
| <Argument>
<Argument>
::= <Expression>
! ===========================================================================
! C.2.5 Statements
! ===========================================================================
<Stm List>
::= <Stm List> <Statement>
| <Statement>
<Statement>
::= <Local Var Decl> ';'
| if '(' <Expression> ')' <Statement>
| if '(' <Expression> ')' <Then Stm> else <Statement>
| for '(' <For Init Opt> ';' <For Condition Opt> ';' <For Iterator Opt> ')' <Statement>
| foreach '(' <Valid ID> Identifier in <Expression> ')' <Statement>
| while '(' <Expression> ')' <Statement>
| <Normal Stm>
<Then Stm>
::= if '(' <Expression> ')' <Then Stm> else <Then Stm>
| for '(' <For Init Opt> ';' <For Condition Opt> ';' <For Iterator Opt> ')' <Then Stm>
| foreach '(' <Valid ID> Identifier in <Expression> ')' <Then Stm>
| while '(' <Expression> ')' <Then Stm>
| <Normal Stm>
<Normal Stm>
::= break ';'
| continue ';'
| return <Expression Opt> ';'
| <Statement Exp> ';'
| ';'
| <Block>
<Block>
::= '{' <Stm List> '}'
| '{' '}'
<Variable Decs>
::= <Variable Declarator>
| <Variable Decs> ',' <Variable Declarator>
<Variable Declarator>
::= Identifier
| Identifier '=' <Variable Initializer>
<Variable Initializer>
::= <Expression>
| <Array Initializer>
! ----- Array Initializations
<Array Initializer>
::= '{' <Variable Initializer List Opt> '}'
| '{' <Variable Initializer List> ',' '}'
<Variable Initializer List Opt>
::= <Variable Initializer List>
| ! Nothing
<Variable Initializer List>
::= <Variable Initializer>
| <Variable Initializer List> ',' <Variable Initializer>
! ===========================================================================
! For Clauses
! ===========================================================================
<For Init Opt>
::= <Local Var Decl>
| <Statement Exp List>
| !Nothing
<For Iterator Opt>
::= <Statement Exp List>
| !Nothing
<For Condition Opt>
::= <Expression>
| !Nothing
<Statement Exp List>
::= <Statement Exp List> ',' <Statement Exp>
| <Statement Exp>
! ===========================================================================
! Statement Expressions & Local Variable Declaration
! ===========================================================================
! ----
! The following notes are from Devin Cook regarding LALR parsing of the C# spec.
! Due to this languages similarity to C# much of this section is reusable after
! modification to reduce complexity.
! ----
! The complex productions below are able to avoid the shift-reduce error caused
! by declaring an array. The notation used by C# (and the rest of the C++
! family) prevents an array declaration to be distinguished from an array
! assignment statement until a number of characters are read.
!
! a.b.c[2] = "Test"
! a.b.c[] = new String[3]
!
! The system CANNOT make a decision between the two until it is reading the
! contents the [ ... ].
!
! As a result, the local variable declaration below contains the full notation
! for each of the C# methods at the same level as local variable declarations.
! Since the system does not have to reduce UNTIL it is within the [ ... ], no
! shift-reduce error will occur. Nasty, huh?
<Local Var Decl>
::= <Qualified ID> <Variable Decs>
<Statement Exp>
::= <Qualified ID> '(' <Arg List Opt> ')'
| <Qualified ID> '(' <Arg List Opt> ')' <Methods Opt> <Assign Tail>
| <Qualified ID> '[' <Expression List> ']' <Methods Opt> <Assign Tail>
| <Qualified ID> '++' <Methods Opt> <Assign Tail>
| <Qualified ID> '--' <Methods Opt> <Assign Tail>
| <Qualified ID> <Assign Tail>
<Assign Tail>
::= '++'
| '--'
| '=' <Expression>
| '+=' <Expression>
| '-=' <Expression>
| '*=' <Expression>
| '/=' <Expression>
<Methods Opt>
::= <Methods Opt> <Method>
| ! Nothing
<Method>
::= MemberName
| MemberName '(' <Arg List Opt> ')' !Invocation
| '[' <Expression List> ']'
| '++'
| '--'
<Compilation Unit>
::= <Program Items>
<Program Items>
::= <Program Items> <Program Item>
| ! Nothing
<Program Item>
::= <Method Dec>
| <Class Decl>
! ===========================================================================
! Methods
! ===========================================================================
<Method Dec>
::= <Qualified ID> Identifier '(' <Formal Param List Opt> ')' <Block>
<Formal Param List Opt>
::= <Formal Param List>
| !Nothing
<Formal Param List>
::= <Formal Param>
| <Formal Param List> ',' <Formal Param>
<Formal Param>
::= <Qualified ID> Identifier
! ===========================================================================
! Class Declarations
! ===========================================================================
<Class Decl>
::= class Identifier '(' <Formal Param List Opt> ')' '{' <Class Item Decs Opt> '}'
<Class Item Decs Opt>
::= <Class Item Decs Opt> <Class Item>
| !Nothing
<Class Item>
::= <Method Dec>