I'm trying to write a grammar capable of parsing something like the
following:
$sum = number_1 * number_2 / (number_3 + $temp);

>From this, I want to generate this AST:
(ASSIGN
    $sum
    (MUL_OP
         number_1
         (DIV_OP
             number_2
             (ADD_OP
                 number_3
                 $temp))))

This is (the relevant part of) what I currently have -

statement : assignment_expression ';'!;

assignment_expression : variable '=' expression -> ^(ASSIGN variable
expression);

expression
: mul_expression (add_operator mul_expression)? -> ^(add_operator
mul_expression mul_expression?);

mul_expression : term (mul_operator term)* -> ^(mul_operator term term*);

term : '('! expression ')'! | variable | constant;

add_operator : '+' -> ADD_OP | '-' -> SUB_OP;
mul_operator : '*' -> MUL_OP | '/' -> DIV_OP;

It's obviously very wrong. :) The main things I can't figure out are how to
have chained operations (eg. a * b / c) that create new child nodes for each
new operator (eg. (MUL a (DIV b c))), or how to properly support
parenthesis. All my attempts have given me a grammar with mutual
left-recursion that I can't seem to solve. There must be a clean and clear
way to do this. :)

Thanks in advance for any help, and sorry for such a noobie question.
_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org/mailman/listinfo/antlr-dev

Reply via email to