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

Bart Kiers bkiers at gmail.com
Wed Aug 17 13:28:43 PDT 2011


Hi Robert,

AFAIK, you'll need to pass the 'type' of your array down your tree as a
parameter.

A little demo:

grammar LTR_ex1;

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

tokens {
  DIMS;
}

declaration
  :  (type_specifier ARRAY)=> type_specifier ARRAY
identifiers[$type_specifier.tree]
                              -> ^(ARRAY identifiers)+
  ;

identifiers[CommonTree type]
  :  array_or_single_identifier[type] (','
array_or_single_identifier[type])*
     -> array_or_single_identifier+
  ;

array_or_single_identifier[CommonTree type]
  :  (IDENTIFIER '[')=> array[type]
  |                     IDENTIFIER
  ;

array[CommonTree type]
  :  IDENTIFIER '[' constant (',' constant)* ']'
     -> ^(IDENTIFIER ^( DIMS constant+) {new CommonTree(type)})
  ;

constant
  :  DECIMAL_LITERAL
  |  IDENTIFIER
  ;

type_specifier
  :  'LOGICAL'
  |  'INTEGER'
  |  'BOOLEAN'
  ;

ARRAY           : 'ARRAY' ;
DECIMAL_LITERAL : ('0' | '1'..'9' '0'..'9'*);
IDENTIFIER      : LETTER (LETTER|'0'..'9'|'#')*;
SPACE           : ' ' {skip();};
fragment LETTER : 'A'..'Z' | 'a'..'z' | '_';


Regards,

Bart.

On Wed, Aug 17, 2011 at 9:02 PM, Robert Jarzmik <robert.jarzmik at free.fr>wrote:

> 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
>


More information about the antlr-interest mailing list