neogeny / TatSu

竜 TatSu generates Python parsers from grammars in a variation of EBNF
https://tatsu.readthedocs.io/
Other
408 stars 48 forks source link

@@nameguard not honored in simple grammar #95

Closed dvdrndlph closed 5 years ago

dvdrndlph commented 5 years ago

I am puzzled by this behavior:

from pprint import pprint
from tatsu import parse

GRAMMAR = """
sequence = {fingering}+ ;
fingering = '1' | '2' | '3' | '4' | '5' | 'x' ;
"""

test = "22"
ast = parse(GRAMMAR, test)
pprint(ast)  # Prints ['2', '2']

test = "xx"
ast = parse(GRAMMAR, test)
pprint(ast)  # tatsu.exceptions.FailedParse: (1:1) no available options :

Setting the grammar to the following works as expected:

GRAMMAR = """
sequence = {fingering}+ ;
fingering = /[1-5x]/ ; 
"""

Am I missing something?

Victorious3 commented 5 years ago

You either have to tell the parser what rule to start with or name it "start". I'm not sure why the other one works in the first place. There's also $ to specify the end of input, I'd add that to your starting rule.

apalala commented 5 years ago

These are questions better asked on StackOverflow.

You probably need to check interactions with @@nameguard, which is turned on by default.

apalala commented 5 years ago

Confirmed that the results is unexpected even with @@nameguard :: False

dvdrndlph commented 5 years ago

Thanks for the quick turnaround. I learned a lot.