[antlr-interest] AST grammar

Jason Jones louddwarf at gmail.com
Fri Apr 20 03:22:16 PDT 2012


Hi antlr-interest,

I have the following grammar for building an AST, it's a slight adaption of
the Expr.g grammar from the Antlr Reference book:

grammar obfParenthese;

options {
output=AST;
ASTLabelType=CommonTree;
}

statement      : '(' expr symbol^ expr ')';    //first expr token will
match everything....
expr : mult (('+'^ | '-'^) mult)* ;
mult : atom (('*'^ | '/'^) atom)* ;
atom : NUMBER ;
symbol : SYMBOL;

SYMBOL : '+' | '-' | '*' | '/' ;

NUMBER : '0' .. '9'+ ;

And as you might be able to tell from the grammar I want to build an AST
that will take into consideration parenthese and change accordingly. Now it
is obvious to me that the above grammar won't work (as the first token expr
will consume everything) but I'm wondering if there's a way to do this
without using syntactic or semantic predicates and if not how would I do it
using them?

Please be kind as I'm new to Antlr and would very much appreciate it if
you'd explain carefully how it works. Even just a general direction to go
in would be appreciated.

Thanks, Jason.


More information about the antlr-interest mailing list