PythonCompiler icon indicating copy to clipboard operation
PythonCompiler copied to clipboard

Error running your example on python3

Open ibreakoutx opened this issue 6 years ago • 4 comments

python main.py

PythonCompiler/parser.py:41: ParserGeneratorWarning: 4 shift/reduce conflicts return self.pg.build()

Did you come across a similar error

ibreakoutx avatar Jan 17 '19 19:01 ibreakoutx

Sadly I didn't come across this error. Can you please give more information about this issue? Your system specs and what version of each lib are you using?

Thank you for reporting it :blush:

marcelogdeandrade avatar Feb 20 '19 22:02 marcelogdeandrade

The warnings are coming up because you forgot to pass a list of precedents to your ParserGenerator

Easy enough fix, just update your Parser class to reflect this: pg = ParserGenerator( # A list of all token names, accepted by the parser. ['NUMBER', 'OPEN_PARENS', 'CLOSE_PARENS', 'PLUS', 'MINUS', 'MUL', 'DIV' ], # A list of precedence rules with ascending precedence, to # disambiguate ambiguous production rules. precedence=[ ('left', ['PLUS', 'MINUS']), ('left', ['MUL', 'DIV']) ] ) code from https://rply.readthedocs.io/en/latest/users-guide/parsers.html

NoahGWood avatar Feb 22 '19 02:02 NoahGWood

Oh, I didn't use precedents because I chose to do a more theoretical approach, solving this issue in the EBNF. I'll look into it to solve this warning.

Thanks for the help

marcelogdeandrade avatar Feb 27 '19 00:02 marcelogdeandrade

The grammar you used in the article doesn't solve this problem, though. You'd have to use different productions for different operators. If you want to combine them like you did, you do have to provide a precedence list, and the generator will handle splitting up the productions for you.

Here's what fixes the issue for the code in the article.

    self.pg = ParserGenerator(
        # A list of all token names accepted by the parser.
        [
            "NUMBER",
            "PRINT",
            "OPEN_PAREN",
            "CLOSE_PAREN",
            "SEMI_COLON",
            "SUM",
            "SUB",
        ],
        precedence=[("left", ["SUM", "SUB"])],
    )

haferburg avatar Oct 27 '19 10:10 haferburg