antlr - ANTLR3 Tree Grammar: loop did not match anything at input 'EOF' -
i trying create first antlr3 tree grammar, keep hitting same problem. output of parser is:
$ echo 'foo, bar' | ./run.sh foo bar treegrammar.g: node line 0:0 required (...)+ loop did not match @ input 'eof' exception in thread "main" java.lang.nullpointerexception @ driver.main(driver.java:29)
the output shows stage-1 parser results in right tokens ('foo' , 'bar'). somehow stage-2 tree-parser refuses parse results stage-1. since code basic, must simple, dumb oversight @ part ;-)
here's simple test code:
grammar.g:
grammar grammar; options { output = ast; } statement: word (','! word)* eof!; word: id; id: ('a'..'z'|'a'..'z')+; ws: (' ' | '\t' | '\n' | '\r')+ { $channel = hidden; } ;
treegrammar.g:
tree grammar treegrammar; options { tokenvocab = grammar; astlabeltype = commontree; output = template; } statement: word+; word: id;
driver.java:
import java.io.*; import org.antlr.runtime.*; import org.antlr.runtime.tree.*; import org.antlr.stringtemplate.*; public class driver { public static void main(string[] args) throws exception { filereader groupfiler = new filereader("template.stg" ); stringtemplategroup templates = new stringtemplategroup(groupfiler); groupfiler.close(); antlrinputstream input = new antlrinputstream(system.in); grammarlexer lexer = new grammarlexer(input); commontokenstream tokens = new commontokenstream(lexer); grammarparser parser = new grammarparser(tokens); grammarparser.statement_return result = parser.statement(); commontree t = (commontree)result.gettree(); system.out.println(t.tostringtree()); commontreenodestream nodes = new commontreenodestream(t); nodes.settokenstream(tokens); treegrammar walker = new treegrammar(nodes); walker.settemplatelib(templates); walker.statement(); treegrammar.statement_return r2 = walker.statement(); stringtemplate output = (stringtemplate) r2.gettemplate(); system.out.println(output.tostring()); } }
assuming stringtemplate groups formed, problem fatc walk ast twice:
walker.statement(); treegrammar.statement_return r2 = walker.statement();
e.g., call walker.statement()
twice. (first) error telling you:
treegrammar.g: node line 0:0 required (...)+ loop did not match @ input 'eof'
you consume input once walker.statement()
resulting node stream @ end (eof), , call walker.statement()
again , expects tow walk word+
again, yet there's eof
left.
Comments
Post a Comment