[antlr-interest] BASIC if-then-else-endif

Juan Fernando Herrera J. juanfhj at gmail.com
Wed Oct 20 13:42:44 PDT 2010

I'm trying to build an AST for if-then-else-endif like in BASIC. I'm trying

    prog:   ( ast {System.out.println($ast.tree.toStringTree());} )+ EOF;

    ast:   'if' condition a=thenstat b=elsestat 'endif' -> ^('if'
condition $a $b);

    thenstat: 'then'^ stat+;

    elsestat: 'else'^ stat+;

    stat:  'print'^ INT;

This gets me

    (if true (then (print 1) (print 2) (print 3)) (else (print 4)
(print 5) (print 6)))

However, I'd like to get

    (if true ((print 1) (print 2) (print 3)) ((print 4) (print 5) (print 6)))

How can I do it?


More information about the antlr-interest mailing list