Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Safe Specification of Operator Precedence Rules

Ali Afroozeh
October 27, 2013

Safe Specification of Operator Precedence Rules

Presented at Software Language Engineering (SLE) 2013 in Indianapolis, US.

Event link: http://splashcon.org/2013/program/932

For more information see the Iguana Parsing Framework: http://iguana-parser.github.io

Ali Afroozeh

October 27, 2013
Tweet

More Decks by Ali Afroozeh

Other Decks in Research

Transcript

  1. Safe specification of operator precedence rules Ali Afroozeh Mark van

    den Brand Adrian Johnstone Elizabeth Scott Jurgen Vinju
  2. Parsing technology Compiler construction DSLs Reverse Engineering Correctness Speed Natural

    grammars Modularity General parsing technology Correctness
  3. E ::= T E1 E1 ::= + T E1 |

    ✏ T ::= F T1 T1 ::= ⇤ F T1 | ✏ F ::= (E) | a E ::=E ⇤ E |E + E |(E) |a ⇤ left + left ⇤ > +
  4. A simple expression grammar Where + is left associative and

    + > - E ::= E + E | E | a a + a + a ((a + a) + a) (a + (a + a)) a + a ( (a + a)) (( a) + a)
  5. A simple expression grammar Where + is left associative and

    + > - E ::= E + E | E | a a + a + a ((a + a) + a) (a + (a + a)) a + a ( (a + a)) (( a) + a)
  6. A ::= > B ::= ↵ No B in should

    derive ↵ E ::= E + E > E ::= E - a + a
  7. A ::= > B ::= ↵ No B in should

    derive ↵ E ::= E + E > E ::= E - a + a E E E + E (( E) + E) E E + E E ( (E + E)) - -
  8. A ::= > B ::= ↵ No B in should

    derive ↵ E ::= E + E > E ::= E - a + a E E E + E (( E) + E) E E + E E ( (E + E)) - -
  9. A ::= > B ::= ↵ No B in should

    derive ↵ E ::= E + E > E ::= E a + - a
  10. A ::= > B ::= ↵ No B in should

    derive ↵ E ::= E + E > E ::= E E E E + E (E + ( E)) a + - a -
  11. E E E + E E + E E E

    E + E E + E a + - a + a ((E + ( E)) + E) (E + (E + E)) One level filtering E E E + E E + E (E + (( E) + E)) - - -
  12. E E E + E E + E E E

    E + E E + E a + - a + a ((E + ( E)) + E) (E + (E + E)) One level filtering E E E + E E + E (E + (( E) + E)) - - -
  13. E E E + E E + E E E

    E + E ((E + ( E)) + E) - -
  14. Real world example 1 + (if true then 1 +

    3 else (4 + 5)) 1 + (if true then 1 + 3 else 4) + 5
  15. Real world example 1 + (if true then 1 +

    3 else (4 + 5)) 1 + (if true then 1 + 3 else 4) + 5
  16. Operator style ambiguity E ::= E ↵ | E E)E↵)

    E↵ ( E)↵ E) E) E↵ (E↵)
  17. Operator style ambiguity E ::= E + E | E

    | a We only filter left and right recursive ends
  18. Operator style ambiguity E ::= E + E | E

    | a We only filter left and right recursive ends
  19. Support for deeper levels E ⇤ ) E E)E↵ ⇤

    ) E↵) E↵ ( ( E))↵ E ⇤ ) E) E) E↵ ( (E↵)) E ::= E ↵ | E | ...
  20. Support for deeper levels E ::= E + E |

    E | a E)E + E)E + E + E)E + E + E E)E + E)E + E)E + E + E (E + (( E) + E)) (E + ( (E + E)))
  21. Our operator precedence semantics E ::= E ↵ | E

    E ⇤ ) E If , then should not derive . E↵ > E E also, no derivable on the right most end,
 i.e., , should derive . E E ⇤ ) E E qE↵
  22. Support for indirect recursion expr ::= expr “+” expr >

    expr ::= “try” expr “with” pattern-matching pattern-matching ::= 
 "|"? pattern ("when" expr)? -> expr
  23. An Example E :: = E + E (left) >

    E |a E ::= E + qE, E ::= E + E E ::= qE + E, E ::= E
  24. An Example E :: = E + E (left) >

    E |a E1 E2 E ::= E + qE, E ::= E + E E ::= qE + E, E ::= E
  25. An Example E :: = E + E (left) >

    E |a E1 E2 E ::= E2 + E1 | E | a E ::= E + qE, E ::= E + E E ::= qE + E, E ::= E
  26. An Example E1 :: = E | a E ::=

    E2 + E1 | E | a E ::= E + qE, E ::= E + E
  27. An Example E1 :: = E | a E ::=

    E2 + E1 | E | a E2 ::= E2 + E1 | a E ::= E + qE, E ::= E + E E ::= qE + E, E ::= E
  28. An Example E1 :: = E | a E ::=

    E2 + E1 | E | a E2 ::= E2 + E1 | a
  29. An Example E1 :: = E | a E ::=

    E2 + E1 | E | a E2 ::= E2 + E1 | a
  30. An Example E1 :: = E | a E ::=

    E2 + E1 | E | a E2 ::= E2 + E1 | a E3 ::= a
  31. An Example E1 :: = E | a E ::=

    E2 + E1 | E | a E3 ::= a E2 ::= E2 + E3 | a
  32. Results •229 files in general •215 correctly parse and disambiguate

    •182 files produce exact ASTs •The failing cases are related to semicolon rules and AST transformations of the OCaml compiler
  33. Conclusions •Safe disambiguation for operator precedence •Supporting arbitrary deep ambiguity

    patterns •Implementation by grammar rewriting •Evaluation by parsing OCaml examples against its highly ambiguous reference manual