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'
        |       '_'
        ;

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

-- 
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to