source: trunk/yao/share/antlr-2.7.7/examples/python/pascal/pascalw.g @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 7.1 KB
Line 
1// This file is part of PyANTLR. See LICENSE.txt for license
2// details..........Copyright (C) Wolfgang Haefelinger, 2004.
3//
4// $Id$
5
6//
7// Pascal Tree Super Grammar (symtab.g derives from this)
8//
9// Adapted from,
10// Pascal User Manual And Report (Second Edition-1978)
11// Kathleen Jensen - Niklaus Wirth
12//
13// By
14//
15// Hakki Dogusan dogusanh@tr-net.net.tr
16//
17// Then significantly enhanced by Piet Schoutteten
18// with some guidance by Terence Parr.  Piet added tree
19// construction, and some tree walkers.
20//
21
22options {
23    language=Python;
24}
25
26class pascal_w extends TreeParser;
27
28options {
29        importVocab = Pascal;
30    ASTLabelType = "pascal.PascalAST";
31}
32
33program
34    : programHeading
35      block
36    ;
37
38programHeading
39    : #(PROGRAM IDENT identifierList)
40    | #(UNIT IDENT)
41    ;
42
43identifier
44    : IDENT
45    ;
46
47block
48    : ( labelDeclarationPart
49      | constantDefinitionPart
50      | typeDefinitionPart
51      | variableDeclarationPart
52      | procedureAndFunctionDeclarationPart
53      | usesUnitsPart
54      | IMPLEMENTATION
55      )*
56      compoundStatement
57    ;
58
59usesUnitsPart
60    : #(USES identifierList)
61    ;
62
63labelDeclarationPart
64    : #(LABEL ( label )+)
65    ;
66
67label
68    : NUM_INT
69    ;
70
71constantDefinitionPart
72    : #(CONST ( constantDefinition )+ )
73    ;
74
75constantDefinition
76    : #(EQUAL IDENT constant)
77    ;
78
79constant
80    : NUM_INT
81    | NUM_REAL
82    | #( PLUS
83         ( NUM_INT
84         | NUM_REAL
85         | IDENT
86         )
87       )
88    | #( MINUS
89         ( NUM_INT
90         | NUM_REAL
91         | IDENT
92         )
93       )
94    | IDENT
95    | STRING_LITERAL
96    | #(CHR (NUM_INT|NUM_REAL))
97    ;
98
99string
100    : STRING_LITERAL
101    ;
102
103typeDefinitionPart
104    : #(TYPE ( typeDefinition )+)
105    ;
106
107typeDefinition
108    : #(TYPEDECL IDENT
109      ( type
110      | #(FUNCTION (formalParameterList)? resultType)
111      | #(PROCEDURE (formalParameterList)?)
112      )
113      )
114    ;
115
116type
117    : #(SCALARTYPE identifierList)
118    | #(DOTDOT constant constant)
119    | typeIdentifier
120    | structuredType
121    | #(POINTER typeIdentifier)
122    ;
123
124typeIdentifier
125    : IDENT
126    | CHAR
127    | BOOLEAN
128    | INTEGER
129    | REAL
130    | #( STRING
131         ( IDENT
132         | NUM_INT
133         | NUM_REAL
134         |
135         )
136       )
137    ;
138
139structuredType
140    : #(PACKED unpackedStructuredType)
141    | unpackedStructuredType
142    ;
143
144unpackedStructuredType
145    : arrayType
146    | recordType
147    | setType
148    | fileType
149    ;
150
151/** Note here that the syntactic diff between brackets disappears.
152 *  If the brackets mean different things semantically, we need
153 *  two different alternatives here.
154 */
155arrayType
156    : #(ARRAY typeList type)
157    ;
158
159typeList
160    : #( TYPELIST ( type )+ )
161    ;
162
163recordType
164    : #(RECORD fieldList)
165    ;
166
167fieldList
168    : #( FIELDLIST
169         ( fixedPart ( variantPart )?
170         | variantPart
171         )
172       )
173    ;
174
175fixedPart
176    : ( recordSection )+
177    ;
178
179recordSection
180    : #(FIELD identifierList type)
181    ;
182
183variantPart
184    : #( CASE tag ( variant )+ )
185    ;
186
187tag
188    : #(VARIANT_TAG identifier typeIdentifier)
189    | #(VARIANT_TAG_NO_ID typeIdentifier)
190    ;
191
192variant
193    : #(VARIANT_CASE constList fieldList)
194    ;
195
196setType
197    : #(SET type)
198    ;
199
200fileType
201    : #(FILE (type)?)
202    ;
203
204/** Yields a list of VARDECL-rooted subtrees with VAR at the overall root */
205variableDeclarationPart
206    : #( VAR ( variableDeclaration )+ )
207    ;
208
209variableDeclaration
210    : #(VARDECL identifierList type)
211    ;
212
213procedureAndFunctionDeclarationPart
214    : procedureOrFunctionDeclaration
215    ;
216
217procedureOrFunctionDeclaration
218    : procedureDeclaration
219    | functionDeclaration
220    ;
221
222procedureDeclaration
223    : #(PROCEDURE IDENT (formalParameterList)? block )
224    ;
225
226formalParameterList
227    : #(ARGDECLS ( formalParameterSection )+)
228    ;
229
230formalParameterSection
231    : parameterGroup
232    | #(VAR parameterGroup)
233    | #(FUNCTION parameterGroup)
234    | #(PROCEDURE parameterGroup)
235    ;
236
237parameterGroup
238    : #(ARGDECL identifierList typeIdentifier)
239    ;
240
241identifierList
242    : #(IDLIST (IDENT)+)
243    ;
244
245constList
246    : #(CONSTLIST ( constant )+)
247    ;
248
249functionDeclaration
250    : #(FUNCTION IDENT (formalParameterList)? resultType block)
251    ;
252
253resultType
254    : typeIdentifier
255    ;
256
257statement
258    : #(COLON label unlabelledStatement)
259    | unlabelledStatement
260    ;
261
262unlabelledStatement
263    : simpleStatement
264    | structuredStatement
265    ;
266
267simpleStatement
268    : assignmentStatement
269    | procedureStatement
270    | gotoStatement
271    ;
272
273assignmentStatement
274    : #(ASSIGN variable expression)
275    ;
276
277/** A variable is an id with a suffix and can look like:
278 *  id
279 *  id[expr,...]
280 *  id.id
281 *  id.id[expr,...]
282 *  id^
283 *  id^.id
284 *  id^.id[expr,...]
285 *  ...
286 *
287 *  LL has a really hard time with this construct as it's naturally
288 *  left-recursive.  We have to turn into a simple loop rather than
289 *  recursive loop, hence, the suffixes.  I keep in the same rule
290 *  for easy tree construction.
291 */
292variable
293    : #(LBRACK variable (expression)+)
294    | #(LBRACK2 variable (expression)+)
295    | #(DOT variable IDENT)
296    | #(POINTER variable)
297    | #(AT IDENT)
298    | IDENT
299    ;
300
301expression
302    : #(EQUAL expression expression)
303    | #(NOT_EQUAL expression expression)
304    | #(LT expression expression)
305    | #(LE expression expression)
306    | #(GE expression expression)
307    | #(GT expression expression)
308    | #(IN expression expression)
309    | #(PLUS expression (expression)?)
310    | #(MINUS expression (expression)?)
311    | #(OR expression expression)
312    | #(STAR expression expression)
313    | #(SLASH expression expression)
314    | #(DIV expression expression)
315    | #(MOD expression expression)
316    | #(AND expression expression)
317    | #(NOT expression)
318    | variable
319    | functionDesignator
320    | set
321    | NUM_INT
322    | NUM_REAL
323    | #(CHR (NUM_INT|NUM_REAL))
324    | string
325    | NIL
326    ;
327
328functionDesignator
329    : #(FUNC_CALL IDENT (parameterList)?)
330    ;
331
332parameterList
333    : #( ARGLIST (actualParameter)+ )
334    ;
335
336set
337    : #(SET (element)*)
338    ;
339
340element
341    : #(DOTDOT expression expression)
342    | expression
343    ;
344
345procedureStatement
346    : #(PROC_CALL IDENT ( parameterList )?)
347    ;
348
349actualParameter
350    : expression
351    ;
352
353gotoStatement
354    : #(GOTO label)
355    ;
356
357structuredStatement
358    : compoundStatement
359    | conditionalStatement
360    | repetetiveStatement
361    | withStatement
362    ;
363
364compoundStatement
365    : statements
366    ;
367
368statements
369    : #(BLOCK (statement)*)
370    ;
371
372conditionalStatement
373    : ifStatement
374    | caseStatement
375    ;
376
377ifStatement
378    : #(IF expression statement (statement)?)
379    ;
380
381caseStatement //pspsps ???
382    : #(CASE expression
383        ( caseListElement )+
384        ( statements )?
385       )
386    ;
387
388caseListElement
389    : #(COLON constList statement)
390    ;
391
392repetetiveStatement
393    : whileStatement
394    | repeatStatement
395    | forStatement
396    ;
397
398whileStatement
399    : #(WHILE expression statement)
400    ;
401
402repeatStatement
403    : #(REPEAT statements expression)
404    ;
405
406forStatement
407    : #(FOR IDENT forList statement)
408    ;
409
410forList
411    : #(TO initialValue finalValue)
412    | #(DOWNTO initialValue finalValue)
413    ;
414
415initialValue
416    : expression
417    ;
418
419finalValue
420    : expression
421    ;
422
423withStatement
424    : #(WITH recordVariableList statement)
425    ;
426
427recordVariableList
428    : ( variable )+
429    ;
Note: See TracBrowser for help on using the repository browser.