Docsity

Understanding Context-Free Grammars: Objectives, Derivations, and Parse Trees - Prof. Elsa, Study notes of Computer Science

Toggle sidebar
bg1
Outline
Objectives and Review
Context-Free Grammars
Properties of Grammars
CS421 Lecture 10: Introduction to Grammars1
Mark Hills
University of Illinois at Urbana-Champaign
June 19, 2008
1Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Mark Hills CS421 Lecture 10: Introduction to Grammars 1 / 30
Outline
Objectives and Review
Context-Free Grammars
Properties of Grammars
Objectives and Review
Context-Free Grammars
Properties of Grammars
Mark Hills CS421 Lecture 10: Introduction to Grammars 2 / 30
Outline
Objectives and Review
Context-Free Grammars
Properties of Grammars
Objectives
Your goal for this lecture is to learn how to do the following things:
◮Identify and explain the parts of a grammar.
◮Define terminal,nonterminal,production,sentence,
derivation,parse tree,left-recursive,ambiguous.
◮Use a grammar to draw the parse tree of a sentence.
◮Identify a grammar that is left-recursive.
◮Know about ambiguous grammars:
◮Be able to identify one when you see it.
◮Be able to show ambiguity using derivations or parse trees.
◮Know two strategies that could eliminate ambiguity.
Mark Hills CS421 Lecture 10: Introduction to Grammars 3 / 30
Outline
Objectives and Review
Context-Free Grammars
Properties of Grammars
Resources
◮Compilers: Principles, Techniques, and Tools, by Aho, Sethi,
and Ullman (aka The Dragon Book)
◮Engineering a Compiler: Co oper and Torczon
◮Modern Compiler Implementation in Java/ML/C: Appel
◮Most material also in Sebesta and Scott books
◮Theoretical material also in Introduction to the Theory of
Computation by Sipser, and Introduction to Automata
Theory, Languages and Computation by Hopcroft, Motwani,
and Ullman
Mark Hills CS421 Lecture 10: Introduction to Grammars 4 / 30
Outline
Objectives and Review
Context-Free Grammars
Properties of Grammars
Reminder: The Problem
◮Computer programs are entered as a stream of ASCII
characters.
4 + if x > 4 then 5 else 0
◮We want to convert them into an Abstract Syntax Tree
ML code
1PlusExp(
2IntExp 4,
3IfExp(
4GtExp(VarExp "x",
5IntExp 4),
6IntExp 5,
7IntExp 0))
Plus
4If
Gt
x4
5 0
Mark Hills CS421 Lecture 10: Introduction to Grammars 5 / 30
Outline
Objectives and Review
Context-Free Grammars
Properties of Grammars
Reminder: The Solution
Characters Lexer Tokens Parser Tree
The conversion from strings to trees is accomplished in two steps.
◮First, convert the stream of characters into a stream of
tokens.
◮This is called lexing or scanning.
◮Turns characters into words and categorizes them.
◮We did this in lectures 6 – 8!
◮Second, convert the stream of tokens into an abstract syntax
tree.
◮This is called parsing.
◮Turns words into sentences.
Mark Hills CS421 Lecture 10: Introduction to Grammars 6 / 30
pf3
pf4
pf5

Partial preview of the text

Download Understanding Context-Free Grammars: Objectives, Derivations, and Parse Trees - Prof. Elsa and more Study notes Computer Science in PDF only on Docsity!

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CS421 Lecture 10: Introduction to Grammars^1

Mark Hills [email protected]

University of Illinois at Urbana-Champaign

June 19, 2008

(^1) Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter Mark Hills CS421 Lecture 10: Introduction to Grammars 1 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

Objectives and Review

Context-Free Grammars

Properties of Grammars

Mark Hills CS421 Lecture 10: Introduction to Grammars 2 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

Objectives

