Skip to content

Instantly share code, notes, and snippets.

@shivamMg
Last active November 6, 2018 10:19
Show Gist options
  • Select an option

  • Save shivamMg/b60885fea0cf657d82cbb6e820981d7b to your computer and use it in GitHub Desktop.

Select an option

Save shivamMg/b60885fea0cf657d82cbb6e820981d7b to your computer and use it in GitHub Desktop.
Grammars for parsing arithmetic expressions
Grammar with left-recursion (e.g. Expr's derivation contains Expr as first non-terminal).
Not suitable for Recursive Descent parsing.
Expr = Expr "+" Term | Expr "-" Term | Term
Term = Term "*" Factor | Term "/" Factor | Factor
Factor = "(" Expr ")" | "-" Factor | Number
Grammar without left-recursion. But not left-factored, that's why it needs arbitrary lookahead,
e.g. Expr's derivation will need to look ahead Term to see which derivation to go with.
Suitable for R.D. parsing but needs backtracking.
Expr = Term "+" Expr | Term "-" Expr | Term
Term = Factor "*" Term | Factor "/" Term | Factor
Factor = "(" Expr ")" | "-" Factor | Number
Grammar without left-recursion. Left-factored. Needs single lookahead.
Suitable for R.D. parsing. Does not need backtracking.
Expr = Term Expr'
Expr' = "+" Expr | "-" Expr | ε
Term = Factor Term'
Term' = "*" Term | "/" Term | ε
Factor = "(" Expr ")" | "-" Factor | Number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment