Recursion in Pyparsing

I was unable to translate this EBNF expression to Pyparsing, any idea?

token:: [A-Z]
P:: !|token;P|(P^P)|(P*P)

The problem is using recursion, the interpreter fails. An expression like this should really be:

(ASD;!^FFF;!)
A;B;C;!
(((A;!^B;!)^C;D;!)*E;!)
+4
source share
2 answers

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', ';', '!']]]
+2

Lisp xD!!

from pyparsing import *

def pushFirst( strg, loc, toks ):
    toks[0][2], toks[0][1] = toks[0][1], toks[0][2]

def parseTerm(term):
    """
    EBNF syntax elements
    EXCLAM =  !
    HAT =  ^ 
    STAR =  *
    SEMI =  ; 
    LPAR =  (
    RPAR =  )
    """

    EXCLAM,HAT,STAR = map(Literal,"!^*")
    LPAR,RPAR = map(Suppress,"()")
    SEMI = Suppress(";")

    token = oneOf(list(alphas.upper()))
    expr = Forward()
    expr <<=    (
                    EXCLAM | 
                    Group(Word(alphas.upper()) + SEMI + ungroup(expr)) | 
                    Group(LPAR + expr + HAT + expr + RPAR).setParseAction( pushFirst ) | 
                    Group(LPAR + expr + STAR + expr + RPAR).setParseAction( pushFirst )
                )
    try:
        result = expr.parseString(term)   
    except ParseException as pe:
        print ' '*pe.loc + '^'
        print pe
    return result[0]



def computeTerm(term):
    print term


term = (parseTerm("(((AXX;!^B;!)^C;D;!)*E;!)"))

computeTerm(term)
+1

Source: https://habr.com/ru/post/1611466/


All Articles