Greetings!

First 2 caveats then see discussion below:

1) I do not know C#. all of my suggestions are based on the Java target.

2) My suggestions are from memory. I have not actually tested them (or
rather I have tested them in the past on other problems and hope memory
works).


On 11/15/2011 07:43 PM, Voelkel, Andy wrote:
> Hi,
> 
> I keep running into questions like this, and don't know how to answer them. 
> Could anyone look at this simple example and tell me how I would accomplish 
> my goal? I think I've got a central concept mixed up in my head.
> 
> What I'm trying to do is under the "array" rule below, where I'm trying to 
> create a rewrite rule which collects elements which are within a grouping 
> construct. This seems to come up a lot for me, and I don't get how to do it.
> 
> While we're at it, what is the easiest way to strip the " " characters from 
> the strings?

use whatever sub-string functionality is available in C# to remove the
first and last characters from the string. but do so at the last
possible moment.

> 
> Thanks for any help! Here's the grammar excerpt:
> 
> tokens { ARRAY; }
> 
> @header { using System; }
> 
> public prog:   ( stat {Console.WriteLine(
>                 $stat.tree==null?"null":$stat.tree.ToStringTree());} )+ ;
> 
> stat:   assign | block | ';'! ;
> block:  ID^ ID '{'! (assign)+ '}'! ;
> assign: ID '='^ (atom | array) ';'! ;
> atom:   FLOAT | ID | STRING ;
> array:  '[' ((FLOAT)+ | (STRING)+) ']' -> ^(ARRAY _FLOATS_OR_STRINGS_ ) ;

generally, any time you have a cardinality meta-operator (the ? * or +)
in the syntax portion of a rule, you will want a similar cardinality
operator in the re-write portion of that rule.

thus a first try at a re-write rule is:

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

the separate lists on the right of the -> work because your syntax
specifies separate lists.

and i guess this first try really reduces to this rule:

array : '['^ FLOAT+ | STRING+ ']'! ;



note that you can use the += operator to accumulate lists of things. but
these lists are homogeneous (e.g. all of the elements in the list must
be of the same type).

and in this specific case both of your lists are lists of Tokens, so a
second try at a re-write rule is:

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


to me both of the above are not good choices because information that
the parser immediately has about the type of the elements in the array
is not explicitly placed into the tree.

so i would have 2 imaginary tokens ARRAY_FLOAT and ARRAY_STRING and use
this rule:

array
    : ( l='[' (f+=FLOAT)+  ']' -> ^(ARRAY_FLOAT ["FLT ARY",$l] $f+) )
    | ( l='[' (s+=STRING)+ ']' -> ^(ARRAY_STRING["STR ARY",$l] $s+) )
  ;

i think this last form will simplify subsequent processing of the tree.
note also the proper initialization of the imaginary tokens.

> 
> fragment LETTER : '$'| 'A'..'Z' | 'a'..'z' | '_' ;
> fragment DIGITS : ('0'..'9')+;
> fragment EXP : ('e'|'E') ('+'|'-')? ('0'..'9')+ ;
> STRING : '"' ( ~('"') )* '"' ;
> ID  :   LETTER (LETTER|DIGITS)*;
> FLOAT :  (DIGITS? '.' DIGITS EXP?) | (DIGITS EXP?) ;
> WS  :   (' '|'\t'|'\n'|'\r')+ {Skip();} ;
> 

Hope this helps...
   -jbb


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 il-antlr-inter...@googlegroups.com.
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en.

Reply via email to