[email protected] chapter 3 context-free grammars and parsing ambiguous grammar gang s....

32
Compiler [email protected] 1 Chapter 3 Context-Free Grammars and Parsing Ambiguous Grammar Gang S. Liu College of Computer Science & Technology Harbin Engineering University

Upload: may-lindsey-mason

Post on 02-Jan-2016

223 views

Category:

Documents


3 download

TRANSCRIPT

Compiler [email protected] 1

Chapter 3

Context-Free Grammars and Parsing

Ambiguous Grammar

Gang S. LiuCollege of Computer Science &

TechnologyHarbin Engineering University

Compiler [email protected] 2

Ambiguous Grammar

• The string 34 – 3 * 42 has two different parse trees and syntax trees.

exp → exp op exp | (exp) | number

op → + | - | *

*

- 42

34 3

-

34 *

3 42

A grammar that generates a string with two distinct parse trees is called an ambiguous grammar.

Compiler [email protected] 3

Two Ways to Deal with Ambiguity

1. Disambiguating rule• State a rule that specifies in each ambiguous case

which of the trees is correct.

• It corrects the ambiguity without changing and complicating the grammar.

• Syntactic structure of the language is not given by the grammar alone.

2. Change grammar into a form that forces the construction of a correct parse tree.

• In both cases we must decide which of the trees are correct.

Compiler [email protected] 4

Ambiguous Grammar

• The string 34 – 3 * 42 has two different parse trees and syntax trees.

exp → exp op exp | (exp) | number

op → + | - | *

*

- 42

34 3

-

34 *

3 42

A grammar that generates a string with two distinct parse trees is called an ambiguous grammar.

Compiler [email protected] 5

Precedence

• Some operations have precedence over other operations.– Multiplication has precedence over addition.

• To handle the precedence of operations in the grammar, we group the operators into groups of equal precedence.

• We must write a different rule for each precedence.

Compiler [email protected] 6

Exampleexp → exp op exp | (exp) | numberop → + | - | *

exp → exp addop exp | termaddop → + | -term → term mulop term | factormulop → *factor → (exp) | number

*

- 42

34 3

-

34 *

3 42 √√××

Compiler [email protected] 7

Ambiguous

(34 – 3) – 42 = –11 34 – (3 – 42) = 73

34 – 3 – 42

(34 – 3) – 42

√√ ××

Compiler [email protected] 8

Associativity• Some operation (like subtraction) are left

associative.– A series of subtraction operations is performed from

left to right.

• exp → exp addop exp | term– Recursion on both sides of the operator allows either

side to match repetitions of the operator in a derivation.

• exp → exp addop term | term– The right recursion is replaced with the base case,

forcing the repetitive matches on the left side

– Left recursion makes addition and subtraction left associative.

Compiler [email protected] 9

Example

34 - 3 - 42 has a unique parse tree

Compiler [email protected] 10

Exampleexp → exp addop term | termaddop → + | -term → term mulop factor | factormulop → *factor → (exp) | number

34- 3* 42 has a unique parse tree

exp

exp addop term

term - term mulop factor

factor factor * number

number number

Compiler [email protected] 11

Dangling else Problem

• if (0) if (1) other else other has two parse trees with two meaning

if (0) if (1) other else other and

if (0) if (1) other else other

statement → if-stmt | other

if-stmt → if (exp) statement

| if (exp) statement else statement

exp → 0 | 1

√√××

Compiler [email protected] 12

if (0) if (1) other else other

Compiler [email protected] 13

if (0) if (1) other else other

Compiler [email protected] 14

Dangling else Problem

• if (0) if (1) other else other has two parse trees with two meaning

if (0) if (1) other else other and

if (0) if (1) other else other

1. Modify the grammar.

2. Disambiguating rule: the most closely nested rule.

statement → if-stmt | other

if-stmt → if (exp) statement

| if (exp) statement else statement

exp → 0 | 1

Compiler [email protected] 15

Dangling else Problem

statement → matched-stmt | unmatched-stmt

matched-stmt → if (exp) matched-stmt else

matched-stmt | other

unmatched-stmt → if (exp) statement

| if (exp) matched-stmt else unmatched-stmt

exp → 0 | 1

if (0) if (1) other else other

Compiler [email protected] 16

Inessential Ambiguity

• Sometimes a grammar may be ambiguous and yet always produce unique abstract syntax trees.

