[antlr-interest] Need help spotting Tree grammar error(s)
Alex Redinger
alex.redinger at segmint.com
Tue Dec 6 11:02:54 PST 2011
Hello All,
This is my first experience with Antlr (or anything related to BNF).
I am trying to make a program that will parse boolean equations, run them
against sets of data (integers for, now), and return true/false.
Currently, the lexer/parser portion is producing the expected ASTs.
However, I the tree parser produces messages like...
node from line 1:12 required (...)+ loop did not match anything at input
'and'
and
"mismatched tree node: + expecting SET"
Below are my tree grammar and test class:
tree grammar KlicWalker;
options {
tokenVocab=Klic;
ASTLabelType=CommonTree;
}
@header {
package klic;
import java.util.ArrayList;
import java.util.Arrays;
}
@members {
ArrayList<String> set = new ArrayList<String>(Arrays.asList("1234567",
"9875639", "1254098", "8234524", "4937341", "9876543"));
boolean isFit = false;
public boolean getResults() {
try{
this.prog();
}
catch (RecognitionException e){
System.out.println("Awe Snap!");
}
finally{
return isFit;
}
}
public void setSet(ArrayList newSet) {
set = newSet;
isFit = false;
}
public ArrayList getSet(){
return set;
}
}
prog
: stat+ EOF
;
stat
: expr NEWLINE {}
| NEWLINE {}
;
expr returns [boolean value]
: ^(OR a=expr b=expr) {$value = $a.value || $b.value;}
| ^(AND a=expr b=expr) {$value = $a.value && $b.value;}
| ^(NOT a=expr) {$value = !$a.value}
| ^(ALLIN ^(SET KLI)) {
$value = true;
ArrayList<CommonTree> includedSet = new
ArrayList<CommonTree>($SET.getChildren());
for(int i = 0; i < includedSet.size(); i++){
if(!set.contains(includedSet.get(i).getText())){
$value = false;
break;
}//endif
}//end of for loop */ }
}
| ^(PARTIN ^(SET KLI+)) {
$value = false;
ArrayList<CommonTree> partialSet = new
ArrayList<CommonTree>($SET.getChildren());
for(int i = 0; i < partialSet.size(); i++){
if(set.contains(partialSet.get(i).getText())){
$value = true;
break;
}
}
| ^(NOTIN ^(SET KLI+)) {
ArrayList<CommonTree> excludedSet = new
ArrayList<CommonTree>($SET.getChildren());
$value = true;
for(int i = 0; i < excludedSet.size(); i++){
if(set.contains(excludedSet.get(i).getText())){
$value = false;
break;
}//endif
}
}
;
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.debug.DebugEventSocketProxy;
import org.antlr.runtime.tree.*;
import java.util.*;
import klic.*;
public class setTest {
public static void main(String args[]) throws Exception {
KlicLexer lex = new KlicLexer(new ANTLRFileStream("input.txt",
"UTF8"));
CommonTokenStream tokens = new CommonTokenStream(lex);
KlicParser parser = new KlicParser(tokens);
KlicParser.prog_return r = parser.prog();
CommonTree t = (CommonTree)r.getTree();
CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
KlicWalker evaluator = new KlicWalker(nodes);
System.out.println(t.toStringTree());
System.out.println(evaluator.getResults());
}
}
So, if input is "+{1234567} or ^{9876543,1112223} and !{8273645,5556667}"
I get an AST of "(or (+ (SET 1234567)) (and (^ (SET 9876543 1112223)) (!
(SET 8273645 5556667))))". Where '+' maps to ALLIN, '^' maps to PARTIN,
'!' maps to NOTIN, and SET is imaginary.
Any hints or help as to how to make my tree parser work is much appreciated.
A small side-note: I also noticed that my parser's and tree parser's
.token don't really match. I don't know if that is a problem
Thanks,
--Alex
More information about the antlr-interest
mailing list