[antlr-interest] getting data from a grammar

Brian Nelson brian at neuesoftware.com
Mon May 4 13:42:50 PDT 2009


Thanks, Jim. That gets me pretty far... I think you gave me the initial 
CSS grammar I am working on, so you'd probably understand this more 
complex example:

given

foo>bar .foo as the selector, I want an array of ['foo>bar', '.foo']...

selector
     : elementName (elementSubsequent? elementName)*
     ;

Yeah, good times. Thanks for all the help, by the way!

On 5/4/09 1:02 PM, Jim Idle wrote:
> Brian Nelson wrote:
>> So I have been working on this for about a week, and I even bought 
>> the ANTLR book, but I can't seem to find how to get what seems like 
>> simple data out of the grammar.
>>
>> Here's a very, very limited example. I am merely trying to get data 
>> out of CSS files. I guess the questions I have are:
>> 1) Should I be using an AST? I assumes I would, since CSS can br 
>> broken down to a tree very simply, but upon further reading, it may 
>> not actually be beneficial since I am not trying to solve a really 
>> complex language issue...
>> 2) How would one, in speudo code, get the actual data out of the 
>> following:
>>
>> The input would be "aa, bb, cc" and I simply want a list of 
>> ['aa','bb','cc']. I am not trying to process it, just get the list 
>> out of it, ignoring the commas, in a variable I can use.
>>
>>     prog : rone (COMMA rone)*
>>           ;
>>
>>     rone : IDENT;
>>
> You probably do not need a tree if you are just extracting information 
> and you don't need to refer to other parts of the css. From your 
> example above, then  you probably just want the text of your rone 
> rule, which is trivial:
>
> prog
>    : r1=rone   { System.out.println($r1.text); }
>
>         (COMMA r2=rone  { System.out.println($r2.text); })*
> ;
>
> Obviously, though I use sout here, you can do anything with the String 
> that $rn.text gives you.
>
> Jim
> ------------------------------------------------------------------------
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>    

-- 
Brian Nelson
Software Engineer, Neue Software

e. brian at neuesoftware.com
c. +1 408 373 8310

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090504/3f02fbe4/attachment.html 


More information about the antlr-interest mailing list