Parsing Tools Aslan Askarov aslan@cs.au.dk Today: using parsing - - PowerPoint PPT Presentation

parsing tools
SMART_READER_LITE
LIVE PREVIEW

Parsing Tools Aslan Askarov aslan@cs.au.dk Today: using parsing - - PowerPoint PPT Presentation

Compilation 2016 Parsing Tools Aslan Askarov aslan@cs.au.dk Today: using parsing tools to parse 3 simple languages 1. Language of arithmetic expressions 2. Straight-line commands 3. Straight-line commands with alternative syntax Language of


slide-1
SLIDE 1

Compilation 2016

Parsing Tools

Aslan Askarov aslan@cs.au.dk

slide-2
SLIDE 2

Today: using parsing tools to parse 3 simple languages

  • 1. Language of arithmetic expressions
  • 2. Straight-line commands
  • 3. Straight-line commands with alternative syntax
slide-3
SLIDE 3

Language of arithmetic expressions

expr -> id | num | expr op expr | ( expr )

  • p -> plus | minus | times | div

datatype aexp = Id of string | Number of int | Op of binop * aexp * aexp and binop = Plus | Minus | Times | Div

Example: 1 + (2*x- 3) CFG: ML code:

?

slide-4
SLIDE 4

Using ml-yacc

ML declarations to be copied verbatim into the generated parser %% parser declarations (nonterminals, terminals, precedence, etc) %% grammar rules .grm file format parser generator .grm.sml file that implements an LR parser .grm.desc file that helps debug grammar conflicts make sure to use %verbose declaration lexer the rest of the compiler

slide-5
SLIDE 5

>_

slide-6
SLIDE 6

Operator associativity

  • Consider expression 7 - 5 - 3
  • What should be the default interpretation?

(7 - 5) - 3 7 - (5 - 3)

Say that operator MINUS has left associativity … right associativity

error

also a possibility

  • For arithmetic MINUS the correct choices are either left

associativity or error, but not right associativity

  • The desired choice is left associativity
slide-7
SLIDE 7

Examples of associativity

Operators Associativity Arithmetic minus, division Left Arithmetic addition, multiplication Left/Right a+b+c = (a+b)+c = a+(b+c) Assignment in C: x = y = 5 Right y is assigned the value of 5; x is assigned the updated value of y Arithmetic exponentiation 2^1^3 Right 2^1^3 = 2^ (1^3) = 2
 not the same as (2^1)^3 = 8 Comparison operators 2 < 3 < 4 Non-associative parse error

slide-8
SLIDE 8

What can parse trees tell about associativity?

  • 7

5 3

left-associative parse tree

  • 7
  • 5

3

7 - 5 - 3

right-associative parse tree

?

slide-9
SLIDE 9

Grammar conflicts

  • Shift/reduce conflicts
  • Typical fixes
  • try specifying associativity/precedence
  • otherwise rewrite grammar
  • Default action: shift
  • Reduce/reduce conflicts
  • Rarely a good sign: must rewrite grammar
slide-10
SLIDE 10

Scenario 1

Example shift/reduce conflict

exp : exp . MINUS exp exp : exp MINUS exp . (reduce by rule 4)

stack: input string exp MINUS exp exp MINUS

slide-11
SLIDE 11

Scenario 1

Example shift/reduce conflict

exp : exp . MINUS exp exp : exp MINUS exp . (reduce by rule 4)

stack: input string exp MINUS exp exp MINUS shift action Scenario 2 stack: input string exp MINUS exp exp MINUS

slide-12
SLIDE 12

Scenario 1

Example shift/reduce conflict

exp : exp . MINUS exp exp : exp MINUS exp . (reduce by rule 4)

stack: input string exp MINUS exp exp MINUS shift action Scenario 2 stack: input string exp exp MINUS reduce action

slide-13
SLIDE 13

Example shift/reduce conflict

  • Possible fix: specifying associativity using%left or

%right directive

  • %right favors shifting
  • %left favors reducing
  • Q: What should we do for MINUS?
  • Other fixes: precedence is given by the order of

parser directives in the .grm file

  • If not enough: rework the grammar!
slide-14
SLIDE 14

Language of straight-line programs

… cmd -> id := aexp | if aexp then cmd | if aexp then cmd else cmd | ( cmds ) cmds -> cmd 
 | cmd ; cmds … and cmd = Assign of id * aexp | If of aexp * cmd * cmd option | Cmds of cmd list

CFG: ML code:

Example: y:=1 + (2*x- 3); 
 if x then if y then z := 20 else y := 2

slide-15
SLIDE 15

Syntactic variation of SLP

… cmd -> id := aexp | if aexp then cmd fi | if aexp then cmd else cmd fi | ( cmds ) cmds -> cmd 
 | cmd ; cmds … and cmd = Assign of id * aexp | If of aexp * cmd * cmd option | Cmds of cmd list

CFG: ML code:

Example: if x then if y then z := 20 else y := 2 fi fi

extra keyword to mark the end of IfThenElse same AST new grammar

slide-16
SLIDE 16

Syntactic variation of SLP

  • Pros of explicit syntax
  • Avoids some grammar conflicts
  • Handy at prototyping stages when designing

your own PL and the language features are not yet stable (unlike this course)

  • Cons
  • Not very elegant; your programmers may be

grumpy

Example: if x then if y then z := 20 else y := 2 fi fi

slide-17
SLIDE 17

Reporting syntax errors

  • Simplest strategy: fail on the first syntax error
  • OK, but not very helpful
  • Local error recovery:
  • adjust parse stack/input at the point of error
  • used in many variants of Yacc (but not ML-Yacc)
  • Global error recovery:
  • adjust input stream before the point of error
  • see Burke-Fisher error repair
  • essentially tries all possible edits
  • used in ML-Yacc
  • see %change and %value directives
slide-18
SLIDE 18

Summary

  • Use %left, %right parser declarations to control

associativity

  • See ML-Yacc manual on Precedence
  • Conflict reports in .desc file
  • Develop your grammar slowly to understand the

source of conflicts

  • Exploring design space in developing surface

syntax

  • ML-Yacc uses global error recovery