[antlr-interest] AST build with input tokens out of order

Robert Jarzmik robert.jarzmik at free.fr
Sat Aug 20 05:40:08 PDT 2011


Hi Bart,

Could I abuse a bit more of your time ?

My initial ordering problem came back because of the order of my rules.

I have this input : STRUCT myvar1, myvar2 ( INTEGER i1; INTEGER j1; )

The tree I'd like to have is :
 #(DECL_VARIABLE 'myVar1' 
   #(STRUCT
     #(DECL_VARIABLE 'i1' INTEGER)
     #(DECL_VARIABLE 'j1' INTEGER)
    )
   )
 #(DECL_VARIABLE 'myVar2' 
   #(STRUCT
     #(DECL_VARIABLE 'i1' INTEGER)
     #(DECL_VARIABLE 'j1' INTEGER)
    )
   )

I have a rule 'structure_members which rewrites '( INTEGER i1; INTEGER j1;
)' into #(STRUCT #(DECL_VARIABLE 'i1' INTEGER) #(DECL_VARIABLE 'j1' INTEGER)).

My problem is the main declaration rule :
declaration
    : STRUCT identifiers[$structure_members.tree] structure_members
      -> ^(DECL_VARIABLE identifiers+ structure_members)

The 'structure_members' are only available after identifiers are parsed, and I
cannot give identifiers the parameter $structure_members.tree as I wished (I get
a forward reference error).

Is there a way to pass the structure_members tree to identifiers rule ?

Cheers.

--

Robert

PS: All the input data I'm using:
  => INPUT = "INTEGER good; STRUCT myvar1, myvar2 ( INTEGER i1; INTEGER j1; )"
  => The example grammar to demonstrate the issue
/******************************************************************************
 * LTR_ex4.g
 ******************************************************************************/
grammar LTR_ex4;

options {
  k=1;
  output=AST;
  ASTLabelType=CommonTree;
}

tokens {
  DIMS; DECL_VARIABLE;
}

translation_unit
	: declaration (';'! declaration)*
	;
    
declaration
	: type_identifier identifiers[$type_identifier.tree]
	-> ^(DECL_VARIABLE identifiers)+
	| STRUCT identifiers[$structure_members.tree] '(' structure_members ')'
	-> ^(DECL_VARIABLE identifiers)
	;

structure_members
	: (declaration ';')+
	-> ^(STRUCT declaration+)
	;
    
identifiers[CommonTree type]
	: IDENTIFIER (',' IDENTIFIER)*
	-> ({type} IDENTIFIER)+
	;

type_identifier
    	:	 'INTEGER'
	;

STRUCT 	:	'STRUCT' ;
IDENTIFIER  :('a'..'z' | '0'..'9')+ ;
WS  :   ( ' ' | '\t'| '\r'| '\n') {$channel=HIDDEN;};


More information about the antlr-interest mailing list