Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

The New Coffee Room

  1. TNCR
  2. General Discussion
  3. Puzzle: operator precedence

Puzzle: operator precedence

Scheduled Pinned Locked Moved General Discussion
13 Posts 3 Posters 103 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • KlausK Klaus

    One common method is to have a grammar with a hierarchy of nonterminals to express precedence and associativity, e.g. something like this:

    e4000680-06f1-4616-a073-0a9165589f48-image.png

    Nobody in his right mind would build a parser by hand-built recursive descent, though.

    By the way, an equally interesting problem is the dual problem of how to pretty-print ASTs with a minimal number of parentheses (considering operator precedence and associativity).

    One can even go so far and have only one grammar from which both a parser and a pretty-printer with that property can be derived, but only insane people would ever do that.

    HoraceH Offline
    HoraceH Offline
    Horace
    wrote on last edited by
    #4

    @Klaus said in Puzzle: operator precedence:

    Nobody in his right mind would build a parser by hand-built recursive descent, though.

    Why not?

    Education is extremely important.

    KlausK 1 Reply Last reply
    • HoraceH Horace

      @Klaus said in Puzzle: operator precedence:

      Nobody in his right mind would build a parser by hand-built recursive descent, though.

      Why not?

      KlausK Offline
      KlausK Offline
      Klaus
      wrote on last edited by
      #5

      @Horace said in Puzzle: operator precedence:

      Why not?

      Because look-ahead is too difficult to implement, and grammar refactorings like left-factoring suck. If parsing speed isn't super-important, use a parser combinator library. Otherwise, use a parser generator such as ANTLR. I'm a big fan of SGLR parsing - it allows you to express your grammar in the most direct way possible and avoids the complications of a separate scanner.

      1 Reply Last reply
      • HoraceH Offline
        HoraceH Offline
        Horace
        wrote on last edited by
        #6

        I don't think look ahead is necessary if the language is sufficiently simple.

        Education is extremely important.

        1 Reply Last reply
        • KlausK Offline
          KlausK Offline
          Klaus
          wrote on last edited by
          #7

          I have no idea what you want to do, but if it's more than a toy, check out language workbenches like Metaborg.

          1 Reply Last reply
          • HoraceH Offline
            HoraceH Offline
            Horace
            wrote on last edited by
            #8

            It's just a simple language to do calculations and logical operations on literals or input variables. No functions and the only flow control is if...else. Structure is limited to parentheses and blocks (to group the if...else). I know there are libraries available for this sort of thing, but my hand coded implementation is less than 1000 lines of code and has everything I need. Or at least, so far so good. I solved the problem of this thread by calling a precedence imposer method on binary operations after the recursive tree builder returns the right hand side. If the right hand side of the operation is itself a binary operation, like so:

            a <op1> (b <op2> c)

            then the precedence imposer compares precedence of op1 and op2 and if op1 precedence is >= op2, the operation is re-organized as

            (a <op1> b) <op2> c

            Seems to be working so far. This has been a fun little project, as evidenced that I'm working on it on a weekend.

            Education is extremely important.

            1 Reply Last reply
            • KlausK Offline
              KlausK Offline
              Klaus
              wrote on last edited by
              #9

              But what's your goal? You want to learn more about parsing? Learn about interpreters or language implementation? Code generation?

              1 Reply Last reply
              • HoraceH Offline
                HoraceH Offline
                Horace
                wrote on last edited by
                #10

                this is something I have to write for a product. Oh man you're going to tell me I'm being irresponsible for not using a library.

                Education is extremely important.

                1 Reply Last reply
                • HoraceH Offline
                  HoraceH Offline
                  Horace
                  wrote on last edited by Horace
                  #11

                  According to the google calculator (math expressions typed into the search box), the exponent operator has different associativity than minus.

                  4-3-2 is calculated as (4-3)-2
                  4^3^2 is calculated as 4^(3^2)

                  Python appears to share this. That throws a wrench into my rules that all equal precedence operators are computed left to right.

                  Education is extremely important.

                  1 Reply Last reply
                  • jon-nycJ Online
                    jon-nycJ Online
                    jon-nyc
                    wrote on last edited by
                    #12

                    You’re being irresponsible for not using a library.

                    Only non-witches get due process.

                    • Cotton Mather, Salem Massachusetts, 1692
                    1 Reply Last reply
                    • HoraceH Offline
                      HoraceH Offline
                      Horace
                      wrote on last edited by
                      #13

                      I knew it.

                      Education is extremely important.

                      1 Reply Last reply
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups