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

Voelkel, Andy andy.voelkel at plantronics.com
Tue Nov 15 16:43:54 PST 2011


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?

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

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();} ;


- Andy

________________________________

CONFIDENTIALITY NOTICE: This e-mail transmission, and any documents, files or previous e-mail messages attached to it, may contain information that is confidential and/or legally privileged. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, please DO NOT disclose the contents to another person, store or copy the information in any medium, or use any of the information contained in or attached to this transmission for any purpose. If you have received this transmission in error, please immediately notify the sender by reply email or at privacy at plantronics.com, and destroy the original transmission and its attachments without reading or saving in any manner.

For further information about Plantronics - the Company, its products, brands, partners, please visit our website www.plantronics.com.


More information about the antlr-interest mailing list