Skip to content Skip to sidebar Skip to footer

How To Parse Groups With Operator And Brackets

First I would like to say that I am just starting using pyparsing, and I need some help with the following. So here is the context: I have a file with text lines. Each line can hav

Solution 1:

Your Regex and sample strings were good inputs for defining a simple parser, usually done a little more formally as a BNF, but these were sufficient. Here is a basic implementation of your simple ans format, you should be able to generalize from here what the question would look like:

import pyparsing as pp

LPAR, RPAR, COMMA, LT = map(pp.Suppress, "(),<")

element = pp.Word(pp.alphas.lower(), pp.alphanums.lower())
action = pp.Word(pp.alphas.lower())
subject = pp.Word(pp.alphas.lower())
number = pp.pyparsing_common.number()
timeout_expr = LT + number("timeout")

# put the pieces together into a larger expression
ans_expr = pp.Group(pp.Literal('ans')
                    + LPAR
                    + element('element')
                    + COMMA
                    + action('action')
                    + COMMA
                    + subject('subject')
                    + COMMA
                    + number('num*')
                    + (COMMA + number('num*'))[...]
                    + RPAR
                    + pp.Optional(timeout_expr)
                    )

# use runTests to try it out, will also flag parse errors
ans_expr.runTests("""
    ans(first, act, sub, 1000)
    ans(first, act, sub, 1000, 2000)
    ans(first, act, sub, 1000, 2000) < 50

    # example of a parsing error
    ans(first, act1, sub, 1000)
    """)

Will print:

ans(first, act, sub, 1000)
[['ans', 'first', 'act', 'sub', 1000]]
[0]:
  ['ans', 'first', 'act', 'sub', 1000]
  - action: 'act'
  - element: 'first'
  - num: [1000]
  - subject: 'sub'

ans(first, act, sub, 1000, 2000)
[['ans', 'first', 'act', 'sub', 1000, 2000]]
[0]:
  ['ans', 'first', 'act', 'sub', 1000, 2000]
  - action: 'act'
  - element: 'first'
  - num: [1000, 2000]
  - subject: 'sub'

ans(first, act, sub, 1000, 2000) < 50[['ans', 'first', 'act', 'sub', 1000, 2000, 50]]
[0]:
  ['ans', 'first', 'act', 'sub', 1000, 2000, 50]
  - action: 'act'
  - element: 'first'
  - num: [1000, 2000]
  - subject: 'sub'
  - timeout: 50

# example of a parsing error
ans(first, act1, sub, 1000)
              ^
FAIL: Expected ',', found '1'  (at char14), (line:1, col:15)

Note the use of results names to help you access the results by name, which will make your parser easier to maintain and use.

Post a Comment for "How To Parse Groups With Operator And Brackets"