I’ve already a grammar definition in ANTLR for the very simple expressions like (a + b) * c:
grammar SimpleCalc;
options {
language=CSharp2;
output=AST;
}
tokens {
PLUS = '+' ;
MINUS = '-' ;
MULT = '*' ;
DIV = '/' ;
}
/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
ID : ('a'..'z' | 'A' .. 'Z' | '0' .. '9')+ ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ { Skip(); } ;
/*------------------------------------------------------------------
* PARSER RULES
*------------------------------------------------------------------*/
expr : multExpr ((PLUS | MINUS)^ multExpr)*;
multExpr : atom ((MULT | DIV)^ atom )*;
atom : ID
| '(' expr ')' -> expr;
Now I have some pre-defined type for creating tree structure:
public class Expr { }
public class SimpleExpr : Expr
{
public SimpleExpr(string name) { ... }
}
public enum BinaryExprType
{
Plus,
Minus,
Multiply,
Divide
}
public class BinaryExpr : Expr
{
public BinaryExpr(Expr left, BinaryExprType op, Expr right) { ... }
}
I know we can write some code to convert a CommonTree to Expr tree, but I’d like to rewrite directly to the custom types like:
atom returns [Expr e]
: ID { $e = new SimpleExpr($ID.text); }
| '(' expr ')' { $e = $expr.e; };
expr returns [Expr e]
: /* ??? */;
multExpr returns [Expr e]
: /* ??? */;
But how should I do for expr and multExpr to create BinaryExpr with proper BinaryExprType?
Try something like this (untested!):
The trick is to recursively use the rule itself in the binary expressions: the
$expr.eand$multExpr.einnew BinaryExpr(...).