[antlr-interest] parsing rules

George Soom george.soom at siria.cc
Thu May 27 03:51:32 PDT 2010


Thank You,

now I understand my mistake. However, can You give me a bit bigger example?
All relevant tutorials seem to be for ANTLR v2
 (
http://www.antlr.org/wiki/display/ST/Language+Translation+Using+ANTLR+and+StringTemplate
) 
and syntax seems to bee changed more than I can understand.

Georg

Jim Idle <jimi at temporal-wave.com> wrote on 26 May 2010, 06:43 PM:
Subject: Re: [antlr-interest] parsing rules
>Your s: rule needs to set $st to something too or it will return an empty
>template :-)
>Jim
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of George Soom
>> Sent: Wednesday, May 26, 2010 4:01 AM
>> To: antlr-interest at antlr.org
>> Subject: [antlr-interest] parsing rules
>> 
>> Hi,
>> 
>> I have to rewrite several files with mathematical text in them. Text
>> substitutions are really simple (e.g. 'a=4' -> 'a:=4;' and similar).
>> Input is in form:
>> ----
>> a=4
>> b=3
>> draw(a)
>> draw(b)
>> ----
>> 
>> Needed output
>> -----
>> a:=4;
>> b:=3;
>> draw(a, "constant arguments")
>> draw(b, "constant arguments")
>> ----
>> 
>> The best option I have found is to use StringTemplate. At chapter 9.3
>> in
>> book 'The Definitive ANTLRD reference' has a suitable example.
>> To explain my probelm I will add lines from two examples:
>> Grammar:
>> 
>> grammar T;
>> options {output=template;}
>> s : ID '=' INT ';' -> assign(x={$ID.text},y={$INT.text}) ;
>> ---lexer part not pasted---
>> 
>> Test.java
>> --header, input, parser-lexer generation etc. not pasted---
>> parser.setTemplateLib(templates); // give parser templates
>> TParser.s_return r = parser.s();      // parse rule s
>> StringTemplate output = r.getTemplate();
>> System.out.println(output.toString());// emit translation
>> 
>> This will work fine. But if I change rule s to
>> s: assign+;
>> assign: ID '=' INT ';' -> assign(x={$ID.text},y={$INT.text}) ;
>> 
>> I have to call parse.assign() to get correct results. How can i still
>> parse
>> starting from rule 's' and get desired output?
>> 
>> George
>> 
>> 
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
>
>
>
>
>List: http://www.antlr.org/mailman/listinfo/antlr-interest
>Unsubscribe:
>http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>



More information about the antlr-interest mailing list