-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGramatica.grm
309 lines (217 loc) · 17.9 KB
/
Gramatica.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
"Case Sensitive" = True
"Start Symbol" = <Program>
{id head} = {letter} + [_]
{id tail} = {id head} + {digit}
{string} = {printable} - ["]
id = {id head} {id tail}*
num = {digit}+ | {digit}+'.'{digit}+
str = '"' ( {string} | '\'{printable} )* '"'
log = true | false
Comment Start = '/*'
Comment End = '*/'
Comment Line = '//'
!-----------------------------------------------------------------------
!Start
<Program> ::= <Global Decls> <Decls> <Start Block> <Decls> !first = {struct, const}
<Start Block> ::= start '(' ')' <Func Block> !first = {start}
<Global Decls> ::= const '{' <Const Decls> '}' <Var Block>
| var '{' <Var Decls> '}' <Const Block>
|
<Decls> ::= <Decl> <Decls> !first = {function, procedure, struct, typedef}
|
<Decl> ::= <Func Decl> !first = {function}
| <Proc Decl> !first = {procedure}
| <Struct Block> !first = {struct, typedef}
!-----------------------------------------------------------------------
!Struct
<Struct Block> ::= struct id <Extends> '{' <Var Decls> '}' !first = {struct, typedef}
| typedef struct <Extends> '{' <Var Decls> '}' id ';'
<Extends> ::= extends struct id !first = {extends}
|
!-----------------------------------------------------------------------
!Constants and Variables Block
<Const Block> ::= const '{' <Const Decls> '}' !first = {const}
|
<Var Block> ::= var '{' <Var Decls> '}' !first = {var}
|
!-----------------------------------------------------------------------
!Variables Type and Typedef
<Type> ::= int !first = {int}
| real !first = {real}
| boolean !first = {boolean}
| string !first = {string}
| struct id !first = {struct}
<Typedef> ::= typedef <Type> id ';' !first = {typedef}
!-----------------------------------------------------------------------
!Variable Declarations
<Var Decls> ::= <Var Decl> <Var Decls> !first = {int, real, boolean, string, struct, typedef, id}
|
<Var Decl> ::= <Type> <Var> <Var List> !first = {int, real, boolean, string, struct}
| <Typedef> !first = {typedef}
| id <Var> <Var List> !first = {id}
<Var> ::= id <Arrays> !first = {id}
<Var List> ::= ',' <Var> <Var List> !first = {',', '=', ';'}
| '=' <Decl Atribute> <Var List>
| ';'
!-----------------------------------------------------------------------
!Constant Declarations
<Const Decls> ::= <Const Decl> <Const Decls> !first = {int, real, boolean, string, struct, typedef, id, local, global}
|
<Const Decl> ::= <Type> <Const> <Const List> !first = {int, real, boolean, string, struct}
| <Typedef> !first = {typedef}
| id <Const> <Const List> !first = {id}
<Const> ::= id <Arrays> '=' <Decl Atribute> !first = {id}
<Const List> ::= ',' <Const> <Const List> !first = {','}
|';' !first = {';'}
<Decl Atribute> ::= <Array Decl> !first = {'{'}
| <Expr> !first = {'!', num, str, log, id, '('}
!-----------------------------------------------------------------------
!Arrays Atributes
<Array Decl> ::= '{' <Array Def> '}' !first = {'{'}
<Array Def> ::= <Expr> <Array Expr> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Array Expr> ::= ',' <Array Def> !first = {','}
|
!-----------------------------------------------------------------------
!Arrays
<Array> ::= '[' <Index> ']' !first = {'['}
<Index> ::= id !first = {num, id}
| num
|
<Arrays> ::= <Array> <Arrays> !first = {'['}
|
!-----------------------------------------------------------------------
!Assign and Access
<Assign> ::= '=' <Expr> ';' !first = {'='}
| '++' ';' !first = {'++'}
| '--' ';' !first = {'--'}
<Access> ::= '.' id <Arrays> !first = {'.'}
<Accesses> ::= <Access> <Accesses> !first = {'.'}
|
!-----------------------------------------------------------------------
!Arguments
<Args> ::= <Expr> <Args List> !first = {'!', num, str, log, id '('}
|
<Args List> ::= ',' <Expr> <Args List> !first = {','}
|
!-----------------------------------------------------------------------
!Function Declaration
<Func Decl> ::= function <Param Type> id '(' <Params> ')' <Func Block> !first{function}
!-----------------------------------------------------------------------
!Procedure Declaration
<Proc Decl> ::= procedure id '(' <Params> ')' <Proc Block> !first{procedure}
!-----------------------------------------------------------------------
!Parameters
<Param Type> ::= <Type> !first = {int, real, boolean, string, struct}
| id !first = {id}
<Params> ::= <Param> <Params List> !first = {int, real, boolean, string, struct, id}
|
<Param> ::= <Param Type> id <Param Arrays> !first = {int, real, boolean, string, struct, id}
<Params List> ::= ',' <Param> <Params List> !first = {','}
|
<Param Arrays> ::= '[' ']' <Param Mult Arrays> !first = {'['}
|
<Param Mult Arrays> ::= '[' num ']' <Param Mult Arrays> !first = {'['}
|
!-----------------------------------------------------------------------
!Function Block
<Func Block> ::= '{' <Var Block> <Func Stms> '}' !first = {'{'}
<Func Stms> ::= <Func Stm> <Func Stms> !first = {if, while, '{', id, local, global, print, read, ';', return}
|
<Func Stm> ::= <If Stm> !first = {if}
| <While Stm> !first = {while}
| <Var Stm> !first = {id, local, global, print, read}
| return <Expr> ';' !first = {return}
<Else Stm> ::= else '{' <Func Stms> '}' !first = {else}
|
<If Stm> ::= if '(' <Log Expr> ')' then '{' <Func Stms> '}' <Else Stm> !first = {if}
<While Stm> ::= while '(' <Log Expr> ')' '{' <Func Stms> '}' !first = {while}
!-----------------------------------------------------------------------
!Procedure Block
<Proc Block> ::= '{' <Var Block> <Proc Stms> '}' !first = {'{'}
<Proc Stms> ::= <Proc Stm> <Proc Stms> !first = {if, while, '{', id, local, global, print, read, ';', return}
|
<Proc Stm> ::= <If Proc Stm> !first = {if}
| <While Proc Stm> !first = {while}
| <Var Stm> !first = {id, local, global, print, read}
<Else Proc Stm> ::= else '{' <Proc Stms> '}' !first = {else}
|
<If Proc Stm> ::= if '(' <Log Expr> ')' then '{' <Proc Stms> '}' <Else Stm> !first = {if}
<While Proc Stm> ::= while '(' <Log Expr> ')' '{' <Proc Stms> '}' !first = {while}
!-----------------------------------------------------------------------
!Variable Statements
<Var Stm> ::= <Stm Scope> !first = {local, global}
| id <Stm Id> !first = {id}
| <Stm Cmd> !first = {print, read}
<Stm Id> ::= <Assign> !first = {'=', '+', '-'}
| <Array> <Arrays> <Accesses> <Assign> !first = {'['}
| <Access> <Accesses> <Assign> !first = {'.'}
| '(' <Args> ')' ';' !first = {'('}
<Stm Scope> ::= local <Access> <Accesses> <Assign> !first = {local}
| global <Access> <Accesses> <Assign> !first = {global}
<Stm Cmd> ::= print '(' <Args> ')' ';' !first = {print}
| read '(' <Args> ')' ';' !first = {read}
!-----------------------------------------------------------------------
!Expressions
<Expr> ::= <And> <Or> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Or> ::= '||' <And> <Or> !first = {'||'}
|
<And> ::= <Equate> <And Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<And Aux> ::= '&&' <Equate> <And Aux> !first = {'&&'}
|
<Equate> ::= <Compare> <Equate Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Equate Aux> ::= '==' <Compare> <Equate Aux> !first = {'=='}
| '!=' <Compare> <Equate Aux> !first = {'!='}
|
<Compare> ::= <Add> <Compare Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Compare Aux> ::= '<' <Add> <Compare Aux> !first = {'>'}
| '>' <Add> <Compare Aux> !first = {'<'}
| '<=' <Add> <Compare Aux> !first = {'<='}
| '>=' <Add> <Compare Aux> !first = {'>='}
|
<Add> ::= <Mult> <Add Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Add Aux> ::= '+' <Mult> <Add Aux> !first = {'+'}
| '-' <Mult> <Add Aux> !first = {'-'}
|
<Mult> ::= <Unary> <Mult Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Mult Aux> ::= '*' <Unary> <Mult Aux> !first = {'*'}
| '/' <Unary> <Mult Aux> !first = {'/'}
|
<Unary> ::= '!' <Unary> !first = {'!'}
| <Value> !first = {num, str, log, id '('}
<Value> ::= '-' <Value>
| num !first = {num}
| str !first = {str}
| log !first = {log}
| local <Access> <Accesses> !first = {local}
| global <Access> <Accesses> !first = {global}
| id <Id Value> !first = {id}
| '(' <Expr> ')' !first = {'('}
<Id Value> ::= <Arrays> <Accesses> !first = {'[', '.'}
| '(' <Args> ')' !first = {'('}
!----------------------------------------------------------------------
!Logic Expression
<Log Expr> ::= <Log And> <Log Or> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Log Or> ::= '||' <Log And> <Log Or> !first = {'||'}
|
<Log And> ::= <Log Equate> <Log And Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Log And Aux> ::= '&&' <Log Equate> <Log And Aux> !first = {'&&'}
|
<Log Equate> ::= <Log Compare> <Log Equate Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Log Equate Aux> ::= '==' <Log Compare> <Log Equate Aux> !first = {'=='}
| '!=' <Log Compare> <Log Equate Aux> !first = {'!='}
|
<Log Compare> ::= <Log Unary> <Log Compare Aux> !first = {'!', num, str, log, id, '(', 'global', 'local'}
<Log Compare Aux> ::= '<' <Log Unary> <Log Compare Aux> !first = {'>'}
| '>' <Log Unary> <Log Compare Aux> !first = {'<'}
| '<=' <Log Unary> <Log Compare Aux> !first = {'<='}
| '>=' <Log Unary> <Log Compare Aux> !first = {'>='}
|
<Log Unary> ::= '!' <Log Unary> !first = {'!'}
| <Log Value> !first = {num, str, log, id '('}
<Log Value> ::= num !first = {num}
| str !first = {str}
| log !first = {log}
| local <Access> <Accesses> !first = {local}
| global <Access> <Accesses> !first = {global}
| id <Id Value> !first = {id}
| '(' <Log Expr> ')' !first = {'('}