Docsity

Parsing - Compiler Design - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Parsing - Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Lecture #7, Feb. 5, 2007

  • Grammars
  • Top down parsing
  • Transition Diagrams
  • Ambiguity
  • Left recursion
  • Refactoring by adding levels
  • Recursive descent parsing
  • Predictive Parsers
  • First and Follow
  • Parsing tables
  • Grammar
    • A set of tokens (terminals): T
    • A set of non-terminals: N
    • A set of productions { lhs ::= rhs , ... }
      • lhs in N
      • rhs is a sequence of N U T
    • A Start symbol: S (in N)
  • Shorthands
    • Provide only the productions
      • All lhs symbols comprise N
      • All other sysmbols comprise T
      • lhs of first production is S

Grammars 1

• Parse trees. Grammars 3

  • Graphical representations of derivations.
  • The leaves of a parse tree for fully filled out tree is a sentence.
  • Context Free Grammars
  • how do they compare to regular expressions?
  • Nesting (matched ()’s) requires CFG,’s RE's are not powerful enough.
  • Ambiguity
  • A string has two derivations
  • E ::= E + E | E * E | id
  • x + x * y
  • Left-recursion
  • E ::= E + E | E * E | id
  • Makes certain top-down parsers loop

• Begin with the start symbol and try and derive theTop Down Parsing

parse tree from the root.

  • Consider the grammar

Exp ::= id

| Exp + Exp

| Exp * Exp

| ( Exp )

derives x, x+x, x+x+x,

x * y

x + y * z ...

Top Down Parse (cont)

Exp y * z / |
Exp + Exp | / | \

id(x) Exp * Exp

Exp z / |
Exp + Exp | / | \

id(x) Exp * Exp

| id(y)

Top Down Parse (cont.)

Exp

/ | \

Exp + Exp

| / | \

id(x) Exp * Exp

id(y) id(z)

Problems with Top Down Parsing
  • Backtracking may be necessary:
    • S ::= ee | bAc | bAe
    • A ::= d | cA try on string “bcde”
  • Infinite loops possible from (indirect) left recursive grammars.
    • E ::= E + id | id
  • Ambiguity is a problem when a unique parse is not possible.
  • These often require extensive grammar restructuring (grammar debugging).

Grammar Transformations

  • Removing ambiguity.
  • Removing Left Recursion
  • Backtracking and Factoring

Adding levels (cont)

  • Original grammar

st ::= if exp then st else st | if exp then st | id := exp

  • Assume that every st between then and else must be matched, i.e. it must have both a then and an else.
  • New Grammar with addtional levels

st -> match | unmatch match -> if exp then match else match | id := exp unmatch -> if exp then st | if exp then match else unmatch

Removing Left Recursion

  • Top down recursive descent parsers require non-left recursive grammars
  • Technique: Left Factoring
    • E := E + E | E * E | id
    • E ::= id E’
    • E’ ::= + E E’ | * E E’ | ε
  • General Technique to remove direct left recursion
    • Every Non terminal with productions
      • T ::= T n | T m (left recursive productions) | a | b (non-left recursive productions)
    • Make a new non-terminal T’
    • Remove the old productions
    • Add the following productions
      • T ::= a T’ | b T’
      • T’ ::= n T’ | m T’ | ε T

T n

T n T n a

**(a | b) (n | m) ***

“a” and “b” because they are the rhs of the non-left recurive productions.

Recursive Descent Parsing

• One procedure (function) for each non-terminal.

• Procedures are often (mutually) recursive.

• They can return a bool (the input matches that

non-terminal) or more often they return a data-

structure (the input builds this parse tree)

• Need to control the lexical analyzer (requiring it

to “back-up” on occasion)

R.D. parser for R.E.’s

  • Build an instance of the datatype: datatype Re = empty of int | simple of string * int | concat of Re * Re | closure of Re | union of Re * Re;
  • The lexical Analyzer datatype datatype token = Done | Bar | Star | Hash | Leftparen | Rightparen | Single of string;

Left Recursive Grammar

Alt -> Alt bar Concat Alt -> Concat Concat -> Concat Closure Concat -> Closure Closure -> simple star

Closure -> simple

simple -> id | (Alt ) | #

Alt ::= Concat moreAlt moreAlt ::= Bar Concat moreAlt | ε Concat ::= Closure moreConcat moreConcat ::= Closure moreConcat | ε Closure ::= Simple Star | Simple Simple ::= Id | ( Alt ) | #

1. For every Non terminal with productions T ::= T n | T m (left recursive **productions) | a | b (non-left recursive productions)

  1. Make a new non-terminal T’
  2. Remove the old productions
  3. Add the following productions** T ::= a T’ | b T’ T’ ::= n T’ | m T’ | ε

Lookahead and the Lexer

val lookahead = ref Done;

val input = ref [Done];

val location = ref 0;

fun nextloc () =

(location := (!location) + 1;

!location)

fun init s = (location := 0;

input := lexan s;

lookahead :=