Skip to content

Commit

Permalink
encoding the operator precedence rules as part of the grammar. It wou…
Browse files Browse the repository at this point in the history
…ld make a great test case, when ragg supports precedence, to make sure we get the same results.
  • Loading branch information
dyoo committed Feb 15, 2013
1 parent 8922911 commit d6246fe
Showing 1 changed file with 43 additions and 14 deletions.
57 changes: 43 additions & 14 deletions ragg/examples/lua-parser.rkt
Expand Up @@ -32,28 +32,57 @@ var : NAME | prefixexp "[" exp "]" | prefixexp "." NAME
namelist : NAME ("," NAME)*

explist : (exp ",")* exp

;; FIXME: handle exp operator precedences.


;; Note by dyoo: The parsing of exp deviates from Lua in that we have these administrative
;; rules to explicitly represent the precedence rules.
;;
;; See: http://www.lua.org/manual/5.1/manual.html#2.5.6
;;
;;
;; Ragg doesn't yet automatically desugar operator precedence rules.
;; I'm doing it by hand at the moment, which is not ideal, so a future version of
;; ragg will have a story about describing precedence.
;;
;; Operator precedence in Lua follows the table below, from lower to higher priority:
;;
;; or
;; and
;; < > <= >= ~= ==
;; ..
;; + -
;; * / %
;; not # - (unary)
;; ^
;; or exp_1
;; and exp_2
;; < > <= >= ~= == exp_3
;; .. exp_4
;; + - exp_5
;; * / % exp_6
;; not # - (unary) exp_7
;; ^ exp_8
;;
;; As usual, you can use parentheses to change the precedences of an expression.
;; The concatenation ('..') and exponentiation ('^') operators are right associative.
;; All other binary operators are left associative.

exp : NIL | FALSE | TRUE | NUMBER | STRING | "..." | function |
prefixexp | tableconstructor | exp binop exp | unop exp
;;
;; The original grammar rule before encoding precedence was:
;;
;; exp : NIL | FALSE | TRUE | NUMBER | STRING | "..." | function |
;; prefixexp | tableconstructor | exp binop exp | unop exp

exp : exp_1
exp_1: exp_1 binop_1 exp_2 | exp_2
exp_2: exp_2 binop_2 exp_3 | exp_3
exp_3: exp_3 binop_3 exp_4 | exp_4
exp_4: exp_5 binop_4 exp_4 | exp_5 ;; right associative
exp_5: exp_5 binop_5 exp_6 | exp_6
exp_6: exp_6 binop_6 exp_7 | exp_7
exp_7: unop exp_8
exp_8: exp_9 binop_8 exp_8 | exp_9 ;; right associative
exp_9: NIL | FALSE | TRUE | NUMBER | STRING | "..." | function |
prefixexp | tableconstructor
binop_1: OR
binop_2: AND
binop_3: "<" | ">" | "<=" | ">=" | "~=" | "=="
binop_4: ".."
binop_5: "+" | "-"
binop_6: "*" | "/" | "%"
binop_8: "^"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


prefixexp : var | functioncall | "(" exp ")"

Expand Down

0 comments on commit d6246fe

Please sign in to comment.