There are 3 issues:
{$value ||= $b.value;}
should be{$value = $value || $b.value;}
{$value &&= $b.value;}
should be{$value = $value && $b.value;}
- the label
$term
in the fourth alternative of theterm
rule is ambiguous: is could refer to the rule itself, or theterm
followed by'¬'
I.e., the following:
term returns [boolean value]
: ...
| '¬' term {$value = !$term.value;}
;
should be:
term returns [boolean value]
: ...
| '¬' t=term {$value = !$t.value;}
;
Having made those changes, the following test class:
import org.antlr.runtime.*;
public class Main {
public static void main(String[] args) throws Exception {
String source = "(1 & 0) | 1";
TestLexer lexer = new TestLexer(new ANTLRStringStream(source));
TestParser parser = new TestParser(new CommonTokenStream(lexer));
System.out.println(source + " = " + parser.code());
}
}
produces the desired output:
java -cp antlr-3.3.jar org.antlr.Tool Test.g
javac -cp antlr-3.3.jar *.java
java -cp .:antlr-3.3.jar Main
(1 & 0) | 1 = true