-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.

  • 1
    Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/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 Feb 25 '21 at 23:30
  • 1
    Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/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 Feb 25 '21 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? – Tim Roberts Feb 26 '21 at 01: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]`. – Tim Roberts Feb 26 '21 at 01:27
  • I should use argparse , that say to me the mentor – Belkov Dmytro Feb 26 '21 at 05:52
  • @TimRoberts , sorry , when Im creating this post I was already worked for 11 hours , today I edit this post and for now I change traceback for the right one. – Belkov Dmytro Feb 26 '21 at 06:09
  • No, argparse is not useful for this. You don't have any named arguments at all. You need one unnamed thing from the command line. Don't use argparse. And the assignment does not say anything about parglare. How did you choose that? Just because of the word EBNF? The assignment just used EBNF to describe what input you should accept. It's just the spec. You're not expected to use that EBNF in your code. I solved your problem below. – Tim Roberts Feb 26 '21 at 06:39
  • @TimRoberts but what if I want to use EBNF in my python code , where should I find the info for that? And I have solution without EBNF , but just want to try it – Belkov Dmytro Feb 26 '21 at 07:16
  • That's not what this assignment is about. You are drifting into the weeds here. Your EXACT problem, as the error message says, is the parentheses. parglare doesn't want those. It wants `Number | Formula Sign Formula`, but even that's wrong because it has a loop. Better would be `Number | Formula Sign Number`. – Tim Roberts Feb 26 '21 at 18:31

1 Answers1

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( '' ))
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30