-
Notifications
You must be signed in to change notification settings - Fork 53
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
Support left recursive grammars. Currently going into infinite loop #126
Comments
May be helpful: |
I found a workaround that even though is uglier it does the job. You can represent the same grammar without left recursion (see here). It would be great to support left recursion as it is way easier to read. Original (left recursion)The failing example that fails: grammar = """
# Root rule
root ::= where-expr
# Expressions
where-expr ::= "(" ws where-expr ws ")"
| where-expr ws "AND" ws where-expr
| where-expr ws "OR" ws where-expr
| "hello" ws "there"
ws ::= [ ]*
""".strip() Rewrittencan be rewritten avoiding left recursion into: grammar = """
# Root rule
root ::= where-expr
# Expressions
where-expr ::= "NOT" ws where-expr
| simple-where-expr (ws where-expr-tail)?
where-expr-tail ::= "AND" ws simple-where-expr (ws where-expr-tail)?
| "OR" ws simple-where-expr (ws where-expr-tail)?
simple-where-expr ::= "(" ws where-expr ws ")"
| "hello" ws "there"
ws ::= [ ]*
""".strip() |
Hi @AlbertoCastelo , thanks for reporting the issue! The current support for grammar with left recursion can be problematic. You are right that there is a systematic approach to convert CFG with left recursion into an equivalent CFG without left recursion (a tutorial can be found here). That can be a workaround now. We will support automatically eliminating left recursion in XGrammar later. |
@Ubospica Are there any specific roadmap about fixing this issue? It seems like XGrammar directly segfaults on some left-recursive grammars. |
@Dan-wanna-M Thanks for asking for that! We now have a plan to enhance the parser and support the left recursion grammar. That should be completed in 1-2 weeks. |
Env
Currently using
xgrammar==0.1.6
Issue
When creating grammars that contain left recursive rules,
grammar_compiler.compile_grammar(grammar)
gets stuck in infinite recursion.It would be great if:
Reproducing
Example 0: Works well
Example from the web:
Example 1: Works well
Example 2: Gets stuck
The text was updated successfully, but these errors were encountered: