Sunteți pe pagina 1din 21

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.

Artale

(1)

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Grammars Alessandro Artale
Faculty of Computer Science Free University of Bolzano POS Building Room: 2.03 artale@inf.unibz.it

http://www.inf.unibz.it/artale/

2013/2014 First Semester

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(2)

Summary of Lecture V
Generating Languages from Grammars Ambiguous Grammars

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(3)

Notion of Derivation
To characterize a Language starting from a Grammar we need to introduce the notion of Derivation. The notion of Derivation uses Productions to generate a string starting from another string. Direct Derivation (in symbols ). If P and , V , then, . Derivation (in symbols ). If 1 2 , 2 3 , ..., n1 n , then, 1 n .

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(4)

Generating Languages from Grammars


Generative Denition of a Language. We say that a Language L is generated by the Grammar G= (VT ,VN ,S,P), in symbols L(G), if: L(G) = {w VT | S w}. Example. Consider the following Grammar for arithmetic expressions: E E + E | E E | (E ) | E | id The sequence of Tokens (id + id) is a well-formed sentence: E E (E ) (E + E ) (id + E ) (id + id) Note: Token is a synonym of Terminal Symbol when talking of Grammars for programming languages.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(5)

Derivation Trees for Context-Free Grammars


Derivation Trees, called also Parse Trees, are a visual method of describing any derivation in a context-free grammar. Let G= (VT ,VN ,S,P) be a CFG. A tree is a derivation tree for G if: 1. Every node has a label, which is a symbol of V; 2. The label of the root is S; 3. If a node, n, labeled with A has at least one descendant, then A must be in VN ; 4. If nodes n1 , n2 , . . . , nk are direct descendants of node n, with labels A1 , A2 , . . . , Ak , respectively, then: A A1 , A2 , . . . , Ak must be a production in P.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(6)

Choices in Derivations
At each step in a Derivation there are two choices to be made: 1. Which non-terminal to replace; 2. Which Production to use for that non-terminal. W.r.t. point (1), we have two derivations for (id + id): 1. E E (E ) (E + E ) (id + E ) (id + id) 2. E E (E ) (E + E ) (E + id) (id + id) A Parser will consider either Leftmost Derivationsthe leftmost nonterminal is chosenor Rightmost Derivations.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(7)

Parse-Trees and Derivations


A Parse-Tree is a visualization of a Derivation that ignores variations in the order in which non-terminal are replacedpoint (1) above. The Parse-Tree associated to the two Derivations in the previous slide is E ( E id E E + ) E id

Every Parse-Tree is associated with a unique leftmost and a unique rightmost derivation, and viceversa. Problem of Ambiguity: A sentence can have more than one Parse-Tree. Related to point (2) above: Which Production to use for a given non-terminal

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(8)

Summary of Lecture V
Generating Languages from Grammars Ambiguous Grammars

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(9)

Ambiguity
A grammar is ambiguous if it has more than one Parse-Tree for some string. Equivalently, there is more than one right-most or left-most derivation for some string. Ambiguity is bad: Leaves meaning of some programs ill-dened since we cannot decide its syntactical structure uniquely. Ambiguity is a Property of Grammars, not Languages. Two alternative solutions: 1. Disambiguate the grammar 2. Use extra-grammatical mechanisms, like disambiguating rules, to discard alternative Parse-Trees.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(10)

Ambiguity: Arithmetic Expressions


Consider the Grammar for arithmetic expressions: E E + E | E E | (E ) | E | id The sequence of Tokens id + id id has two Parse-Trees E E id + E id E * E id E id E + E * E id E id

The rst Parse-Tree reects the usual assumption that * takes precedence on +.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(11)

Eliminating Ambiguity by Disambiguating the Grammar


Sometime it is possible to eliminate ambiguity by rewriting the Grammar. Example. Let us rewrite the Grammar for arithmetic expressions: E E +T | T T F T F | F (E ) | id

Enforces precedence of * over +; Enforces left-associativity of + and *

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(12)

Eliminating Ambiguity: Example


E E +T | T T F T F | F (E ) | id

The sequence of Tokens id + id id has now only one Parse-Tree E E T F id + T F id T * F id

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(13)

Ambiguity: The Dangling Else


Consider the Grammar for if-then-else statements: Stmt if Expr then Stmt | | if Expr then Stmt else Stmt other

This Grammar is ambiguous. Example. Consider the statement: if E1 then if E2 then S1 else S2 .

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(14)

The Dangling Else: Example


The statement: if E1 then if E2 then S1 else S2 , has two Parse-Trees Stmt if E1 if then Stmt E2 then S1 else S2 if E1 if Stmt then Stmt else E2 then S1 S2

Typically, the rst Parse-Tree is preferred. Disambiguating Rule: Match each else with the closest unmatched then.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(15)

Disambiguating Dangling Else


Disambiguating Rule: Match each else with the closest unmatched then. The rule can be incorporated into the Grammar if we distinguish between matched and unmatched statements. A statement between a then-else must be matched. Stmt Matched stmt | Unmatched stmt Matched stmt if Expr then Matched stmt else Matched stmt | Other-Stmt

Unmatched stmt if Expr then Stmt | if Expr then Matched stmt else Unmatched stmt

This Grammar generates the same set of strings as the previous one but gives just one Parse-Tree for if-then-else statements.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(16)

Disambiguating Rules: Precedence and Associativity Declarations


Instead of rewriting the Grammar: 1. Use the more natural (ambiguous) Grammar; 2. Along with disambiguating declarations. Most tools (e.g. YACC) allow precedence and associativity declarations for terminals (e.g, * takes precedence over +) to disambiguate grammars (see the Book, Sections 4.8-4.9, for more details).

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(17)

Inherent Ambiguity
It would be nice if for every ambiguous grammar, there were some way to x the ambiguity. Unfortunately, certain CFLs are inherently ambiguous, meaning that every grammar for the language is ambiguous.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(18)

Example: Inherent Ambiguity


The language {0i 1j 2k | i = j or j = k} is inherently ambiguous. Intuitively, at least some of the strings of the form 0n 1n 2n must be generated by two different parse trees, one based on checking the 0s and 1s, the other based on checking the 1s and 2s.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(19)

One Possible Ambiguous Grammar

S AB | CD A 0A1 | 01 B 2B | 2 C 0C | 0 D 1D 2 | 12 A generates equal 0s and 1s B generates any sequence of 2s C generates any sequence of 0s D generates equal 1s and 2s

There are two derivations of every string with equal numbers of 0s, 1s, and 2s. E.g.: S AB 01B 012 S CD 0D 012

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(20)

Ambiguity: Summary
No general techniques for handling ambiguity. Impossible to convert automatically an ambiguous Grammar to an unambiguous one. Used with care, ambiguity can simplify the Grammar Sometimes ambiguous Grammars allow for more natural denitions But then we need extra-grammatical disambiguation mechanisms.

Free University of BolzanoFormal Languages and Compilers. Lecture V, 2013/2014 A.Artale

(21)

Summary of Lecture V
Generating Languages from Grammars Ambiguous Grammars

S-ar putea să vă placă și