[antlr-interest] parsing rules

George Soom george.soom at siria.cc
Wed May 26 04:01:18 PDT 2010


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



More information about the antlr-interest mailing list