4

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.

Community
  • 1
  • 1
marka.thore
  • 2,795
  • 2
  • 20
  • 35

1 Answers1

2

You have a semantic predicate (syntax {...}?) which contains a reference to a value which is changed by an action (syntax {...}). In your case, the value is the fields Evaluation.matched and Evaluation.done.

You should avoid this situation altogether - never¹ include a predicate that depends on an action executing. Instead, check these values in the actions by wrapping the action code in if (eval.matched && !eval.done) { ... }

¹ There are some people who write grammars that interact in this way, but I strictly avoid it due to the potential for problems like you are seeing and others that are even worse.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Ok, so what do you suggest me to do in my case? I'm looking for a way to execute a branch (if the condition is true) or skip it entirely. That's the reason why I chose to use the Semantic Predicate, because if the conditions is false, I could skip the subtree with `^(CODEBLOCK .*)`, that consumes all tokens. My language has a lot of different instruction, I have to replicate this check `if (eval.matched && !eval.done) ` everywhere ? – marka.thore Mar 14 '13 at 19:02