marcelogdeandrade / PythonCompiler

Code used on "Writing your own programming language and compiler with Python" post
GNU General Public License v3.0
236 stars 53 forks source link

Error running your example on python3 #2

Open ibreakoutx opened 5 years ago

ibreakoutx commented 5 years ago

python main.py

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

Did you come across a similar error

marcelogdeandrade commented 5 years ago

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:

NoahGWood commented 5 years ago

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

marcelogdeandrade commented 5 years ago

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

haferburg commented 5 years ago

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"])],
    )