[antlr-interest] NullPointerException getting text from a rule
Travis Jensen
travis.jensen at gmail.com
Fri Nov 9 07:46:42 PST 2007
I have the following action associated with my language:
selectStatement
: 'from' type (localeClause)? (whereClause)? {
contentType=$type.text; }
;
If I use "from fred" as input, the rule is matched appropriately, but I get
an NPE when the action is run. Breaking down the location of the NPE, I
find that my token stream (referenced at
"contentType=input.getTokenStream().toStream(...)")
is null.
The very basic driver that I'm using looks like this, which is taken pretty
much verbatim from the ANTLR reference calculator example:
public class Test {
public static void main(String[] args) throws Exception {
// Create an input character stream from standard in
ANTLRInputStream input = new ANTLRInputStream(System.in);
// Create an ExprLexer that feeds from that stream
cqlLexer lexer = new cqlLexer(input);
// Create a stream of tokens fed by the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Create a parser that feeds off the token stream
cqlParser parser = new cqlParser(tokens);
// Begin parsing at rule prog, get return value structure
cqlParser.query_return r=parser.query();
// Walk the resulting tree
// Get the tree from the parser
CommonTree t = (CommonTree)r.getTree();
// Create a tree node stream from the resulting tree
CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
// Create a tree walker
cqlVcm walker = new cqlVcm(nodes);
// launch at the start rule
walker.query();
}
}
Just for completeness sake, my entire tree parsing grammar is:
tree grammar cqlVcm;
options {
tokenVocab=cql;
ASTLabelType=CommonTree;
}
@header {
import java.util.HashMap;
import java.util.Map;
}
@members {
Map attributes = new HashMap();
String contentType = null;
}
query
: (expr)+
{
StringBuilder qry=new StringBuilder();
qry.append("select * from ");
qry.append(contentType);
if (attributes.size() > 0)
qry.append(" where");
for (Object key:attributes.keySet())
qry.append(key).append('=').append(attributes.get(key));
System.out.println(qry);
}
;
expr
: selectStatement
;
selectStatement
: 'from' type (localeClause)? (whereClause)? {
contentType=$type.text; }
;
localeClause
: ^('locale' QUOTED_STRING) { attributes.put("locale",
$QUOTED_STRING.text); }
;
whereClause
: ^('where' whereCompare)
;
whereCompare
: ^('=' column QUOTED_STRING) {attributes.put($column.text,
$QUOTED_STRING.text); }
;
type
: ID
;
column
: ID
;
Help appreciated. :)
tj
--
Travis Jensen
travis.jensen at gmail.com
http://cmssphere.blogspot.com/
Software Maven * Philosopher-in-Training * Avenged Nerd
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071109/0711acf5/attachment-0001.html
More information about the antlr-interest
mailing list