Slide 1

Slide 1 text

Safe specification of operator precedence rules Ali Afroozeh Mark van den Brand Adrian Johnstone Elizabeth Scott Jurgen Vinju

Slide 2

Slide 2 text

Parsing technology Compiler construction DSLs Reverse Engineering Correctness Speed Natural grammars Modularity Correctness

Slide 3

Slide 3 text

Parsing technology Compiler construction DSLs Reverse Engineering Correctness Speed Natural grammars Modularity Correctness

Slide 4

Slide 4 text

Parsing technology Compiler construction DSLs Reverse Engineering Correctness Speed Natural grammars Modularity General parsing technology Correctness

Slide 5

Slide 5 text

Declarative parser generation EBNF context-free syntax Disambiguation rules Lexical definitions Correct and efficient parser

Slide 6

Slide 6 text

From reference manuals to parsers

Slide 7

Slide 7 text

E ::= T E1 E1 ::= + T E1 | ✏ T ::= F T1 T1 ::= ⇤ F T1 | ✏ F ::= (E) | a E ::=E ⇤ E |E + E |(E) |a ⇤ left + left ⇤ > +

Slide 8

Slide 8 text

50 alternatives 18 precedence levels

Slide 9

Slide 9 text

Contributions

Slide 10

Slide 10 text

Contributions •Safe

Slide 11

Slide 11 text

Contributions •Safe •Correct

Slide 12

Slide 12 text

Contributions •Safe •Correct •Technology Independent

Slide 13

Slide 13 text

Contributions •Safe •Correct •Technology Independent operator precedence rules from reference manuals.

Slide 14

Slide 14 text

Terms

Slide 15

Slide 15 text

Terms •General(ized) parsing

Slide 16

Slide 16 text

Terms •General(ized) parsing •GLL

Slide 17

Slide 17 text

Terms •General(ized) parsing •GLL •Yacc

Slide 18

Slide 18 text

Terms •General(ized) parsing •GLL •Yacc •SDF

Slide 19

Slide 19 text

Terms •General(ized) parsing •GLL •Yacc •SDF •OCaml

Slide 20

Slide 20 text

Terms Technologies •General(ized) parsing •GLL •Yacc •SDF •OCaml

Slide 21

Slide 21 text

Terms Case study •General(ized) parsing •GLL •Yacc •SDF •OCaml

Slide 22

Slide 22 text

Problem Description

Slide 23

Slide 23 text

A simple expression grammar E ::= E + E | E | a

Slide 24

Slide 24 text

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)

Slide 25

Slide 25 text

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)

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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)) - -

Slide 30

Slide 30 text

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)) - -

Slide 31

Slide 31 text

A ::= > B ::= ↵ No B in should derive ↵

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Safety: a disambiguation mechanism is safe if it does not change the underlying language.

Slide 35

Slide 35 text

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)) - - -

Slide 36

Slide 36 text

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)) - - -

Slide 37

Slide 37 text

E E E + E E + E E E E + E ((E + ( E)) + E) - -

Slide 38

Slide 38 text

Problems with current solutions •It is not safe •It is applied at one level only

Slide 39

Slide 39 text

Real world example 1 + if true then 1 + 3 else 4 + 5

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Our solution

Slide 43

Slide 43 text

Operator style ambiguity E ::= E ↵ | E E)E↵) E↵ ( E)↵ E) E) E↵ (E↵)

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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)))

Slide 48

Slide 48 text

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↵

Slide 49

Slide 49 text

Support for indirect recursion expr ::= expr “+” expr > expr ::= “try” expr “with” pattern-matching pattern-matching ::= 
 "|"? pattern ("when" expr)? -> expr

Slide 50

Slide 50 text

An Example E :: = E + E (left) > E |a

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Evaluation •Highly ambiguous reference grammar of OCaml •OCaml files from the OCaml test suite

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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