Giter Site home page Giter Site logo

aameen951 / operator_precedence Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 9 KB

This repository presents two methods for parsing operators according to precedence rules and associativity.

HTML 8.91% JavaScript 91.09%
parser compiler grammar recursive-decent precedence associativity

operator_precedence's Introduction

Operator Precedence and Associativity

This repository presents two methods for parsing operators according to precedence rules and associativity. The relevant code is in parser_a.js and parser_b.js.

1. parser_a

This parser can handle terms, factors, and exponents with the correct precedence and associativity using simple and straight forward parsing code (loops and recursion) and without any correction passes.

Explanation:

  • There is a separate function for each precedence level.
  • Operators with low precedence call the function for operators with the next level of precedence.
  • Operators with highest precedence calls parse_operand directly.
  • Right-associative operators should already be familiar:
    1. Parse the left side by calling the next precedence level
    2. Parse the operator.
    3. Recurse to parse the right side.
    4. Combine left and right using the operator as the result.
  • Left-associative operators are the tricky ones:
    1. Parse the left side by calling the next precedence level.
    2. Parse the operator.
    3. Parse the right side by calling the next precedence level again.
    4. Combine the left and right using the operator and treat the result as the new left side.
    5. Loop to 2.
    6. return the last left side.

Problems:

After reading the code, you can see that the logic is the same in all functions. Only the operators are different. Also left-associative operators are very similar to right-associative.

In this example there are only 3 levels of precedence so it doesn't matter that much but in a real language there could be more than 10 levels of precedence and each will have more code for error handling and building the AST nodes etc. which means there is going to be a lot of duplicate and error-prone code.

How can you compress them into a single function? I suggest giving it a try yourself. Then look at parser_b for the compressed version of this code.

Precedence Rules and Associativity:

terms (add/sub): lowest precedence and left-associative.
factors (mul/div): higher precedence and left-associative.
exponents (powers): highest precedence and right-associative.

BNF Rules:

add_expr       :=   multiply_expr  |  add_expr       +    multiply_expr
                                   |  add_expr       -    multiply_expr

multiply_expr  :=   power_expr     |  multiply_expr  *    power_expr
                                   |  multiply_expr  /    power_expr

power_expr     :=   operand        |  operand        **   power_expr

2. parser_b

Before looking at this parser, make sure you understand parser_a method.

This is a compressed version of 'parser_a' for handling precedence and associativity. It is data-driven by the following table:

{
  '+'  : { level: 1, associativity: LEFT },
  '-'  : { level: 1, associativity: LEFT },
  '*'  : { level: 2, associativity: LEFT },
  '/'  : { level: 2, associativity: LEFT },
  '**' : { level: 3, associativity: RIGHT },
}

This method is called the Precedence Climbing Method.

Precedence Climbing Method:

operator_precedence's People

Contributors

aameen951 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.