Your goal for this lecture is to learn how to do the following things: ◮ (^) Identify and explain the parts of a grammar. ◮ (^) Define terminal, nonterminal, production, sentence, derivation, parse tree, left-recursive, ambiguous. ◮ (^) Use a grammar to draw the parse tree of a sentence. ◮ (^) Identify a grammar that is left-recursive. ◮ (^) Know about ambiguous grammars: ◮ (^) Be able to identify one when you see it. ◮ (^) Be able to show ambiguity using derivations or parse trees. ◮ (^) Know two strategies that could eliminate ambiguity.

Mark Hills CS421 Lecture 10: Introduction to Grammars 3 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

Resources

◮ (^) Compilers: Principles, Techniques, and Tools, by Aho, Sethi, and Ullman (aka The Dragon Book) ◮ (^) Engineering a Compiler: Cooper and Torczon ◮ (^) Modern Compiler Implementation in Java/ML/C: Appel ◮ (^) Most material also in Sebesta and Scott books ◮ (^) Theoretical material also in Introduction to the Theory of Computation by Sipser, and Introduction to Automata Theory, Languages and Computation by Hopcroft, Motwani, and Ullman

Mark Hills CS421 Lecture 10: Introduction to Grammars 4 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

Reminder: The Problem

◮ (^) Computer programs are entered as a stream of ASCII characters. 4 + if x > 4 then 5 else 0 ◮ (^) We want to convert them into an Abstract Syntax Tree

ML code 1 PlusExp( 2 IntExp 4, 3 IfExp( 4 GtExp(VarExp "x", 5 IntExp 4), 6 IntExp 5, 7 IntExp 0))

Plus

4 If

Gt x (^4)

Outline Objectives and Review Context-Free Grammars Properties of Grammars

Reminder: The Solution

Characters Lexer Tokens Parser Tree

The conversion from strings to trees is accomplished in two steps. ◮ (^) First, convert the stream of characters into a stream of tokens. ◮ (^) This is called lexing or scanning. ◮ (^) Turns characters into words and categorizes them. ◮ (^) We did this in lectures 6 – 8! ◮ (^) Second, convert the stream of tokens into an abstract syntax tree. ◮ (^) This is called parsing. ◮ (^) Turns words into sentences.

Objectives and Review Context-Free Grammars Properties of Grammars

Example Derivations and Parse Trees Ambiguity

Context-free Grammars

Def: A Context-free Grammar (CFG) is a 4-tuple: G = (N, Σ, P, S) where:

  1. N is a finite, nonempty set of symbols (non-terminals)
  2. Σ is a finite set of symbols (terminals) N ∩ Σ = ∅ V ≡ N ∪ Σ (vocabulary)
  3. P is a finite subset of N × V∗^ (production rules)
  4. S ∈ N (Goal symbol or start symbol) Sometimes written as G = (V, Σ, P, S), N = V − Σ

Mark Hills CS421 Lecture 10: Introduction to Grammars 7 / 30

Objectives and Review Context-Free Grammars Properties of Grammars

Example Derivations and Parse Trees Ambiguity

Example Grammar: Arithmetic Expressions

G = (N, Σ, P, S) where:

N = {E , T , F }

Σ = {(, ), +, ∗, id} P = {E → T E → E + T T → F T → T ∗ F F → id F → (E )} S = E

Note: P ⊆ N × V∗, where V = N ∪ Σ = {E , T , F , (, ), +, ∗, id}

Note: (A, α) ∈ P is usually writ- ten: A → α or A ::= α or A : α Mark Hills CS421 Lecture 10: Introduction to Grammars 8 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Notational Conventions

◮ (^) Lower-case letters early in the alphabet (a,b,c), operator symbols, punctuation symbols, digits, bold names like id are terminals ◮ Upper-case letters early in the alphabet (A,B,C), S, and lower-case italic names like expr or stmt are non-terminals ◮ (^) Upper-case letters later in the alphabet (X,Y,Z) are grammar symbols, either terminals or non-terminals ◮ (^) Lower-case letters later in the alphabet (u,v,w,x,y,z) represent strings of terminals ◮ (^) Lower-case Greek letters (α, β, γ) represent strings of grammar symbols These are from The Dragon Book, pp. 166-167.

