source: CIVL/grammar/CivlCParser.g@ af8a6cb

1.23 2.0 acw/focus-triggers main test-branch
Last change on this file since af8a6cb was af8a6cb, checked in by Stephen Siegel <siegel@…>, 14 years ago

Cleaning up build for grammars.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 33.6 KB
Line 
1/* Grammar for programming CIVL-C.
2 * Based on C11 grammar.
3 *
4 * Author: Stephen F. Siegel
5 * Last modified:
6 *
7 * This grammar assumes the input token stream is the result of
8 * translation phase 7, as specified in the Standard.
9 * In particular, all the preprocessing has already been
10 * done.
11 *
12 * In addition to the Standard, I borrowed from the older
13 * C grammar included with the ANTLR distribution.
14 *
15 */
16parser grammar CivlCParser;
17
18options
19{
20 language=Java;
21 tokenVocab=PreprocessorParser;
22 output=AST;
23}
24
25tokens
26{
27 ABSENT; // represents missing syntactic element
28 GENERIC_ASSOC_LIST; // generic association list
29 GENERIC_ASSOCIATION; // a generic association
30 ENUMERATION_CONSTANT; // use of enumeration constant
31 COMPOUND_LITERAL; // literal for structs, etc.
32 CALL; // function call
33 INDEX; // array subscript operator
34 ARGUMENT_LIST; // list of arguments to an operator
35 POST_INCREMENT;
36 POST_DECREMENT;
37 PRE_INCREMENT;
38 PRE_DECREMENT;
39 OPERATOR; // symbol indicating an operator
40 TYPE; // symbol indicating "type"
41 EXPR; // symbol indicating "expression"
42 PARENTHESIZED_EXPRESSION;
43 CAST; // type cast operator
44 DECLARATION; // a declaration
45 DECLARATION_SPECIFIERS; // list of declaration specifiers
46 INIT_DECLARATOR_LIST; // list of initializer-declarator pairs
47 INIT_DECLARATOR; // initializer-declaration pair
48 STRUCT_DECLARATION_LIST; // list of field declarations
49 STRUCT_DECLARATION; // a field declaration
50 SPECIFIER_QUALIFIER_LIST; // list of type specifiers and qualifiers
51 STRUCT_DECLARATOR_LIST; // list of struct/union declarators
52 STRUCT_DECLARATOR; // a struct/union declarator
53 ENUMERATOR_LIST; // list of enumerators in enum type definition
54 ENUMERATOR; // identifier and optional int constant
55 DECLARATOR; // a declarator
56 DIRECT_DECLARATOR; // declarator after removing leading *s
57 TYPE_QUALIFIER_LIST; // list of type qualifiers
58 ARRAY_SUFFIX; // [..] used in declarator
59 FUNCTION_SUFFIX; // (..) used in declarator
60 POINTER; // * used in declarator
61 PARAMETER_TYPE_LIST; // parameter list and optional "..."
62 PARAMETER_LIST; // list of parameter decls in function decl
63 PARAMETER_DECLARATION;// parameter declaration in function decl
64 IDENTIFIER_LIST; // list of parameter names only in function decl
65 TYPE_NAME; // type specification without identifier
66 ABSTRACT_DECLARATOR; // declarator without identifier
67 DIRECT_ABSTRACT_DECLARATOR; // direct declarator sans identifier
68 SCALAR_INITIALIZER; //
69 INITIALIZER_LIST;
70 DESIGNATED_INITIALIZER;
71 DESIGNATION;
72 ARRAY_ELEMENT_DESIGNATOR;
73 FIELD_DESIGNATOR;
74 IDENTIFIER_LABELED_STATEMENT;
75 CASE_LABELED_STATEMENT;
76 DEFAULT_LABELED_STATEMENT;
77 COMPOUND_STATEMENT;
78 BLOCK_ITEM_LIST;
79 EXPRESSION_STATEMENT;
80 TRANSLATION_UNIT;
81 DECLARATION_LIST;
82 FUNCTION_DEFINITION;
83 TYPEDEF_NAME;
84 TOKEN_LIST;
85}
86
87scope Symbols {
88 Set<String> types; // to keep track of typedefs
89 Set<String> enumerationConstants; // to keep track of enum constants
90 boolean isFunctionDefinition; // "function scope": entire function definition
91}
92
93scope DeclarationScope {
94 boolean isTypedef; // is the current declaration a typedef
95}
96
97@header
98{
99package edu.udel.cis.vsl.civl.civlc.parse.common;
100
101import java.util.Set;
102import java.util.HashSet;
103import edu.udel.cis.vsl.civl.civlc.parse.IF.RuntimeParseException;
104}
105
106@members {
107 @Override
108 public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
109 String hdr = getErrorHeader(e);
110 String msg = getErrorMessage(e, tokenNames);
111
112 throw new RuntimeParseException(hdr+" "+msg, e.token);
113 }
114
115 @Override
116 public void emitErrorMessage(String msg) { // don't try to recover!
117 throw new RuntimeParseException(msg);
118 }
119
120 boolean isTypeName(String name) {
121 for (Object scope : Symbols_stack)
122 if (((Symbols_scope)scope).types.contains(name)) return true;
123 return false;
124 }
125
126 boolean isEnumerationConstant(String name) {
127 boolean answer = false;
128
129 // System.err.print("Is "+name+" an enumeration constant: ");
130 for (Object scope : Symbols_stack) {
131 if (((Symbols_scope)scope).enumerationConstants.contains(name)) {
132 answer=true;
133 break;
134 }
135 }
136 // System.err.println(answer);
137 // System.err.flush();
138 return answer;
139 }
140}
141
142
143/* ***** A.2.1: Expressions ***** */
144
145/* Constants from A.1.5 */
146
147constant
148 : enumerationConstant
149 | INTEGER_CONSTANT
150 | FLOATING_CONSTANT
151 | CHARACTER_CONSTANT
152 ;
153
154enumerationConstant
155 : {isEnumerationConstant(input.LT(1).getText())}? IDENTIFIER ->
156 ^(ENUMERATION_CONSTANT IDENTIFIER)
157 ;
158
159/* 6.5.1 */
160primaryExpression
161 : constant
162 | // {!isEnumerationConstant(input.LT(1).getText())}?
163 IDENTIFIER
164 | STRING_LITERAL
165 | LPAREN expression RPAREN
166 -> ^(PARENTHESIZED_EXPRESSION LPAREN expression RPAREN)
167 | genericSelection
168 | PROC LSQUARE expression RSQUARE DOT IDENTIFIER
169 -> ^(PROC expression IDENTIFIER)
170 ;
171
172/* 6.5.1.1 */
173
174genericSelection
175 : GENERIC LPAREN assignmentExpression COMMA genericAssocList
176 RPAREN
177 -> ^(GENERIC assignmentExpression genericAssocList)
178 ;
179
180/* 6.5.1.1 */
181genericAssocList
182 : genericAssociation (COMMA genericAssociation)*
183 -> ^(GENERIC_ASSOC_LIST genericAssociation+)
184 ;
185
186/* 6.5.1.1 */
187genericAssociation
188 : typeName COLON assignmentExpression
189 -> ^(GENERIC_ASSOCIATION typeName assignmentExpression)
190 | DEFAULT COLON assignmentExpression
191 -> ^(GENERIC_ASSOCIATION DEFAULT assignmentExpression)
192 ;
193
194/* 6.5.2 */
195postfixExpression
196 : (postfixExpressionRoot -> postfixExpressionRoot)
197 ( l=LSQUARE expression RSQUARE
198 -> ^(OPERATOR
199 INDEX[$l]
200 ^(ARGUMENT_LIST $postfixExpression expression)
201 RSQUARE)
202 | LPAREN argumentExpressionList RPAREN
203 -> ^(CALL LPAREN $postfixExpression argumentExpressionList RPAREN)
204 | DOT IDENTIFIER
205 -> ^(DOT $postfixExpression IDENTIFIER)
206 | ARROW IDENTIFIER
207 -> ^(ARROW $postfixExpression IDENTIFIER)
208 | p=PLUSPLUS
209 -> ^(OPERATOR POST_INCREMENT[$p]
210 ^(ARGUMENT_LIST $postfixExpression))
211 | AT IDENTIFIER
212 -> ^(AT $postfixExpression IDENTIFIER)
213 | m=MINUSMINUS
214 -> ^(OPERATOR POST_DECREMENT[$m]
215 ^(ARGUMENT_LIST $postfixExpression))
216 )*
217 ;
218
219/*
220 * The "(typename) {...}" is a "compound literal".
221 * See C11 Sec. 6.5.2.5. I don't know what
222 * it means when it ends with an extra COMMA.
223 * I assume it doesn't mean anything and is just
224 * allowed as a convenience for the poor C programmer
225 * (but why?).
226 *
227 * Ambiguity: need to distinguish the compound literal
228 * "(typename) {...}" from the primaryExpression
229 * "(expression)". Presence of '{' implies it must
230 * be the compound literal.
231 */
232postfixExpressionRoot
233 : (LPAREN typeName RPAREN LCURLY)=>
234 LPAREN typeName RPAREN LCURLY initializerList
235 ( RCURLY
236 | COMMA RCURLY
237 )
238 -> ^(COMPOUND_LITERAL LPAREN typeName initializerList RCURLY)
239 | primaryExpression
240 ;
241
242/* 6.5.2 */
243argumentExpressionList
244 : -> ^(ARGUMENT_LIST)
245 | assignmentExpression (COMMA assignmentExpression)*
246 -> ^(ARGUMENT_LIST assignmentExpression+)
247 ;
248
249/* 6.5.3 */
250unaryExpression
251 : postfixExpression
252 | p=PLUSPLUS unaryExpression
253 -> ^(OPERATOR PRE_INCREMENT[$p]
254 ^(ARGUMENT_LIST unaryExpression))
255 | m=MINUSMINUS unaryExpression
256 -> ^(OPERATOR PRE_DECREMENT[$m]
257 ^(ARGUMENT_LIST unaryExpression))
258 | unaryOperator castExpression
259 -> ^(OPERATOR unaryOperator ^(ARGUMENT_LIST castExpression))
260 | (SIZEOF LPAREN typeName)=> SIZEOF LPAREN typeName RPAREN
261 -> ^(SIZEOF TYPE typeName)
262 | SIZEOF unaryExpression
263 -> ^(SIZEOF EXPR unaryExpression)
264 | ALIGNOF LPAREN typeName RPAREN
265 -> ^(ALIGNOF typeName)
266 | spawnExpression
267 ;
268
269
270spawnExpression
271 : SPAWN postfixExpressionRoot LPAREN
272 argumentExpressionList RPAREN
273 -> ^(SPAWN postfixExpressionRoot argumentExpressionList)
274 ;
275
276
277/* 6.5.3 */
278unaryOperator
279 : AMPERSAND | STAR | PLUS | SUB | TILDE | NOT
280 ;
281
282/* 6.5.4 */
283// ambiguity: (expr) is a unary expression and looks
284// like (typeName).
285castExpression
286 : (LPAREN typeName RPAREN)=> l=LPAREN typeName RPAREN castExpression
287 -> ^(CAST typeName castExpression $l)
288 | unaryExpression
289 ;
290
291/* 6.5.5 */
292multiplicativeExpression
293 : (castExpression -> castExpression)
294 ( STAR y=castExpression
295 -> ^(OPERATOR STAR ^(ARGUMENT_LIST $multiplicativeExpression $y))
296 | DIV y=castExpression
297 -> ^(OPERATOR DIV ^(ARGUMENT_LIST $multiplicativeExpression $y))
298 | MOD y=castExpression
299 -> ^(OPERATOR MOD ^(ARGUMENT_LIST $multiplicativeExpression $y))
300 )*
301 ;
302
303/* 6.5.6 */
304additiveExpression
305 : (multiplicativeExpression -> multiplicativeExpression)
306 ( PLUS y=multiplicativeExpression
307 -> ^(OPERATOR PLUS ^(ARGUMENT_LIST $additiveExpression $y))
308 | SUB y=multiplicativeExpression
309 -> ^(OPERATOR SUB ^(ARGUMENT_LIST $additiveExpression $y))
310 )*
311 ;
312
313/* 6.5.7 */
314shiftExpression
315 : (additiveExpression -> additiveExpression)
316 ( SHIFTLEFT y=additiveExpression
317 -> ^(OPERATOR SHIFTLEFT ^(ARGUMENT_LIST $shiftExpression $y))
318 | SHIFTRIGHT y=additiveExpression
319 -> ^(OPERATOR SHIFTRIGHT ^(ARGUMENT_LIST $shiftExpression $y))
320 )*
321 ;
322
323/* 6.5.8 */
324relationalExpression
325 : ( shiftExpression -> shiftExpression )
326 ( relationalOperator y=shiftExpression
327 -> ^(OPERATOR relationalOperator ^(ARGUMENT_LIST $relationalExpression $y))
328 )*
329 ;
330
331relationalOperator
332 : LT | GT | LTE | GTE
333 ;
334
335/* 6.5.9 */
336equalityExpression
337 : ( relationalExpression -> relationalExpression )
338 ( equalityOperator y=relationalExpression
339 -> ^(OPERATOR equalityOperator ^(ARGUMENT_LIST $equalityExpression $y))
340 )*
341 ;
342
343equalityOperator
344 : EQUALS | NEQ
345 ;
346
347/* 6.5.10 */
348andExpression
349 : ( equalityExpression -> equalityExpression )
350 ( AMPERSAND y=equalityExpression
351 -> ^(OPERATOR AMPERSAND ^(ARGUMENT_LIST $andExpression $y))
352 )*
353 ;
354
355/* 6.5.11 */
356exclusiveOrExpression
357 : ( andExpression -> andExpression )
358 ( BITXOR y=andExpression
359 -> ^(OPERATOR BITXOR ^(ARGUMENT_LIST $exclusiveOrExpression $y))
360 )*
361 ;
362
363/* 6.5.12 */
364inclusiveOrExpression
365 : ( exclusiveOrExpression -> exclusiveOrExpression )
366 ( BITOR y=exclusiveOrExpression
367 -> ^(OPERATOR BITOR ^(ARGUMENT_LIST $inclusiveOrExpression $y))
368 )*
369 ;
370
371/* 6.5.13 */
372logicalAndExpression
373 : ( inclusiveOrExpression -> inclusiveOrExpression )
374 ( AND y=inclusiveOrExpression
375 -> ^(OPERATOR AND ^(ARGUMENT_LIST $logicalAndExpression $y))
376 )*
377 ;
378
379/* 6.5.14 */
380logicalOrExpression
381 : ( logicalAndExpression -> logicalAndExpression )
382 ( OR y=logicalAndExpression
383 -> ^(OPERATOR OR ^(ARGUMENT_LIST $logicalOrExpression $y))
384 )*
385 ;
386
387
388/* 6.5.15 */
389conditionalExpression
390 : logicalOrExpression
391 ( -> logicalOrExpression
392 | QMARK expression COLON conditionalExpression
393 -> ^(OPERATOR QMARK
394 ^(ARGUMENT_LIST
395 logicalOrExpression
396 expression
397 conditionalExpression))
398 )
399 ;
400
401/* 6.5.16
402 * conditionalExpression or
403 * Root: OPERATOR
404 * Child 0: assignmentOperator
405 * Child 1: ARGUMENT_LIST
406 * Child 1.0: unaryExpression
407 * Child 1.1: assignmentExpression
408 */
409assignmentExpression
410 : (unaryExpression assignmentOperator)=>
411 unaryExpression assignmentOperator assignmentExpression
412 -> ^(OPERATOR assignmentOperator
413 ^(ARGUMENT_LIST unaryExpression assignmentExpression))
414 | conditionalExpression
415 ;
416
417/* 6.5.16 */
418assignmentOperator
419 : ASSIGN | STAREQ | DIVEQ | MODEQ | PLUSEQ | SUBEQ
420 | SHIFTLEFTEQ | SHIFTRIGHTEQ | BITANDEQ | BITXOREQ | BITOREQ
421 ;
422
423/* 6.5.17
424 * assignmentExpression or
425 * Root: OPERATOR
426 * Child 0: COMMA
427 * Child 1: ARGUMENT_LIST
428 * Child 1.0: arg0
429 * Child 1.1: arg1
430 */
431expression
432 : ( x=assignmentExpression -> assignmentExpression)
433 ( COMMA y=assignmentExpression
434 -> ^(OPERATOR COMMA ^(ARGUMENT_LIST $x $y))
435 )*
436 ;
437
438/* 6.6 */
439constantExpression
440 : conditionalExpression
441 ;
442
443/* ***** A.2.2: Declarations ***** */
444
445/* 6.7.
446 *
447 * This rule will construct either a DECLARATION or
448 * STATICASSERT tree:
449
450 * Root: DECLARATION
451 * Child 0: declarationSpecifiers
452 * Child 1: initDeclaratorList or ABSENT
453 *
454 * Root: STATICASSERT
455 * Child 0: constantExpression
456 * Child 1: stringLiteral
457 *
458 * The declarationSpecifiers rule returns a bit telling whether
459 * "typedef" occurred among the specifiers. This bit is passed
460 * to the initDeclaratorList rule, and down the call chain,
461 * where eventually an IDENTIFIER should be reached. At that point,
462 * if the bit is true, the IDENTIFIER is added to the set of typedef
463 * names.
464 *
465 */
466declaration
467scope DeclarationScope;
468@init {
469 $DeclarationScope::isTypedef = false;
470}
471 : d=declarationSpecifiers
472 (
473 i=initDeclaratorList
474 -> ^(DECLARATION $d $i)
475 | -> ^(DECLARATION $d ABSENT)
476 )
477 SEMI
478 | staticAssertDeclaration
479 ;
480
481/* 6.7
482 * Root: DECLARATION_SPECIFIERS
483 * Children: declarationSpecifier (any number)
484 */
485declarationSpecifiers
486 : l=declarationSpecifierList
487 -> ^(DECLARATION_SPECIFIERS declarationSpecifierList)
488 ;
489
490/* Tree: flat list of declarationSpecifier
491 */
492declarationSpecifierList
493 : (
494 {!$DeclarationScope::isTypedef || input.LT(2).getType() != SEMI }?
495 s=declarationSpecifier
496 )+
497 ;
498
499declarationSpecifier
500 : s=storageClassSpecifier
501 | typeSpecifierOrQualifier
502 | functionSpecifier
503 | alignmentSpecifier
504 ;
505
506/*
507 * I factored this out of the declarationSpecifiers rule
508 * to deal with the ambiguity of "ATOMIC" in one place.
509 * "ATOMIC ( typeName )" matches atomicTypeSpecifier, which
510 * is a typeSpecifier. "ATOMIC" matches typeQualifier.
511 * When you see "ATOMIC" all you have to do is look at the
512 * next token. If it's '(', typeSpecifier is it.
513 */
514typeSpecifierOrQualifier
515 : (typeSpecifier)=> typeSpecifier
516 | typeQualifier
517 ;
518
519/* 6.7
520 * Root: INIT_DECLARATOR_LIST
521 * Children: initDeclarator
522 */
523initDeclaratorList
524 : i+=initDeclarator (COMMA i+=initDeclarator)*
525 -> ^(INIT_DECLARATOR_LIST $i+)
526 ;
527
528/* 6.7
529 * Root: INIT_DECLARATOR
530 * Child 0: declarator
531 * Child 1: initializer or ABSENT
532 */
533initDeclarator
534 : d=declarator
535 ( -> ^(INIT_DECLARATOR $d ABSENT)
536 | (ASSIGN i=initializer) -> ^(INIT_DECLARATOR $d $i)
537 )
538 ;
539
540/* 6.7.1 */
541storageClassSpecifier
542 : TYPEDEF {$DeclarationScope::isTypedef = true;}
543 | (EXTERN | STATIC | THREADLOCAL | AUTO | REGISTER)
544 ;
545
546/* 6.7.2 */
547typeSpecifier
548 : VOID | CHAR | SHORT | INT | LONG | FLOAT | DOUBLE
549 | SIGNED | UNSIGNED | BOOL | COMPLEX
550 | atomicTypeSpecifier
551 | structOrUnionSpecifier
552 | enumSpecifier
553 | typedefName
554 ;
555
556/* 6.7.2.1
557 * Root: STRUCT or UNION
558 * Child 0: IDENTIFIER (the tag) or ABSENT
559 * Child 1: structDeclarationList or ABSENT
560 */
561structOrUnionSpecifier
562 : structOrUnion
563 ( IDENTIFIER LCURLY structDeclarationList RCURLY
564 -> ^(structOrUnion IDENTIFIER structDeclarationList RCURLY)
565 | LCURLY structDeclarationList RCURLY
566 -> ^(structOrUnion ABSENT structDeclarationList RCURLY)
567 | IDENTIFIER
568 -> ^(structOrUnion IDENTIFIER ABSENT)
569 )
570 ;
571
572/* 6.7.2.1 */
573structOrUnion
574 : STRUCT | UNION
575 ;
576
577/* 6.7.2.1
578 * Root: STRUCT_DECLARATION_LIST
579 * Children: structDeclaration
580 */
581structDeclarationList
582 : structDeclaration+
583 -> ^(STRUCT_DECLARATION_LIST structDeclaration+)
584 ;
585
586/* 6.7.2.1
587 * Two possible trees:
588 *
589 * Root: STRUCT_DECLARATION
590 * Child 0: specifierQualifierList
591 * Child 1: structDeclaratorList or ABSENT
592 *
593 * or
594 *
595 * staticAssertDeclaration (root: STATICASSERT)
596 */
597structDeclaration
598scope DeclarationScope;
599@init {
600 $DeclarationScope::isTypedef = false;
601}
602 : s=specifierQualifierList
603 ( -> ^(STRUCT_DECLARATION $s ABSENT)
604 | structDeclaratorList
605 -> ^(STRUCT_DECLARATION $s structDeclaratorList)
606 )
607 SEMI
608 | staticAssertDeclaration
609 ;
610
611/* 6.7.2.1
612 * Root: SPECIFIER_QUALIFIER_LIST
613 * Children: typeSpecifierOrQualifier
614 */
615specifierQualifierList
616 : typeSpecifierOrQualifier+
617 -> ^(SPECIFIER_QUALIFIER_LIST typeSpecifierOrQualifier+)
618 ;
619
620/* 6.7.2.1
621 * Root: STRUCT_DECLARATOR_LIST
622 * Children: structDeclarator (at least 1)
623 */
624structDeclaratorList
625 : s+=structDeclarator (COMMA s+=structDeclarator)*
626 -> ^(STRUCT_DECLARATOR_LIST $s+)
627 ;
628
629/* 6.7.2.1
630 * Root: STRUCT_DECLARATOR
631 * Child 0: declarator or ABSENT
632 * Child 1: constantExpression or ABSENT
633 */
634structDeclarator
635 : declarator
636 ( -> ^(STRUCT_DECLARATOR declarator ABSENT)
637 | COLON constantExpression
638 -> ^(STRUCT_DECLARATOR declarator constantExpression)
639 )
640 | COLON constantExpression
641 -> ^(STRUCT_DECLARATOR ABSENT constantExpression)
642 ;
643
644/* 6.7.2.2
645 * Root: ENUM
646 * Child 0: IDENTIFIER (tag) or ABSENT
647 * Child 1: enumeratorList
648 */
649enumSpecifier
650 : ENUM
651 ( IDENTIFIER
652 -> ^(ENUM IDENTIFIER ABSENT)
653 | IDENTIFIER LCURLY enumeratorList COMMA? RCURLY
654 -> ^(ENUM IDENTIFIER enumeratorList)
655 | LCURLY enumeratorList COMMA? RCURLY
656 -> ^(ENUM ABSENT enumeratorList)
657 )
658 ;
659
660/* 6.7.2.2
661 * Root: ENUMERATOR_LIST
662 * Children: enumerator
663 */
664enumeratorList
665 : enumerator (COMMA enumerator)*
666 -> ^(ENUMERATOR_LIST enumerator+)
667 ;
668
669/* 6.7.2.2
670 * Root: ENUMERATOR
671 * Child 0: IDENTIFIER
672 * Child 1: constantExpression or ABSENT
673 */
674enumerator
675 : IDENTIFIER
676 {
677 $Symbols::enumerationConstants.add($IDENTIFIER.text);
678 // System.err.println("define enum constant "+$IDENTIFIER.text);
679 }
680 ( -> ^(ENUMERATOR IDENTIFIER ABSENT)
681 | (ASSIGN constantExpression)
682 -> ^(ENUMERATOR IDENTIFIER constantExpression)
683 )
684 ;
685
686/* 6.7.2.4 */
687atomicTypeSpecifier
688 : ATOMIC LPAREN typeName RPAREN
689 -> ^(ATOMIC typeName)
690 ;
691
692/* 6.7.3 */
693typeQualifier
694 : CONST | RESTRICT | VOLATILE | ATOMIC
695 | INPUT | OUTPUT
696 ;
697
698/* 6.7.4 */
699functionSpecifier
700 : INLINE | NORETURN
701 ;
702
703/* 6.7.5
704 * Root: ALIGNAS
705 * Child 0: TYPE or EXPR
706 * Child 1: typeName (if Child 0 is TYPE) or constantExpression
707 * (if Child 0 is EXPR)
708 */
709alignmentSpecifier
710 : ALIGNAS LPAREN
711 ( typeName RPAREN
712 -> ^(ALIGNAS TYPE typeName)
713 | constantExpression RPAREN
714 -> ^(ALIGNAS EXPR constantExpression)
715 )
716 ;
717
718/* 6.7.6
719 * Root: DECLARATOR
720 * Child 0: pointer or ABSENT
721 * Child 1: directDeclarator
722 */
723declarator
724 : d=directDeclarator
725 -> ^(DECLARATOR ABSENT $d)
726 | pointer d=directDeclarator
727 -> ^(DECLARATOR pointer $d)
728 ;
729
730/* 6.7.6
731 * Root: DIRECT_DECLARATOR
732 * Child 0: directDeclaratorPrefix
733 * Children 1..: list of directDeclaratorSuffix (may be empty)
734 */
735directDeclarator
736 : p=directDeclaratorPrefix
737 ( -> ^(DIRECT_DECLARATOR $p)
738 | s+=directDeclaratorSuffix+ ->^(DIRECT_DECLARATOR $p $s+)
739 )
740 ;
741
742/*
743 * Tree: either an IDENTIFIER or a declarator.
744 */
745directDeclaratorPrefix
746 : IDENTIFIER
747 {
748 if ($DeclarationScope::isTypedef) {
749 $Symbols::types.add($IDENTIFIER.text);
750 //System.err.println("define type "+$IDENTIFIER.text);
751 }
752 }
753 | LPAREN! declarator RPAREN!
754 ;
755
756
757directDeclaratorSuffix
758 : directDeclaratorArraySuffix
759 | directDeclaratorFunctionSuffix
760 ;
761
762/*
763 * Root: ARRAY_SUFFIX
764 * child 0: LSQUARE (for source information)
765 * child 1: STATIC or ABSENT
766 * child 2: TYPE_QUALIFIER_LIST
767 * child 3: expression (array extent),
768 * "*" (unspecified variable length), or ABSENT
769 * child 4: RSQUARE (for source information)
770 */
771directDeclaratorArraySuffix
772 : LSQUARE
773 ( typeQualifierList_opt assignmentExpression_opt RSQUARE
774 -> ^(ARRAY_SUFFIX LSQUARE ABSENT typeQualifierList_opt
775 assignmentExpression_opt RSQUARE)
776 | STATIC typeQualifierList_opt assignmentExpression RSQUARE
777 -> ^(ARRAY_SUFFIX LSQUARE STATIC typeQualifierList_opt
778 assignmentExpression RSQUARE)
779 | typeQualifierList STATIC assignmentExpression RSQUARE
780 -> ^(ARRAY_SUFFIX LSQUARE STATIC typeQualifierList
781 assignmentExpression RSQUARE)
782 | typeQualifierList_opt STAR RSQUARE
783 -> ^(ARRAY_SUFFIX LSQUARE ABSENT typeQualifierList_opt
784 STAR RSQUARE)
785 )
786 ;
787
788/*
789 * Root: FUNCTION_SUFFIX
790 * child 0: LPAREN (for source information)
791 * child 1: either parameterTypeList or identifierList or ABSENT
792 * child 2: RPAREN (for source information)
793 */
794directDeclaratorFunctionSuffix
795 : LPAREN
796 ( parameterTypeList RPAREN
797 -> ^(FUNCTION_SUFFIX LPAREN parameterTypeList RPAREN)
798 | identifierList RPAREN
799 -> ^(FUNCTION_SUFFIX LPAREN identifierList RPAREN)
800 | RPAREN -> ^(FUNCTION_SUFFIX LPAREN ABSENT RPAREN)
801 )
802 ;
803
804/*
805 * Root: TYPE_QUALIFIER_LIST
806 * Children: typeQualifier
807 */
808typeQualifierList_opt
809 : typeQualifier* -> ^(TYPE_QUALIFIER_LIST typeQualifier*)
810 ;
811
812/*
813 * Tree: assignmentExpression or ABSENT
814 */
815assignmentExpression_opt
816 : -> ABSENT
817 | assignmentExpression
818 ;
819
820/* 6.7.6
821 * Root: POINTER
822 * chilren: STAR
823 */
824pointer
825 : pointer_part+ -> ^(POINTER pointer_part+)
826 ;
827
828/*
829 * Root: STAR
830 * child 0: TYPE_QUALIFIER_LIST
831 */
832pointer_part
833 : STAR typeQualifierList_opt -> ^(STAR typeQualifierList_opt)
834 ;
835
836/* 6.7.6
837 * Root: TYPE_QUALIFIER_LIST
838 * children: typeQualifier
839 */
840typeQualifierList
841 : typeQualifier+ -> ^(TYPE_QUALIFIER_LIST typeQualifier+)
842 ;
843
844/* 6.7.6
845 * Root: PARAMETER_TYPE_LIST
846 * child 0: parameterList (at least 1 parameter declaration)
847 * child 1: ELLIPSIS or ABSENT
848 *
849 * If the parameterTypeList occurs in a function prototype
850 * (that is not part of a function definition), it defines
851 * a new scope (a "function prototype scope"). If it occurs
852 * in a function definition, it does not define a new scope.
853 */
854
855parameterTypeList
856 : {$Symbols::isFunctionDefinition}? parameterTypeListWithoutScope
857 | parameterTypeListWithScope
858 ;
859
860parameterTypeListWithScope
861scope Symbols;
862@init {
863 $Symbols::types = new HashSet<String>();
864 $Symbols::enumerationConstants = new HashSet<String>();
865 $Symbols::isFunctionDefinition = false;
866}
867 : parameterTypeListWithoutScope
868 ;
869
870parameterTypeListWithoutScope
871 : parameterList
872 ( -> ^(PARAMETER_TYPE_LIST parameterList ABSENT)
873 | COMMA ELLIPSIS
874 -> ^(PARAMETER_TYPE_LIST parameterList ELLIPSIS)
875 )
876 ;
877
878/* 6.7.6
879 * Root: PARAMETER_LIST
880 * children: parameterDeclaration
881 */
882parameterList
883 : parameterDeclaration (COMMA parameterDeclaration)*
884 -> ^(PARAMETER_LIST parameterDeclaration+)
885 ;
886
887/* 6.7.6
888 * Root: PARAMETER_DECLARATION
889 * Child 0: declarationSpecifiers
890 * Child 1: declarator, or abstractDeclarator, or ABSENT
891 */
892parameterDeclaration
893scope DeclarationScope;
894@init {
895 $DeclarationScope::isTypedef = false;
896}
897 : declarationSpecifiers
898 ( -> ^(PARAMETER_DECLARATION declarationSpecifiers ABSENT)
899 | declaratorOrAbstractDeclarator
900 -> ^(PARAMETER_DECLARATION
901 declarationSpecifiers declaratorOrAbstractDeclarator)
902 )
903 ;
904
905
906// this has non-LL* decision due to recursive rule invocations
907// reachable from alts 1,2... E.g., both can start with pointer.
908
909declaratorOrAbstractDeclarator
910 : (declarator)=> declarator
911 | abstractDeclarator
912 ;
913
914
915/* 6.7.6
916 * Root: IDENTIFIER_LIST
917 * children: IDENTIFIER (at least 1)
918 */
919identifierList
920 : IDENTIFIER ( COMMA IDENTIFIER )*
921 -> ^(IDENTIFIER_LIST IDENTIFIER+)
922 ;
923
924/* 6.7.6. This is how a type is described without attaching
925 * it to an identifier.
926 * Root: TYPE_NAME
927 * child 0: specifierQualifierList
928 * child 1: abstractDeclarator or ABSENT
929 */
930typeName
931 : specifierQualifierList
932 ( -> ^(TYPE_NAME specifierQualifierList ABSENT)
933 | abstractDeclarator
934 -> ^(TYPE_NAME specifierQualifierList abstractDeclarator)
935 )
936 ;
937
938/* 6.7.7. Abstract declarators are like declarators without
939 * the IDENTIFIER.
940 *
941 * Root: ABSTRACT_DECLARATOR
942 * Child 0. pointer (may be ABSENT). Some number of *s with possible
943 * type qualifiers.
944 * Child 1. directAbstractDeclarator (may be ABSENT).
945 */
946abstractDeclarator
947 : pointer
948 -> ^(ABSTRACT_DECLARATOR pointer ABSENT)
949 | directAbstractDeclarator
950 -> ^(ABSTRACT_DECLARATOR ABSENT directAbstractDeclarator)
951 | pointer directAbstractDeclarator
952 -> ^(ABSTRACT_DECLARATOR pointer directAbstractDeclarator)
953 ;
954
955/* 6.7.7
956 *
957 * Root: DIRECT_ABSTRACT_DECLARATOR
958 * Child 0. abstract declarator or ABSENT.
959 * Children 1..: any number of direct abstract declarator suffixes
960 *
961 * Note that the difference between this and a directDeclarator
962 * is that Child 0 of a direct declarator would be either
963 * an IDENTIFIER or a declarator, but never ABSENT.
964 */
965directAbstractDeclarator
966 : LPAREN abstractDeclarator RPAREN directAbstractDeclaratorSuffix*
967 -> ^(DIRECT_ABSTRACT_DECLARATOR abstractDeclarator
968 directAbstractDeclaratorSuffix*)
969 | directAbstractDeclaratorSuffix+
970 -> ^(DIRECT_ABSTRACT_DECLARATOR ABSENT directAbstractDeclaratorSuffix+)
971 ;
972
973
974/* 6.7.8
975 * Root: TYPEDEF_NAME
976 * Child 0: IDENTIFIER
977 *
978 * Ambiguity: example:
979 * typedef int foo;
980 * typedef int foo;
981 *
982 * This is perfectly legal: you can define a typedef twice
983 * as long as both definitions are equivalent. However,
984 * the first definition causes foo to be entered into the type name
985 * table, so when parsing the second definition, foo is
986 * interpreted as a typedefName (a type specifier), and the
987 * declaration would have empty declarator. This is not
988 * what you want, so you have to forbid it somehow. I do this
989 * by requiring that if you are "in" a typedef, a typedef name
990 * cannot be immediately followed by a semicolon. This is sound
991 * because the C11 Standard requires at least one declarator
992 * to be present in a typedef.
993 */
994typedefName
995 : {isTypeName(input.LT(1).getText())}? IDENTIFIER
996 -> ^(TYPEDEF_NAME IDENTIFIER)
997 ;
998
999/* 6.7.7
1000 * Two possibilities:
1001 *
1002 * Root: ARRAY_SUFFIX
1003 * Child 0: STATIC or ABSENT
1004 * Child 1: typeQualifierList or ABSENT
1005 * Child 2: expression or STAR or ABSENT
1006 *
1007 * Root: FUNCTION_SUFFIX
1008 * Child 0: parameterTypeList or ABSENT
1009 */
1010directAbstractDeclaratorSuffix
1011 : LSQUARE
1012 ( typeQualifierList_opt assignmentExpression_opt RSQUARE
1013 -> ^(ARRAY_SUFFIX ABSENT typeQualifierList_opt
1014 assignmentExpression_opt)
1015 | STATIC typeQualifierList_opt assignmentExpression RSQUARE
1016 -> ^(ARRAY_SUFFIX STATIC typeQualifierList_opt
1017 assignmentExpression)
1018 | typeQualifierList STATIC assignmentExpression RSQUARE
1019 -> ^(ARRAY_SUFFIX STATIC typeQualifierList assignmentExpression)
1020 | STAR RSQUARE
1021 -> ^(ARRAY_SUFFIX ABSENT ABSENT STAR)
1022 )
1023 | LPAREN
1024 ( parameterTypeList RPAREN
1025 -> ^(FUNCTION_SUFFIX parameterTypeList)
1026 | RPAREN
1027 -> ^(FUNCTION_SUFFIX ABSENT)
1028 )
1029 ;
1030
1031/* 6.7.9 */
1032initializer
1033 : assignmentExpression -> ^(SCALAR_INITIALIZER assignmentExpression)
1034 | LCURLY initializerList
1035 ( RCURLY
1036 | COMMA RCURLY
1037 )
1038 -> initializerList
1039 ;
1040
1041/* 6.7.9 */
1042initializerList
1043 : designatedInitializer (COMMA designatedInitializer)*
1044 -> ^(INITIALIZER_LIST designatedInitializer+)
1045 ;
1046
1047designatedInitializer
1048 : initializer
1049 -> ^(DESIGNATED_INITIALIZER ABSENT initializer)
1050 | designation initializer
1051 -> ^(DESIGNATED_INITIALIZER designation initializer)
1052 ;
1053
1054/* 6.7.9 */
1055designation
1056 : designatorList ASSIGN -> ^(DESIGNATION designatorList)
1057 ;
1058
1059/* 6.7.9 */
1060designatorList
1061 : designator+
1062 ;
1063
1064/* 6.7.9 */
1065designator
1066 : LSQUARE constantExpression RSQUARE
1067 -> ^(ARRAY_ELEMENT_DESIGNATOR constantExpression)
1068 | DOT IDENTIFIER
1069 -> ^(FIELD_DESIGNATOR IDENTIFIER)
1070 ;
1071
1072/* 6.7.10 */
1073staticAssertDeclaration
1074 : STATICASSERT LPAREN constantExpression COMMA STRING_LITERAL
1075 RPAREN SEMI
1076 -> ^(STATICASSERT constantExpression STRING_LITERAL)
1077 ;
1078
1079
1080/* ***** A.2.3: Statements ***** */
1081
1082/* 6.8 */
1083statement
1084 : labeledStatement
1085 | compoundStatement
1086 | expressionStatement
1087 | selectionStatement
1088 | iterationStatement
1089 | jumpStatement
1090 | pragma
1091 | assertStatement
1092 | assumeStatement
1093 | waitStatement
1094 | whenStatement
1095 | chooseStatement
1096 ;
1097
1098statementWithScope
1099scope Symbols;
1100@init {
1101 $Symbols::types = new HashSet<String>();
1102 $Symbols::enumerationConstants = new HashSet<String>();
1103 $Symbols::isFunctionDefinition = false;
1104}
1105 : statement
1106 ;
1107
1108/* 6.8.1
1109 * Three possible trees:
1110 *
1111 * Root: IDENTIFIER_LABELED_STATEMENT
1112 * Child 0: IDENTIFIER
1113 * Child 1: statement
1114 *
1115 * Root: CASE_LABELED_STATEMENT
1116 * Child 0: CASE
1117 * Child 1: constantExpression
1118 * Child 2: statement
1119 *
1120 * Root: DEFAULT_LABELED_STATEMENT
1121 * Child 0: DEFAULT
1122 * Child 1: statement
1123 */
1124labeledStatement
1125 : IDENTIFIER COLON statement
1126 -> ^(IDENTIFIER_LABELED_STATEMENT IDENTIFIER statement)
1127 | CASE constantExpression COLON statement
1128 -> ^(CASE_LABELED_STATEMENT CASE constantExpression statement)
1129 | DEFAULT COLON statement
1130 -> ^(DEFAULT_LABELED_STATEMENT DEFAULT statement)
1131 ;
1132
1133/* 6.8.2
1134 * Root: BLOCK
1135 * Child 0: LCURLY (for source information)
1136 * Child 1: blockItemList or ABSENT
1137 * Child 2: RCURLY (for source information)
1138 */
1139compoundStatement
1140scope Symbols;
1141@init {
1142 $Symbols::types = new HashSet<String>();
1143 $Symbols::enumerationConstants = new HashSet<String>();
1144 $Symbols::isFunctionDefinition = false;
1145}
1146 : LCURLY
1147 ( RCURLY
1148 -> ^(COMPOUND_STATEMENT LCURLY ABSENT RCURLY)
1149 | blockItemList RCURLY
1150 -> ^(COMPOUND_STATEMENT LCURLY blockItemList RCURLY)
1151 )
1152 ;
1153
1154/* 6.8.2 */
1155blockItemList
1156 : blockItem+ -> ^(BLOCK_ITEM_LIST blockItem+)
1157 ;
1158
1159/* 6.8.2 */
1160blockItem
1161 : declaration
1162 | statement
1163 | functionDefinition
1164 ;
1165
1166/* 6.8.3
1167 * Root: EXPRESSION_STATEMENT
1168 * Child 0: expression or ABSENT
1169 * Child 1: SEMI (for source information)
1170 */
1171expressionStatement
1172 : expression SEMI -> ^(EXPRESSION_STATEMENT expression SEMI)
1173 | SEMI -> ^(EXPRESSION_STATEMENT ABSENT SEMI)
1174 ;
1175
1176/* 6.8.4
1177 * Two possible trees:
1178 *
1179 * Root: IF
1180 * Child 0: expression
1181 * Child 1: statement (true branch)
1182 * Child 2: statement or ABSENT (false branch)
1183 *
1184 * Root: SWITCH
1185 * Child 0: expression
1186 * Child 1: statement
1187 */
1188selectionStatement
1189scope Symbols;
1190@init {
1191 $Symbols::types = new HashSet<String>();
1192 $Symbols::enumerationConstants = new HashSet<String>();
1193 $Symbols::isFunctionDefinition = false;
1194}
1195 : IF LPAREN expression RPAREN s1=statementWithScope
1196 ( (ELSE)=> ELSE s2=statementWithScope
1197 -> ^(IF expression $s1 $s2)
1198 | -> ^(IF expression $s1 ABSENT)
1199 )
1200 | SWITCH LPAREN expression RPAREN s=statementWithScope
1201 -> ^(SWITCH expression $s)
1202 ;
1203
1204/* 6.8.5
1205 * Three possible trees:
1206 *
1207 * Root: WHILE
1208 * Child 0: expression
1209 * Child 1: statement
1210 *
1211 * Root: DO
1212 * Child 0: statement
1213 * Child 1: expression
1214 *
1215 * Root: FOR
1216 * Child 0: clause-1: declaration, expression, or ABSENT
1217 * (for loop initializer)
1218 * Child 1: expression or ABSENT (condition)
1219 * Child 2: expression or ABSENT (incrementer)
1220 * Child 3: statement (body)
1221 *
1222 */
1223iterationStatement
1224scope Symbols;
1225@init {
1226 $Symbols::types = new HashSet<String>();
1227 $Symbols::enumerationConstants = new HashSet<String>();
1228 $Symbols::isFunctionDefinition = false;
1229}
1230 : WHILE LPAREN expression RPAREN invariant_opt
1231 s=statementWithScope
1232 -> ^(WHILE expression $s invariant_opt)
1233 | DO s=statementWithScope WHILE LPAREN expression RPAREN
1234 invariant_opt SEMI
1235 -> ^(DO $s expression invariant_opt)
1236 | FOR LPAREN
1237 ( d=declaration e1=expression_opt SEMI e2=expression_opt
1238 RPAREN i=invariant_opt s=statementWithScope
1239 -> ^(FOR $d $e1 $e2 $s $i)
1240 | e0=expression_opt SEMI e1=expression_opt SEMI
1241 e2=expression_opt RPAREN s=statementWithScope
1242 -> ^(FOR $e0 $e1 $e2 $s $i)
1243 )
1244 ;
1245
1246expression_opt
1247 : expression
1248 | -> ABSENT
1249 ;
1250
1251invariant_opt
1252 : -> ABSENT
1253 | INVARIANT LPAREN expression RPAREN
1254 -> ^(INVARIANT expression)
1255 ;
1256
1257
1258/* 6.8.6
1259 * Four possible trees:
1260 *
1261 * Root: GOTO
1262 * Child 0: IDENTIFIER
1263 * Child 1: SEMI (for source information)
1264 *
1265 * Root: CONTINUE
1266 * Child 0: SEMI (for source information)
1267 *
1268 * Root: BREAK
1269 * Child 0: SEMI (for source information)
1270 *
1271 * Root: RETURN
1272 * Child 0: expression or ABSENT
1273 * Child 1: SEMI (for source information)
1274 */
1275jumpStatement
1276 : GOTO IDENTIFIER SEMI -> ^(GOTO IDENTIFIER SEMI)
1277 | CONTINUE SEMI -> ^(CONTINUE SEMI)
1278 | BREAK SEMI -> ^(BREAK SEMI)
1279 | RETURN expression_opt SEMI -> ^(RETURN expression_opt SEMI)
1280 ;
1281
1282/*
1283 * Root: PRAGMA
1284 * child 0: IDENTIFIER (first token following # pragma)
1285 * child 1: TOKEN_LIST (chilren are list of tokens following identifier)
1286 * child 2: NEWLINE (character which ends the pragma)
1287 */
1288pragma : PRAGMA IDENTIFIER pragmaBody NEWLINE
1289 -> ^(PRAGMA IDENTIFIER ^(TOKEN_LIST pragmaBody) NEWLINE)
1290 ;
1291
1292pragmaBody
1293 : (~ NEWLINE)*
1294 ;
1295
1296assertStatement
1297 : ASSERT expression SEMI -> ^(ASSERT expression)
1298 ;
1299
1300assumeStatement
1301 : ASSUME expression SEMI -> ^(ASSUME expression)
1302 ;
1303
1304waitStatement
1305 : WAIT expression SEMI -> ^(WAIT expression)
1306 ;
1307
1308whenStatement
1309 : WHEN LPAREN expression RPAREN statement
1310 -> ^(WHEN expression statement)
1311 ;
1312
1313chooseStatement
1314 : CHOOSE LCURLY statement+ RCURLY
1315 -> ^(CHOOSE statement+)
1316 ;
1317
1318/* ***** A.2.4: External Definitions ***** */
1319
1320/* 6.9
1321 * Root: TRANSLATION_UNIT
1322 * Children: externalDeclaration
1323 */
1324translationUnit
1325scope Symbols; // the global scope
1326scope DeclarationScope; // just to have an outermost one with isTypedef false
1327@init {
1328 $Symbols::types = new HashSet<String>();
1329 $Symbols::enumerationConstants = new HashSet<String>();
1330 $Symbols::isFunctionDefinition = false;
1331 $DeclarationScope::isTypedef = false;
1332}
1333 : externalDeclaration* EOF
1334 -> ^(TRANSLATION_UNIT externalDeclaration*)
1335 ;
1336
1337/* 6.9
1338 * Need to look ahead to distinguish function definition from
1339 * a declaration. As soon as you see the "{", you know you
1340 * are in a function definition.
1341 */
1342externalDeclaration
1343 : (declarationSpecifiers declarator declarationList_opt LCURLY)=>
1344 functionDefinition
1345 | declaration
1346 | pragma
1347 ;
1348
1349/* 6.9.1 */
1350functionDefinition
1351scope Symbols; // "function scope"
1352@init {
1353 $Symbols::types = new HashSet<String>();
1354 $Symbols::enumerationConstants = new HashSet<String>();
1355 $Symbols::isFunctionDefinition = true;
1356}
1357 : declarationSpecifiers declarator
1358 declarationList_opt compoundStatement
1359 -> ^(FUNCTION_DEFINITION declarationSpecifiers declarator
1360 declarationList_opt compoundStatement)
1361 ;
1362
1363
1364/* 6.9.1
1365 * Root: DECLARATION_LIST
1366 * Children: declaration (any number)
1367 */
1368declarationList_opt
1369 : declaration* -> ^(DECLARATION_LIST declaration*)
1370 ;
Note: See TracBrowser for help on using the repository browser.