[antlr-interest] [AST grammar] input null pointer exception
maulattu
maulattu at yahoo.it
Tue Sep 16 07:00:16 PDT 2008
Hi mates!
I'm using antlr to build a tree grammar used to parse this pseudo-language
BUS_0 := EBUS_0 | EBUS_1;
in order to have a compiled code like this:
0; 0; 1; 128
that is:
* 0 -> "or" (operator)
* 0 -> ebus_0 (1st operand)
* 1 -> ebus_1 (2nd operand)
* 128 -> bus_0 (result)
I've an assignment operator (:=), or (|) and "and" (&). In future it will support shift operator.
I used a grammar to build an AST derived from the simpleCalc grammar available on the antlr book as well as on the wiki:
grammar canScript;
options {
language = Java;
output = AST;
ASTLabelType=CommonTree; // type of $stat.tree ref etc...
}
prog: ( stat {System.out.println($stat.tree.toStringTree());} )+ ;
stat: busExpr ':=' expr ';' -> ^(':=' busExpr expr)
| NEWLINE ->
;
expr: andExpr ('|'^ andExpr)*
;
andExpr
: atom ('&'^ atom)*
;
atom: INT
| ebusExpr
| '('! expr ')'!
;
busExpr
: 'BUS_0'
| 'BUS_1'
| 'BUS_2'
;
ebusExpr
: 'EBUS_0'
| 'EBUS_1'
| 'EBUS_2'
;
INT : '0'..'9'+ ;
NEWLINE:'\r'? '\n' ;
WS : (' '|'\t'|'\n'|'\r')+ {skip();} ;
This is the walker grammar:
tree grammar canScriptEval;
options {
language = Java;
tokenVocab = canScript;
ASTLabelType = CommonTree;
}
@header {
import java.util.Vector;
}
@members {
Vector<String> vect = new Vector<String>(50,50);
}
prog
: stat+
;
stat
: expr
| ^(':=' busExpr expr)
;
expr
: ^('|' a=expr b=expr) {
vect.add(new String("OR"));
/*vect.add($a.text);
vect.add($b.text);*/
}
| ^('&' a=expr b=expr) {
vect.add(new String("AND"));
/*vect.add($a.text);
vect.add($b.text);*/
}
| ebusExpr
| INT
;
busExpr
: 'BUS_0'
| 'BUS_1'
| 'BUS_2'
;
ebusExpr
: 'EBUS_0'
| 'EBUS_1'
| 'EBUS_2'
;
It works fine, since that if it receives as input
BUS_0 := (EBUS_0 | EBUS_1) & EBUS_2;
it will correctly build the ast:
(:= BUS_0 (& (| EBUS_0 EBUS_1) EBUS_2))
As you can see from the walker grammar, the commented rows cause a null-pointer exception in the resulting java code
vect.add(new String("OR"));
vect.add((a!=null?(input.getTokenStream().toString(
input.getTreeAdaptor().getTokenStartIndex(a.start),
input.getTreeAdaptor().getTokenStopIndex(a.start))):null));
In particular, input.getTokenStream() returns null and I can't understand why (it is in the "expr()" function)
A lot of thanks in advance!
Mau
__________________________________________________
Do You Yahoo!?
Poco spazio e tanto spam? Yahoo! Mail ti protegge dallo spam e ti da tanto spazio gratuito per i tuoi file e i messaggi
http://mail.yahoo.it
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080916/6e9226b1/attachment.html
More information about the antlr-interest
mailing list