[antlr-interest] NullPointerException getting text from a rule
Travis Jensen
travis.jensen at gmail.com
Sat Nov 10 16:45:11 PST 2007
Thanks! That did it.
tj
On Fri, 2007-11-09 at 16:58 -0800, Curtis Clauson wrote:
> In a tree parser rule action, to use the ".text" attribute with a
> matched element reference, you must provide the original TokenStream
> from which the tree was made. If you do not, then input.getTokenStream()
> will return null and you get that NullPointerException.
>
> In your main() method, add this line
> nodes.setTokenStream(tokens);
> after you create the NodeStream.
>
> This grammar will then work correctly with the "from fred" source text.
> (tested AntLR v3.0.1)
>
> -- Curtis
>
>
> Travis Jensen wrote:
> > 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
> > <http://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
> > <mailto:travis.jensen at gmail.com>
> > http://cmssphere.blogspot.com/
> > Software Maven * Philosopher-in-Training * Avenged Nerd
>
More information about the antlr-interest
mailing list