I'm trying to parse a simple regex grammar with concatenation, disjunction, and kleene star. My grammar and tests look like this:
from pyparsing import Word, nums, Forward, Suppress, OneOrMore
#A grammar for a simple class of regular expressions
number = Word(nums)('number')
lparen = Suppress('(')
rparen = Suppress(')')
expression = Forward()('expression')
concatenation = expression + expression
concatenation.setResultsName('concatenation')
disjunction = lparen + OneOrMore(expression + Suppress('|')) + expression + rparen
disjunction.setResultsName('disjunction')
kleene = lparen + expression + rparen + Suppress('*')
kleene.setResultsName('kleene')
expression << number | concatenation | disjunction | kleene
#Test a simple input
tests = """
7
23
(7)*
(45)*
(1|2|3)
((2)*|3)
((0|1))*
""".splitlines()
for t in tests:
print t
print expression.parseString(t)
print
However, the program fails on the very first test:
Traceback (most recent call last):
File "main.py", line 34, in <module>
print expression.parseString(t)
File "/home/elliot/miniconda2/lib/python2.7/site-packages/pyparsing.py", line 1216, in parseString
raise exc
pyparsing.ParseException: Expected W:(0123...) (at char 0), (line:1, col:1)
What's the problem here? Also, any other feedback on my grammar (i.e. how I could have done things better/simpler) would be appreciated as well.
The first test is not "7"
. The first test is ""
, because your tests
string starts with an empty line. ""
does not match a valid expression in your grammar.