[antlr-interest] This should be easy - but I can't figure it out

John B. Brodie jbb at acm.org
Wed Nov 16 17:39:05 PST 2011


On 11/16/2011 12:02 AM, Voelkel, Andy wrote:
> 
> array: '[' FLOAT+ | STRING+ ']' -> ^(ARRAY FLOAT+ STRING+) ;
> 
> the separate lists on the right of the -> work because your syntax
> specifies separate lists.
> 
> [Andy - this approach doesn't work - I get exceptions thrown. I haven't debugged that yet.]

yes. sorry. I got the cardinality on the right of the -> wrong. it
should be (i think):

array: '[' FLOAT+ | STRING+ ']' -> ^(ARRAY FLOAT* STRING* ) ;

because when specifying an array of floats, the list of FLOATs is full
the list of STRINGs is empty and so we must have STRING* on the right
hand side. similar reasoning for FLOAT*.

> 
> array: '[' (t+=FLOAT)+ | (t+=STRING)+ ']' -> ^(ARRAY $t+) ;
> 
> [Andy - that doesn't work either. I don't get exceptions, but I get errors and non-sensical output]

not enough information here for me to be of any help to you, sorry. as
far as i recall this works in the Java target, maybe something is
different with the C# target. or more likely i have mis-remembered it...

> 
> array
>     : ( l='[' (f+=FLOAT)+  ']' -> ^(ARRAY_FLOAT ["FLT ARY",$l] $f+) )
>     | ( l='[' (s+=STRING)+ ']' -> ^(ARRAY_STRING["STR ARY",$l] $s+) )
>   ;
> 
> [Andy - This causes compiler errors, and I'm really not sure what you are getting at.

compiler errors are odd here, maybe the C# target does not support the [
] notation for initializing an imaginary token?

> i think this last form will simplify subsequent processing of the tree.
> note also the proper initialization of the imaginary tokens.
> 
> [Andy - what do you mean "proper initialization of the imaginary tokens"]

the stuff between the [ ] on the right hand side of the -> is
information used to initialize the imaginary token. in Java, they get
translated into parameters to its constructor. i refer you to Dr. Parr's
book to find out more about this feature.



seems like i probably wasn't very helpful to you after all, sorry about
that...
   -jbb



More information about the antlr-interest mailing list