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

Stefan Mätje Stefan.Maetje at esd-electronics.com
Mon Dec 5 10:17:25 PST 2011


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
	;


Any help appreciated,
	Stefan Mätje


More information about the antlr-interest mailing list