[antlr-interest] parsing rules

Jim Idle jimi at temporal-wave.com
Wed May 26 08:43:53 PDT 2010


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





More information about the antlr-interest mailing list