Mark Hills CS421 Lecture 10: Introduction to Grammars 9 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Derivations of a Grammar

Directly Derives or =⇒ : If α and β are strings in V∗^ (vocabulary), then α directly derives β (written α =⇒ β) iff there is a production A → δ s.t. − A is a symbol in α − Substituting string δ for A in α produces the string β.

Canonical Derivation Step : The above derivation step is called rightmost if A is the rightmost non-terminal in α. (Similarly, leftmost.)

A rightmost derivation step is also called canonical. Mark Hills CS421 Lecture 10: Introduction to Grammars 10 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Derivations and Sentential Forms

Derivation : A sequence of steps α 0 ⇒ α 1 ⇒... ⇒ αn, where α 0 = S is called a derivation. It is written S ⇒∗^ αn. If every derivation step is rightmost, then this is a canonical derivation. Sentential Form : Each αi in a derivation is called a sentential form of G. Sentential forms derived by rightmost (leftmost) derivations are called right(left)-sentential forms.

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Sentences and Languages

Sentences and the Language L(G) : A sentential form αi consisting only of tokens (i.e., terminals) is called a sentence of G. The language generated by G is the set of all sentences of G. It is denoted L(G).

Objectives and Review Context-Free Grammars Properties of Grammars

Example Derivations and Parse Trees Ambiguity

Uniqueness of Derivations (see Dragon Book, p.171)

Derivations and Parse Trees ◮ (^) Every parse tree has a unique derivation: Yes? No? ◮ Every parse tree has a unique rightmost derivation: Yes? No? ◮ (^) Every parse tree has a unique leftmost derivation: Yes? No?

Mark Hills CS421 Lecture 10: Introduction to Grammars 17 / 30

Objectives and Review Context-Free Grammars Properties of Grammars

Example Derivations and Parse Trees Ambiguity

Uniqueness of Derivations (see Dragon Book, p.171)

Derivations and Parse Trees ◮ (^) Every parse tree has a unique derivation: Yes? No? ◮ Every parse tree has a unique rightmost derivation: Yes? No? ◮ (^) Every parse tree has a unique leftmost derivation: Yes? No?

Derivations and Parse Trees ◮ (^) Every u ∈ L(G ) has a unique derivation: Yes? No?

Mark Hills CS421 Lecture 10: Introduction to Grammars 17 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Uniqueness of Derivations (see Dragon Book, p.171)

Derivations and Parse Trees ◮ (^) Every parse tree has a unique derivation: Yes? No? ◮ (^) Every parse tree has a unique rightmost derivation: Yes? No? ◮ Every parse tree has a unique leftmost derivation: Yes? No? Derivations and Parse Trees ◮ (^) Every u ∈ L(G ) has a unique derivation: Yes? No? ◮ (^) Every u ∈ L(G ) has a unique rightmost derivation: Yes? No?

Mark Hills CS421 Lecture 10: Introduction to Grammars 17 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Uniqueness of Derivations (see Dragon Book, p.171)

Derivations and Parse Trees ◮ (^) Every parse tree has a unique derivation: Yes? No? ◮ (^) Every parse tree has a unique rightmost derivation: Yes? No? ◮ Every parse tree has a unique leftmost derivation: Yes? No? Derivations and Parse Trees ◮ (^) Every u ∈ L(G ) has a unique derivation: Yes? No? ◮ (^) Every u ∈ L(G ) has a unique rightmost derivation: Yes? No? ◮ (^) Every u ∈ L(G ) has a unique leftmost derivation: Yes? No?

Mark Hills CS421 Lecture 10: Introduction to Grammars 17 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Ambiguity

