Starting from this grammar: https://stackoverflow.com/a/14287002/1082002 I would realize a simple grammar that accepts and evaluates simple language like this:
{
if a==c {
a
if a==b {
b
} else {
c
}
}
}
So, if a==c
, it executes a
and evaluates if a==b
, if it's true, it executes b
otherwise c
. Really simple.
The parser grammar and the tree grammar are these:
TreeEvaluator.g (combined grammar to produce an AST)
grammar TreeEvaluator;
options {
output = AST;
}
tokens {
CONDBLOCK;
CODEBLOCK;
DEFAULT;
}
compilationUnit : block EOF -> block;
condition : cif elif* celse? -> ^(IF cif elif* celse?);
cif : IF expr block -> ^(CONDBLOCK expr block);
elif : ELIF expr block -> ^(CONDBLOCK expr block);
celse : ELSE block -> ^(DEFAULT block);
expr : ID EQ^ ID;
block : LCUR instruction* RCUR -> ^(CODEBLOCK instruction*);
instruction : ID | condition;
IF : 'if';
ELIF: 'elif';
ELSE: 'else';
LCUR: '{';
RCUR: '}';
EQ : '==';
ID : ('a'..'z'|'A'..'Z')+;
WS : (' '|'\t'|'\f'|'\r'|'\n')+ {skip();};
AstTreeEvaluatorParser.g (tree parser)
tree grammar AstTreeEvaluatorParser;
options {
output = AST;
tokenVocab = TreeEvaluator;
ASTLabelType = CommonTree;
}
@members {
private static final class Evaluation {
boolean matched = false;
boolean done = false;
}
private java.util.HashMap<String, Integer> vars = new java.util.HashMap<String, Integer>();
public void addVar(String name, int value){
vars.put(name, value);
}
}
compilationUnit : block+;
block : ^(CODEBLOCK instruction*);
instruction : ifStat | ID;
ifStat
@init { Evaluation eval = new Evaluation(); }
: ^(IF condition[eval]* defcond[eval]?)
;
condition [Evaluation eval]
: ^(CONDBLOCK exp {if ($exp.value) eval.matched = true;} evalblock[eval])
;
defcond [Evaluation eval]
: ^(DEFAULT {eval.matched = true;} evalblock[eval]) //force a match
;
evalblock [Evaluation eval]
: {eval.matched && !eval.done}? //Only do this when a condition is matched but not finished
block //call the execution code
{eval.done = true;} //evaluation is complete.
| ^(CODEBLOCK .*) //read the code node and continue without executing
;
exp returns [boolean value]
: ^(EQ lhs=ID rhs=ID)
{$value = vars.get($lhs.getText()) == vars.get($rhs.getText());}
;
The problem is the DFA generated to predict the rule evalblock
, this DFA has a method SpecialStateTransition()
that refers to the parameter eval
(as specified in the rule), but in generated Java class, that parameter is not visible.
I don't understand why, and if there is a way to avoid this problem.