[antlr-interest] unexpected AST node

Zara Jane Sheil zara.sheil at ucdconnect.ie
Tue Feb 28 04:01:33 PST 2006


Hi,
Im trying to build a parser that acts as a calculator as well as 
recognizing ==,>,>=,<,<=,& and |.

At the moment when I run the code it can determine that the input:
5==5 & 5>3
returns a true value.

However, it also prints out the following error msgs:
<AST>:0:0: unexpected AST node: &
0
<AST>:0:0: unexpected AST node: &
false
Also, when I give it input 3+4 it can return the value 7 but gives an 
error msg:
<AST>:0:0: unexpected AST node: +
false
<AST>:0:0: unexpected AST node: +
false
This happens for all input. Im not sure if theres something wrong with 
the grammar or my Main file.

Im also trying to put in a logical NOT !, any advice on how I might be 
able to achieve this?
Attached is the grammar file and my Main.
Thanks,
Zara

-------------- next part --------------
import antlr.*;
import antlr.collections.*;

public class Main {
    public static void main(String[] args) throws Exception {
        ExprLexer lexer = new ExprLexer(System.in);
        ExprParser parser = new ExprParser(lexer);
        parser.logicalOrExpression();
        AST t = parser.getAST();
        System.out.println(t.toStringTree());
        ExprTreeParser treeParser = new ExprTreeParser();
        //parser.expr();
	int y = treeParser.expr(t);
	System.out.println(y);
        boolean k = treeParser.bexpr(t);
        System.out.println(k);
        boolean w = treeParser.lexpr(t);
        System.out.println(w);
    }
}

-------------- next part --------------
class ExprParser extends Parser;

options {

buildAST = true;
defaultErrorHandler = false;

}

logicalOrExpression: logicalAndExpression (OR^ logicalAndExpression)*
;

logicalAndExpression: bexpr (AND^ bexpr)*
;

bexpr: expr ((EQUALS^|NOT_EQUALS^|GT^|GTE^|LT^|LTE^) expr)*
;

expr: mexpr ((PLUS^|MINUS^) mexpr)*
;

mexpr: atom ((MULT^|DIV^) atom)*
;

atom: INT

| LPAREN! logicalOrExpression RPAREN!

// | LPAREN! logicalOrExpression RPAREN!

;

class ExprLexer extends Lexer;

options {

k = 2; // two characters of lookahead

}

/* Integers */

INT : ('0'..'9')+ ;

/* Ignored characters */

WS
    :   '\r' '\n'   // DOS
    |   '\n'        // UNIX
    |   ' ' {$setType(Token.SKIP);} //ignore this token
    ;

/* Logical operators */

OR : "|";

AND : "&";

/* Comparison operators */

EQUALS : "==";

NOT_EQUALS : "<>";

GT : '>';

GTE : ">=";

LT : '<';

LTE : "<=";

/* Arithmetic operators */

PLUS : '+';

MINUS : '-';

MULT : '*';

DIV : '/';

/* Parentheses */

LPAREN : '(';

RPAREN : ')';

class ExprTreeParser extends TreeParser;

options {
importVocab=ExprParser;
}

expr returns [int r=0] { int a,b;}

: #(PLUS a=expr b=expr) { r = a + b;}
| #(MINUS a=expr b=expr) { r = a - b; }
| #(MULT a=expr b=expr) { r = a * b; }
| #(DIV a=expr b=expr) { r = a / b; }
| i:INT {r = (int)Integer.parseInt(i.getText());}
;

bexpr returns [boolean r=false] {int a,b;}
: #(EQUALS a=expr b=expr) { r = a == b; }
| #(GT a=expr b=expr) { r = a > b; }
| #(GTE a=expr b=expr) { r = a >= b; }
| #(LT a=expr b=expr) { r = a < b; }
| #(LTE a=expr b=expr) { r = a <= b; }
;

lexpr returns [boolean r= false] {boolean a, b;}
: #(OR a=bexpr b=bexpr) { r = a || b; }
| #(AND a=bexpr b=bexpr) { r = a && b; }

;





More information about the antlr-interest mailing list