I am making a simple parser for expressions and this is my code:
import parsimonious as parmon
parser = parmon.Grammar(r"""
E = E "+" E / id
id = "0"/"1"/"2"/"3"/"4"/"5"/"6"/"7"/"8"/"9"
""")
code = "2+2"
print(parser.parse(code))
I get this error:
IncompleteParseError(text, node.end, self)
parsimonious.exceptions.IncompleteParseError: Rule 'rules' matched in its entirety, but it didn't consume all the text. The non-matching portion of the text begins with '/ id
id = "0"/"1"' (line 2, column 16).
I have also tried Lark-parser but couldn't get to work on that either. Help appreciated.
I can't offer anything wrt any of the parsers you mentioned. Have you considered pyparsing?
id
is defined to be a one-digit numerical token.Forward
indicates thatE
will be defined later in the code. (It's analogous to the use of 'forward' in procedural languages.)<<
operator inserts the definition ofE
into itself. The parentheses call for a 'match first,' meaning that the first expression in the 'or' will be applied, if possible.print
functions.Here's a simple parser for that kind of expression.
This codes yields this result.