erikrose / parsimonious

The fastest pure-Python PEG parser I can muster
MIT License
1.79k stars 126 forks source link

Don't allow recursion if no characters are being used up. #209

Closed joranmulderij closed 2 years ago

joranmulderij commented 2 years ago

I was trying to write left associative operators in grammar, but an issue that kept coming up was infinite recursion. This could be solved by making every operator right associative, because the parser would cut out the option of just going down the same node over and over. Making it so that the parser can only visit one node twice if characters have been used up would completely solve this issue.

joranmulderij commented 2 years ago

After looking through the code I found that there is already a todo in the code. expressions.py line 183. This would be really nice to get solved.

lucaswiman commented 2 years ago

@joranmulderij could you post the example that's causing the infinite recursion, or a simplified version of it? That would make addressing the bug much easier because it can be turned into a test case. Thanks!

joranmulderij commented 2 years ago

Here is a pretty minimal example of what would cause this issue. This is a stripped down version of my grammar.

from parsimonious.grammar import Grammar

language_grammar = r"""
expression = operator_expression / non_operator_expression
non_operator_expression = number_expression

operator_expression = expression "+" non_operator_expression

number_expression = ~"[0-9]+"
"""

grammar = Grammar(language_grammar)
grammar.parse('1 + 2')

When this script is run, infinite recursion occurs.