See replies below starting with [Andy - ... - Andy
> 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. > > > 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. [Andy - this approach doesn't work - I get exceptions thrown. I haven't debugged that yet.] and i guess this first try really reduces to this rule: array : '['^ FLOAT+ | STRING+ ']'! ; [Andy - Not really. I did that first and it works. What I was trying to do in my example Was use a new token type called ARRAY as the node type, not '[', which seems ugly] 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+) ; [Andy - that doesn't work either. I don't get exceptions, but I get errors and non-sensical output] 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+) ) ; [Andy - This causes compiler errors, and I'm really not sure what you are getting at. But I can adapt your basic idea to this, which is simpler and works: array : ( '[' (FLOAT)+ ']' -> ^(ARRAY_FLOAT FLOAT+) ) | ( '[' (STRING)+ ']' -> ^(ARRAY_STRING STRING+) ) It is similar to an example in Terence's book. It isn't really what I wanted to do. Maybe it is a better idea, I'll need to think about that. But it still doesn't answer the question as to how to get the result I originally wanted. I'm still curious about that.] 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"] ________________________________ 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 priv...@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. 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.