• Example:

( a + b ) + c = a + ( b + c )

Compiler [email protected] 17

Extended BNF Notation (EBNF)

• { } repetitions

A → β {α} and A → {α} β

Compiler [email protected] 18

Left and Right Recursion

• Left recursive grammar:

• A → A α | β – Equivalent to β α *

• A → β {α}

• Right recursive grammar:

• A → α A | β - Equivalent to α * β

• A → {α} β

Compiler [email protected] 19

Extended BNF Notation (EBNF)

• { } repetitions

A → β {α} and A → {α} β• [ ] optional constructs

statement → if-stmt | other

if-stmt → if (exp) statement [ else statement ]

exp → 0 | 1

statement → if-stmt | otherif-stmt → if (exp) statement | if (exp) statement else statementexp → 0 | 1

Compiler [email protected] 20

Syntax Diagrams

• Graphical representations for visually representing EBNF rules are called syntax diagrams.

• They consist of boxes representing terminals and nonterminals, arrowed lines representing sequencing and choices, and nonterminal labels for each diagram representing the grammar rule defining that nonterminal.

• A round or oval box is used to indicate terminals in a diagram, while a square or rectangular box is used to indicate nonterminals.

Compiler [email protected] 21

Syntax Diagrams(cont)

• As an example, consider the grammar rule• factor → ( exp ) | number• This is written as a syntax diagram in the

following way:

Compiler [email protected] 22

Syntax Diagrams(cont)

• Syntax diagrams are written from the EBNF rather than the BNF, so we need diagrams representing repetition and optional constructs. Given a repetition such as

• A → { B } • The corresponding syntax diagram is usually

drawn as follow:

Compiler [email protected] 23

Syntax Diagrams(cont)

• An optional construct such as• A → [ B ] • Is drawn as:

Compiler [email protected] 24

Example

exp → exp addop term | termaddop → + | –term → term mulop factor | factormulop → *factor → (exp) | number

exp → term { addop term }addop → + | –term → factor { mulop factor }mulop → *factor → (exp) | number

Compiler [email protected] 25

Example

statement → if-stmt | other

if-stmt → if (exp) statement [ else statement ]

exp → 0 | 1

statement → if-stmt | otherif-stmt → if (exp) statement | if (exp) statement else statementexp → 0 | 1

Compiler [email protected] 26

Context-free Grammar for TINYprogram → stmt-sequencestmt-sequence → stmt-sequence ; statement | statementstatement → if-stmt | repeat-stmt | assign-stmt| read-stmt | write-stmtif-stmt → if exp then stmt-sequence end | if exp then stmt-sequence else stmt-sequence endrepeat-stmt → repeat stmt-sequence until expassign-stmt → identifier := expread-stmt → read identifierwrite-stmt → write expexp → simple-exp comp-op simple-exp | simple-expcomp-op → < | =simple-exp → simple-exp addop term | termaddop → + | -term → term mulop factor | factormulop → * | /factor → (exp) | number | identifier

Compiler [email protected] 27

Factorial Program

{ Sample program in TINY language - computes factorial }read x; { input an integer }if 0 < x then { don't compute if x <= 0 } fact := 1; repeat fact := fact * x; x := x - 1 until x = 0; write fact { output factorial of x }end

Compiler [email protected] 28

Syntax Tree for Factorial Program

Compiler [email protected] 29

Homework

1.2 1.3 1.7

2.1 2.2 2.4 2.5 2.8 2.12 2.17 2.24

3.3 3.5 3.6 3.24

Compiler [email protected] 30

Homework

3.5 Write a grammar for Boolean expressions that includes the constants true and false, the operators and, or, and not, and parentheses. Be sure to give or a lower precedence than and and and a lower precedence than not and to allow repeated not’s, as in the Boolean expression not not true. Also be sure your grammar is not ambiguous.

Compiler [email protected] 31

Homework

3.6 Consider the following grammar representing simplified LISP-like expressions:

a. Write a leftmost and a rightmost derivation for the string ( a 23 (m x y) ) .

b. Draw a parse tree for the string of part(a).

lexp → atom | listatom → number | identifierlist → (lexp-seq) lexp-seq → lexp-seq lexp | lexp

Compiler [email protected] 32

Homework

3.24 For the TINY program

a. Draw the TINY parse tree.

b. Draw the TINY syntax tree.

read x;x := x + 1;write x