-1
import argparse
from parglare import Grammar
from parglare import Parser

formula = r"""
Formula : Number | (Formula Sign Formula)
Number  : '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
Sign    : '+' | '-'
"""

grammar = Grammar.from_string(formula)
parser = Parser(grammar, build_tree=True, prefer_shifts=True)
parser = argparse.ArgumentParser()

parser.add_argument('expression')

args = parser.parse_args()

expression = args.expression

print(parser.parse_args(expression))

Traceback

task that I need to do

Please help me to find normal examples of coding with EBNF func or explain my mistake.

9
  • 1
    Please supply the expected minimal, reproducible example (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. Off-site links and images of text are not acceptable; we need your question to be self-contained, in keeping with the purpose of this site.
    – Prune
    Commented Feb 25, 2021 at 23:30
  • 1
    Please repeat on topic and how to ask from the intro tour. Asking for recommendations or references is specifically listed as off-topic; your request for examples doesn't belong here. When you properly document your problem, we can perhaps help with the error.
    – Prune
    Commented Feb 25, 2021 at 23:31
  • 1
    That's obviously not the code you're running. The error says it say "mask > =:", which is nowhere here. After you create your parglare.Parser, you immediate overwrite that with argparse.ArgumentParser. Are you supposed to be using parglare? Or are you supposed to be doing the parsing yourself, by hand, using the EBNF as a guide? Commented Feb 26, 2021 at 1:06
  • 1
    And by the way, the advice to use argparse is totally bogus. You only have one parameter. Just do import sys / expression = sys.argv[1]. Commented Feb 26, 2021 at 1:27
  • I should use argparse , that say to me the mentor Commented Feb 26, 2021 at 5:52

1 Answer 1

0

Ordinarily, I wouldn't answer what is obviously a homework question, but in this case I think you are so far off the mark that we'll waste too much time guiding you back into alignment. See if you can use this to figure out how you should have approached the problem.

import sys

def process( accum, op, number ):
    if op == '+':
        return accum + number
    elif op == '-':
        return accum - number
    elif op == '0':
        return number

def parse(expression):
    if not expression:
        return (False, None)
    accum = 0
    number = 0
    pending = '0'
    for c in expression:
        if c.isdigit():
            if number is None:
                number = 0
            number = number * 10 + int(c)
        elif c in "+-":
            if number is None:
                return False, None
            accum = process( accum, pending, number )
            pending = c
            number = None
        else:
            return False, None
    return True, process( accum, pending, number )

if len(sys.argv) > 1:
    print(parse( sys.argv[1] ) )
else:
    print(parse( "1+2+4-2+5-1" ))
    print(parse( "123" ))
    print(parse( "hello+12" ))
    print(parse( "2++12-3" ))
    print(parse( '' ))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.