Stack overflow when writing a Nix parser #180
Replies: 2 comments 3 replies
-
There are at least two examples of left-recursion in your grammar:
You can find such problems with the linter: final grammar = NixGrammar();
final parser = grammar.build();
print(linter(parser).join('\n\n')); To see how to avoid left-recursion in expression grammars have a look at the examples in the tutorial: https://github.com/petitparser/dart-petitparser?tab=readme-ov-file#writing-a-more-complicated-grammar |
Beta Was this translation helpful? Give feedback.
-
That's probably a typo and you rather want Parser assertExpression() => ref0(assertExpression) & ref0(expression) & ... then at parse-time when trying to read an assertions the assertion-parser immediately calls itself. This leads to the infinite recursion that you observe with the stack overflow. A similar, albeit less obvious case, you have with expressions. The wikipedia link about left-recursion has an in-depth explanation of why it happens and how to solve it (with the example of an expression parser). The PetitParser tutorial also gives various examples of expression grammars that do not have this problem. |
Beta Was this translation helpful? Give feedback.
-
I am writing a Nix parser and I get a stack overflow error:
Any ideas as how to solve this?
Beta Was this translation helpful? Give feedback.
All reactions