To build a recursive grammar with Pyparsing, you need to think a bit using the pyparsing Forward class. In Forward, you define an empty placeholder for an expression, which will be defined later. Here is the start for piraping for this BNF:
EXCLAM,SEMI,HAT,STAR = map(Literal,"!;^*")
LPAR,RPAR = map(Suppress,"()")
token = oneOf(list(alphas.upper()))
I use Literal to define your operators, but by suppressing grouping (), we will use pyparsing Group to physically group the results in the sublists.
- Forward:
expr = Forward()
( '< < =' , expr Forward, ). , BNF as-is:
expr <<= (EXCLAM |
token + SEMI + expr |
Group(LPAR + expr + HAT + expr + RPAR) |
Group(LPAR + expr + STAR + expr + RPAR))
:
(ASD;!^FFF;!)
^
Expected ";" (at char 2), (line:1, col:3)
A;B;C;!
['A', ';', 'B', ';', 'C', ';', '!']
(((A;!^B;!)^C;D;!)*E;!)
[[[['A', ';', '!', '^', 'B', ';', '!'], '^', 'C', ';', 'D', ';', '!'], '*', 'E', ';', '!']]
, BNF , , :
expr <<= (EXCLAM |
OneOrMore(token) + SEMI + expr |
Group(LPAR + expr + HAT + expr + RPAR) |
Group(LPAR + expr + STAR + expr + RPAR))
:
(ASD;!^FFF;!)
[['A', 'S', 'D', ';', '!', '^', 'F', 'F', 'F', ';', '!']]
A;B;C;!
['A', ';', 'B', ';', 'C', ';', '!']
(((A;!^B;!)^C;D;!)*E;!)
[[[['A', ';', '!', '^', 'B', ';', '!'], '^', 'C', ';', 'D', ';', '!'], '*', 'E', ';', '!']]
, , "^" "*" . :
expr <<= (EXCLAM |
Group(OneOrMore(token) + SEMI + ungroup(expr)) |
Group(LPAR + expr + HAT + expr + RPAR) |
Group(LPAR + expr + STAR + expr + RPAR) )
, :
(ASD;!^FFF;!)
[[['A', 'S', 'D', ';', '!'], '^', ['F', 'F', 'F', ';', '!']]]
A;B;C;!
[['A', ';', 'B', ';', 'C', ';', '!']]
(((A;!^B;!)^C;D;!)*E;!)
[[[[['A', ';', '!'], '^', ['B', ';', '!']], '^', ['C', ';', 'D', ';', '!']], '*', ['E', ';', '!']]]
script:
from pyparsing import *
EXCLAM,SEMI,HAT,STAR = map(Literal,"!;^*")
LPAR,RPAR = map(Suppress,"()")
token = oneOf(list(alphas.upper()))
expr = Forward()
expr <<= (EXCLAM |
Group(OneOrMore(token) + SEMI + ungroup(expr)) |
Group(LPAR + expr + HAT + expr + RPAR) |
Group(LPAR + expr + STAR + expr + RPAR) )
tests = """\
(ASD;!^FFF;!)
A;B;C;!
(((A;!^B;!)^C;D;!)*E;!)""".splitlines()
for t in tests:
print t
try:
print expr.parseString(t).dump()
except ParseException as pe:
print ' '*pe.loc + '^'
print pe
print
: , "AAA" - 3 "". , , 1 , "OneOrMore ()" "Word (alphas.upper())", -
[[['ASD', ';', '!'], '^', ['FFF', ';', '!']]]