[antlr-interest] AST build with input tokens out of order
Robert Jarzmik
robert.jarzmik at free.fr
Wed Aug 17 12:02:25 PDT 2011
Hello,
I have a little problem to build an AST for my specific grammar. As I'm an ANTLR
beginner please be patient with me ...
I narrowed down the problem to a simple usecase.
The input : INTEGER ARRAY P1[10], P2[40,MAX_NB_CLOUDS]
The wished output (in lisp notation) :
#( #('P1' ARRAY #( #('DIMS' 10) INTEGER)) #('P2' ARRAY #( #('DIMS' 40 MAX_NB_CLOUDS) INTEGER)) )
My main problem is that the type (INTEGER) is before the ARRAY token, and I
cannot think of a way to write the rewrite rules. I provided at the end of the
mail the extract of my grammar [1] narrowed down to my specific problem.
Can anyone figure out how to properly write the rules to have my wished output
please ?
Cheers.
--
Robert
[1] LTR_ex1.g
grammar LTR_ex1;
options {
k=1;
output=AST;
}
tokens {
DIMS;
}
declaration
: (type_specifier ARRAY) => type_specifier ARRAY identifiers
-> ^(ARRAY identifiers type_specifier)+
;
identifiers
: array_or_single_identifier ( ',' array_or_single_identifier )*
-> array_or_single_identifier+
;
array_or_single_identifier
: (IDENTIFIER '[') => array
| IDENTIFIER
;
array
: IDENTIFIER '[' constant ( ',' constant )* ']'
-> IDENTIFIER ^( DIMS constant+ )
;
constant
: DECIMAL_LITERAL
| IDENTIFIER
;
type_specifier
: 'LOGICAL'
| 'INTEGER'
| 'BOOLEAN'
;
//////////////////////////////////////////////////////////////////////
// LEXER definitions //
//////////////////////////////////////////////////////////////////////
ARRAY : 'ARRAY' ;
DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*) ;
IDENTIFIER
: LETTER (LETTER|'0'..'9'|'#')*
;
fragment
LETTER
: 'A'..'Z'
| 'a'..'z'
| '_'
;
More information about the antlr-interest
mailing list