Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'tree' as name of a parser rule causes syntax error. #1960

Closed
mateuszpawlik opened this issue Jul 20, 2017 · 2 comments
Closed

'tree' as name of a parser rule causes syntax error. #1960

mateuszpawlik opened this issue Jul 20, 2017 · 2 comments

Comments

@mateuszpawlik
Copy link

A simple grammar ('g.g4'):

grammar g;
tree : 'X';

causes the following error

java -jar antlr-4.7-complete.jar g.g4 
error(50): g.g4::: syntax error: mismatched character ':' expecting 'g'
error(50): g.g4:2:7: syntax error: ''X'' came as a complete surprise to me

Removing the space between 'tree' and ':' or changing 'tree' to other rule name helps.

I've found that 'tree' was a keyword in antlr v3. But the reference book of v4 doesn't list it any more.

If 'tree' is a reserved keyword, the error message is confusing.

@moy
Copy link

moy commented Sep 26, 2018

It expects a g, and when you give that g (tree g:), it asks for an r, then a a, and so on until you type tree grammar completely, in which case it throws syntax error: 'tree grammar' came as a complete surprise to me. It seems ANTLR is trying to special-case tree grammar as a kind of two-words keyword. Weird.

Workarounds:

  • Don't use tree as a rule name.

  • Stick the : to tree as in tree:, and the error disappears.

@moy
Copy link

moy commented Sep 26, 2018

It seems ANTLR is trying to special-case tree grammar as a kind of two-words keyword.

Indeed:

TREE_GRAMMAR : 'tree' WSNLCHARS* 'grammar' ;

Actually, if I read correctly, this rule is only used to throw a relevant error message when the user writes tree grammar (not supported in ANTLRv4):

if ( $tg!=null ) throw new v3TreeGrammarException(tg);
if ( $t!=null ) ((GrammarRootAST)$tree).grammarType = $t.type;
else ((GrammarRootAST)$tree).grammarType=COMBINED;
}
: ( t=LEXER g=GRAMMAR -> GRAMMAR<GrammarRootAST>[$g, "LEXER_GRAMMAR", getTokenStream()]
| // A standalone parser specification
t=PARSER g=GRAMMAR -> GRAMMAR<GrammarRootAST>[$g, "PARSER_GRAMMAR", getTokenStream()]
// A combined lexer and parser specification
| g=GRAMMAR -> GRAMMAR<GrammarRootAST>[$g, "COMBINED_GRAMMAR", getTokenStream()]
| tg=TREE_GRAMMAR

Still, I don't understand why using a prefix (tree ) of a token raises an error.

KvanTTT added a commit to KvanTTT/antlr4 that referenced this issue Nov 6, 2021
Remove backward compatibility exception v3TreeGrammarException
KvanTTT added a commit to KvanTTT/antlr4 that referenced this issue Nov 12, 2021
Remove backward compatibility exception v3TreeGrammarException
KvanTTT added a commit to KvanTTT/antlr4 that referenced this issue Dec 23, 2021
Remove backward compatibility exception v3TreeGrammarException
@parrt parrt closed this as completed in 7b22bc5 Dec 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants