ANTLR syntax error unexpected token: plus
Hi I have a small problem in my ANTLR tree grammar. I am using ANTLRWorks 1.4. In parser grammar I have the rule like this:
declaration
: 'variable' IDENTIFIER ( ',' IDENTIFIER)* ':' TYPE ';'
-> ^('variable' IDENTIFIER TYPE)+
So I wanted one tree per each IDENTIFIER.
And in the tree grammar I left only rewrite rules:
declaration
: ^('variable' IDENTIFIER TYPE)+
But when I check grammar I got syntax error unexpected token +. And it is this + sign at the end of the declaration rule in the tree grammar. So what I am doing wrong?
开发者_如何学PythonParser grammar works fine and builds AST tree as expected. I generated lexer and parser for C# and test it for some input.
When parsing the source:
variable a, b, c : int;
you're trying to construct an AST that looks like:
variable variable variable
/ | \
a b c
/ | \
int int int
But since 'variable'
and TYPE
are always the same token, I see no need to create all those duplicate nodes. Why not just do:
declaration
: 'variable' IDENTIFIER ( ',' IDENTIFIER)* ':' TYPE ';'
-> ^('variable' TYPE IDENTIFIER+)
;
which will create an AST like:
variable
/ | | \
int a b c
?
精彩评论