Unambiguous Grammar: A grammar, G, is said to be unambiguous if ∀ u ∈ L(G), ∃ exactly one leftmost or rightmost (canonical) derivation S =⇒∗^ u. Ambiguous Grammar: Otherwise, G is said to be ambiguous. E.g., Grammar : E → E + E | E ∗ E | (E ) | id Two parse trees for u = id + id ∗ id:

E

E

E id

+ E

E

id

* E

id

E

E

E

id

+ E

id

* E

id

These are different syntactic interpretations of the input code.

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Order of Evaluation of Parse Tree

Note: These are conventions, not theorems

◮ (^) Code for a non-terminal is evaluated as a single “block” ◮ (^) I.e., cannot partially execute it, then execute something else, then evaluate the rest ◮ (^) A different parse tree would be needed to achieve that ◮ (^) E.g. 1: Non-terminal T enforces precedence of * over + ◮ (^) E.g. 2: E → E + T enforces left-associativity, E → T + E enforces right-associativity. ◮ (^) Parse tree does not specify order of execution of code blocks ◮ (^) Must be enforced by code generated for parent block. Obey: ◮ (^) Operator (e.g, +) cannot be evaluated before operands ◮ (^) Associativity rules

Objectives and Review Context-Free Grammars Properties of Grammars

Example Derivations and Parse Trees Ambiguity

Common Sources of Ambiguity

◮ (^) There are two common forms of ambiguity: ◮ (^) The “dangling else” form: E→ if E then E else E E→ if E then E E→ whatever Example: if a then if x then y else z ... to which if does the else belong? ◮ (^) The “double-ended recursion” form: E→^ E + E E→ E * E Example “3 + 4 * 5” ... is it “(3 + 4) * 5” or “3 + (4 * 5)”?

Mark Hills CS421 Lecture 10: Introduction to Grammars 20 / 30

Objectives and Review Context-Free Grammars Properties of Grammars

Example Derivations and Parse Trees Ambiguity

The Dangling-Else Ambiguity

Draw two separate parse trees for the “dangling else” example:

if a then if x then y else z

E→ if E then E else E E→ if E then E E→ id Note: id is the common token for variable names a, x, y, z.

Mark Hills CS421 Lecture 10: Introduction to Grammars 21 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Parse Tree 1 for Dangling-Else

◮ (^) Draw two separate parse trees for the “dangling else” example:

if a then if x then y else z

E→ if E then E else E E→ if E then E E→ id Tree 1:

E

if E a

then E

if E x

then E y

else E z

Mark Hills CS421 Lecture 10: Introduction to Grammars 22 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Parse Tree 2 for Dangling-Else

◮ (^) Draw two separate parse trees for the “dangling else” example: if a then if x then y else z E→ if E then E else E E→ if E then E E→ id Tree 2:

E

if E a

then E

if E x

then E y

else E z

Mark Hills CS421 Lecture 10: Introduction to Grammars 23 / 30

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Fixing Ambiguity

◮ (^) Ambiguity can often be eliminated by thinking more carefully about what you are trying to express with your grammar. ◮ (^) Double-ended recursion usually reveals a lack of precedence and associativity information. ◮ (^) “Dangling else” usually matches with the nearest if. This can be encoded in the grammar. See §4.3 of the Dragon Book for details. ◮ (^) Language fixes can eliminate this problem – for instance, keywords or symbols to identify the start and end of control blocks (i.e. if-then-else-fi)

Outline Objectives and Review Context-Free Grammars Properties of Grammars

CFGs Example Derivations and Parse Trees Ambiguity

Fixing Ambiguity

◮ (^) The “double-ended recursion” form usually reveals a lack of precedence and associativity information. A technique called stratification often fixes this. ◮ (^) Left-recursive means “associates to the left”, similarly right-recursive. ◮ (^) Higher precedence rules occur lower in the grammar. E→ E + T E→ T T→ T * F T→ F F → ( E ) F → integer