Skip to content

Equivalent of nonassoc precedence #109

Description

@Zelatrix

I want to be able to parse expressions like -5 and -(2 + 3 * 4). At the moment, I'm doing this with two separate rules, but since -5 is the same as -(5) I want to be able to use the same rule for both situations as the parentheses in -5 are implicit. In PLY, there is the nonassoc precedence, so I was wondering if such a thing also existed in RPLY?

Activity

  1. nobodxbodon commented on Jul 7, 2021

    @nobodxbodon
    Contributor

    Here's a test case with nonassoc:

    def test_default_reductions(self):

  2. Zelatrix commented on Jul 7, 2021

    @Zelatrix
    Author

    I did this in my parser, and it sees the integer token 5 and complains because it was expecting a parenthesis. Have I missed something?

             precedence = [
                         ('left', ['PLUS', 'STAR', 'SLASH']),
                         ('nonassoc', ['MINUS'])
                         ]
    
            @self.pg.production('expr : MINUS LEFT_PAR expr RIGHT_PAR')
            def uneg_expr(p):
                return UNeg(self.builder, self.module, p[2])

    If the code I compile is

    print(-(5))
    

    instead of

    print(-5)
    

    it works

  3. nobodxbodon commented on Jul 7, 2021

    @nobodxbodon
    Contributor

    I'm afraid I don't see how nonassoc would help making -(5) equivalent with -5. Have you achieved this using PLY?

  4. Zelatrix commented on Jul 7, 2021

    @Zelatrix
    Author

    I've not achieved it yet, I'm trying to achieve it at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions