[antlr-interest] Can't express a obvious thing in ANTLR tree rewriting syntax

Stefan Mätje Stefan.Maetje at esd-electronics.com
Tue Dec 6 09:07:04 PST 2011


Thanks for the input.

I changed the dclSet rule now to this which works:

dclSet:
     ids=oneIdOrList dimAttr? i='INV'? simpleType globalAttr? initAttr?
         -> {null!=$dimAttr.tree}?    ^(ARRAY_DCL dimAttr simpleType  
^(MOD_LIST $i? globalAttr?) $ids+ initAttr?)
         ->                           ^(VAR_DCL simpleType ^(MOD_LIST $i?  
globalAttr?) $ids+ initAttr?)
	;

It was not clear to me if I must use a semantic predicate or if there would be  
a pure syntactic or
grammatic solution. Now I test if the dimAttr rule returns a tree then it must  
be an array declaration.

Greetings,
	Stefan

Btw.:	You were right, I don't want to leave the initAttr out of the tree but  
I have been stuck
	at the ARRAY_DCL / VAR_DCL issue.


Am 05.12.2011 22:50:40 schrieb(en) John B. Brodie:
> 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