[antlr-interest] Can't express a obvious thing in ANTLR tree rewriting syntax
John B. Brodie
jbb at acm.org
Mon Dec 5 13:50:40 PST 2011
Greetings!
On 12/05/2011 01:17 PM, Stefan Mätje wrote:
> Hi,
>
> I unfortunately can't express what I want in ANTLR syntax. It should be
> obvious but I
> can't see the solution at the moment. I want to build an AST while parsing
> generating
> a VAR_DCL imaginary node for a variable declaration and an ARRAY_DCL imaginary
> node
> for an array declaration.
>
> Here are some example declarations and the expected AST:
> DCL var FIXED; -> (VAR_DCL FIXED MOD_LIST var)
> DCL array (10) FIXED; -> (ARRAY_DCL ^(DIM_LIST BOUND 1 10) FIXED MOD_LIST
> array)
>
> The language itself is not ambigious at this point of the grammar so it should
> be possible
> to simply write the solution down without using syntactic or semantic
> predicates. But I
> don't see how to solve it in the rule "dclSet". Possible semantic predicate
> marked with
> "???CONDITION???".
>
> I have the following parts of a lexer/parser grammar:
>
> /** "VariableDeclaration"
> */
> var_dcl:
> KW_DCL! // "DCL"
> dclSet (',' dclSet)*
> ';'!
> ;
>
> /** "DeclareSentence"
> */
> dclSet:
> ids=oneIdOrList dimAttr? i='INV'? simpleType globalAttr? initAttr?
> -> {???CONDITION???}? ^(ARRAY_DCL $dimAttr simpleType
> ^(MOD_LIST $i? globalAttr?) $ids+)
> -> ^(VAR_DCL simpleType ^(MOD_LIST $i?
> globalAttr?) $ids+)
> ;
>
> /** "OneIdentifierOrList"
> */
> oneIdOrList:
> ( ID | '(' ID (',' ID )* ')' ) -> ID+
> ;
>
> /** "DimensionAttribute"
> The dimension boundaries for an array.
> */
> dimAttr:
> LPAREN dimBound ( ',' dimBound )* RPAREN -> ^(DIM_LIST
> dimBound+)
> ;
>
> /** "DimensionBoundaries"
> BOUND carries always two expressions for the LWB and UPB. If the LWB
> is not specified it is substituted as FIX_LIT with value 1. This is
> an "integer".
> */
> dimBound
> : lwb=constFixExpr COLON upb=constFixExpr -> BOUND[$lwb.start]
> $lwb $upb
> | upb=constFixExpr -> BOUND[$upb.start]
> FIX_LIT[$upb.start,"1"] PRSZ["15"] $upb
> ;
>
just a simple flag...
dclSet @init{boolean array=false;} :
ids=oneIdOrList (dimAttr{array=true;})? i='INV'? simpleType
globalAttr? initAttr?
-> {array}? ^(ARRAY_DCL dimAttr simpleType ^(MOD_LIST $i?
globalAttr?) $ids+)
-> ^(VAR_DCL simpleType ^(MOD_LIST $i? globalAttr?) $ids+)
;
(BTW, did you mean to leave initAttr out of the tree?)
hope this helps
-jbb
More information about the antlr-interest
mailing list