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

Problem with negative lookahead + left recursion #458

Open
fsh opened this issue Jan 15, 2023 · 0 comments
Open

Problem with negative lookahead + left recursion #458

fsh opened this issue Jan 15, 2023 · 0 comments

Comments

@fsh
Copy link

fsh commented Jan 15, 2023

Using ~x + ... seems to disallow whitespace skip with left recursion. Is this intended behavior? If so, what's the idiomatic way of doing it in left recursive grammars?

An example:

import pyparsing as pp
pp.ParserElement.inline_literals_using(pp.Suppress)
pp.ParserElement.enable_left_recursion()

Top = pp.Forward()

# an xyz word that doesn't start with xxx
Atom = ~pp.Literal('xxx') + pp.Word('xyz')
#          ^--------- this negative lookahead seems 
#                     to mess with whitespace handling
#                     in recursive grammars?
#
# This workaround works:
# Atom = pp.White()[0,1] + ~pp.Literal('xxx') + pp.Word('xyz')

# (Minibug: pp.White(min=0) cannot match 0 whitespace)

Expr0 = pp.Forward()
Expr0 <<= Expr0 + '[' + Top + ']' | Atom

# ... in my case there's many levels of expressions,
# and they're constructed dynamically ...

Expr1 = Top
Expr1 <<= Top + '*' + Expr0 | Expr0

The thought is that this would match anything like x[ x * x[y] ][y] * y with optional whitespaces anywhere, but whitespace is only skipped inside expressions, not at the start:

>>> Top.run_tests(['x[y*x]', 'x [y * xx ]', '  x', 'x[ y*xx]', '  x*y'])

x[y*x]
['x', 'y', 'x']

x [y * xx ]
['x', 'y', 'xx']

  x
['x']

x[ y*xx]
x[ y*xx]
 ^
ParseException: Expected end of text, found '['  (at char 1), (line:1, col:2)
FAIL: Expected end of text, found '['  (at char 1), (line:1, col:2)

  x*y
  x*y
   ^
ParseException: Expected end of text, found '*'  (at char 3), (line:1, col:4)
FAIL: Expected end of text, found '*'  (at char 3), (line:1, col:4)
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

1 participant