Docsity

All Lectures Combined - Introduction to Compilers | CMSC 430, Study notes of Computer Science

Toggle sidebar
bg1
Types of parsing
Top-down parsers
•start at the root of derivation tree and fill in
•picks a production and tries to match the input
•may require backtracking
•some grammars are backtrack-free (predictive)
Bottom-up parsers
•start at the leaves and fill in
•start in a state valid for legal first tokens
•as input is consumed, change state to encode possibilities
(recognize valid prefixes)
•use a stack to store both state and sentential forms
CMSC 430 Lecture 4, Page 1
Top-down parsing
A top-down parser starts with the root of the parse tree. It is labeled with
the start symbol or goal symbol of the grammar.
To build a parse, it repeats the following steps until the fringe of the parse
tree matches the input string.
1. At a node labeled A, select a production with Aon its lhs and for each
symbol on its rhs, construct the appropriate child.
2. When a terminal is added to the fringe that doesn’t match the input
string, backtrack.
3. Find the next node to be expanded. (Must have a label in NT)
The key is selecting the right production in step 1.
⇒should be guided by input string
CMSC 430 Lecture 4, Page 2
Example grammar
This is a grammar for simple expressions:
1<goal>::= <expr>
2<expr>::= <expr>+<term>
3|<expr>-<term>
4|<term>
5<term>::= <term>*<factor>
6|<term>/<factor>
7|<factor>
8<factor>::= number
9|id
Consider parsing the input string x-2*y
CMSC 430 Lecture 4, Page 3
Backtracking parse example
One possible parse for x-2*y
Prod’n Sentential form Input
–<goal>↑x-2*y
1<expr>↑x-2*y
3<expr>-<term>↑x-2*y
4<term>-<term>↑x-2*y
7<factor>-<term>↑x-2*y
9<id>-<term>↑x-2*y
–<id>-<term>x↑-2*y
–<id>-<term>x-↑2*y
7<id>-<factor>x-↑2*y
9<id>-<num>x-↑2*y
–<id>-<num>x-2↑*y
–<id>-<term>x-↑2*y
5<id>-<term>*<factor>x-↑2*y
7<id>-<factor>*<factor>x-↑2*y
9<id>-<num>*<factor>x-↑2*y
–<id>-<num>*<factor>x-2↑*y
–<id>-<num>*<factor>x-2*↑y
9<id>-<num>*<id>x-2*↑y
–<id>-<num>*<id>x-2*y↑
CMSC 430 Lecture 4, Page 4
pf3
pf4
pf5
pf8

Partial preview of the text

Download All Lectures Combined - Introduction to Compilers | CMSC 430 and more Study notes Computer Science in PDF only on Docsity!

Top-down parsersTypes of parsing

(^) start at the root of derivation tree and fill in

(^) picks a production and tries to match the input

(^) may require backtracking

(^) some grammars are backtrack-free

predictive

Bottom-up parsers

(^) start at the leaves and fill in

(^) start in a state valid for legal first tokens

(^ as input is consumed, change state to encode possibilities recognize valid prefixes

(^) use a stack to store both state and sentential forms

CMSC 430

Lecture 4, Page 1

tree matches the input string. To build a parse, it repeats the following steps until the fringe of the parsethe start symbol or goal symbol of the grammar. A top-down parser starts with the root of the parse tree. It is labeled with Top-down parsing

  1. At a node labeled

A

, select a production with

A

(^) on its

(^) lhs

(^) and for each

symbol on its

(^) rhs

, construct the appropriate child.

  1. When a terminal is added to the fringe that doesn’t match the input string, backtrack.
  2. Find the next node to be expanded. (Must have a label in

N T

The key is selecting the right production in step 1.

⇒

should be guided by input string

CMSC 430

Lecture 4, Page 2

1 This is a grammar for simple expressions:^ Example grammar

< goal

>

expr

expr

expr

term

expr

term

term

term

term

factor

term

factor

factor

factor

number

id

Consider parsing the input string

(^) x - 2 * y

CMSC 430

Lecture 4, Page 3

One possible parse for Backtracking parse example

(^) x - 2 * y

Prod’n Sentential form

Input

goal

>

x - 2 * y

expr

x - 2 * y

expr

term

x - 2 * y

term

term

x - 2 * y

factor

term

x - 2 * y

id > (^) - (^) < term

x - 2 * y

id > (^) - (^) < term

x ↑

  • 2 * y

id > (^) - (^) < term

x -

2 * y

id > (^) - (^) < factor

x -

2 * y

id > (^) - (^) < num

>

x -

2 * y

id > (^) - (^) < num

>

x - 2

  • y

id > (^) - (^) < term

x -

2 * y

id > (^) - (^) < term

factor

x -

2 * y

id > (^) - (^) < factor

factor

x -

2 * y

id > (^) - (^) < num

> (^) * (^) < factor

x -

2 * y

id > (^) - (^) < num

> (^) * (^) < factor

x - 2

  • y

id > (^) - (^) < num

> (^) * (^) < factor

x - 2 *

y

id > (^) - (^) < num

> (^) * (^) < id >

x - 2 *

y

id > (^) - (^) < num

> (^) * (^) < id >

x - 2 * y

CMSC 430

Lecture 4, Page 4

Another possible parse forExample

(^) x - 2 * y

Prod’n Sentential form

Input

goal

>

x - 2 * y

expr

x - 2 * y

expr

term

x - 2 * y

expr

term

term

x - 2 * y

expr

term

term

term

x - 2 * y

expr

term

term

term

x - 2 * y

x - 2 * y

CMSC 430 This isn’t a good property for a parser to have.^ If the parser makes the wrong choices, the expansion doesn’t terminate.

Lecture 4, Page 5

Formally, Top-down parsers cannot handle left-recursion in a grammar. Left recursion

a grammar is

(^) left recursive

(^) if

∃ (^) A

N T

such that

(^) a derivation

A

Aα

(^) for some string

(^) α .

CMSC 430^ Our simple expression grammar is left recursive.

Lecture 4, Page 6

Consider the grammar fragment: To remove left recursion, we can transform the grammar. Eliminating left recursion

foo >

::=

foo > α

β

where

(^) α (^) and

(^) β (^) do not start with

foo

> .

We can rewrite this as:

foo >

::=

β <

bar

>

bar

>

::=

α <

bar

>

where

bar

> (^) is a new non-terminal.

CMSC 430^ This fragment contains no left recursion.

Lecture 4, Page 7

<^ Our expression grammar contains two cases of left recursion^ Example expr

expr

term

expr

term

term

term

term

factor

term

factor

factor

<^ Applying the transformation gives expr

term

expr

′ >

expr

′ >

::=

term

expr

′ >

term

expr

′ >

term

factor

term

′ >

term

′ >

::=

factor

term

′ >

factor

term

′ >

CMSC 430

Lecture 4, Page 8

1 Example grammarRecursive Descent Parsing

S

a (^) A

b

A ::= S

(^) c

Helpers match(x) tok; // current token

if (tok != x) error();

tok = getToken();

Parser S()

if (tok == a) match(a); A();

else if (tok == b) match(b);

else error();

A() }

S(); match(c);

CMSC 430

Lecture 4, Page 13

Basic idea Predictive Parsing

For any two productions

A

α | β , we would like a distinct way of

choosing the correct production to expand.

For some FIRST sets

(^) rhs

(^) α

∈

G , define

(^) FIRST

α ) as the set of tokens that appear as the

first symbol in some string derived from

(^) α .

That is,

(^) x

∈ (^) FIRST

α ) (^) iff (^) α

⇒ ∗ xγ

for some

(^) γ .

Whenever two productions LL(1) property

A

α

and

(^) A

::=

β both appear in the

grammar, we would like

F IRST

α ) ∩

F IRST

β )

=



Pursuing this idea leads toone symbol! This would allow the parser to make a correct choice with a lookahead of only

(^) predictive

(^) LL(1) parsers.

CMSC 430

Lecture 4, Page 14

Sometimes, we can transform a grammar to have this property. What if a grammar does not have the LL(1) property? Left Factoring

For each non-terminal

A

(^) find the longest prefix

α (^) common to two or more of its alternatives.

if (^) α

6

(^)  , then replace all of the

A

(^) productions

A

αβ 1 | αβ 2 | · · ·

αβ n | γ

with

A

αL

γ

L

β (^1) | β 2 | · · ·

β n

where

L

(^) is a new non-terminal.

non-terminal have a common prefix.Repeat until no two alternatives for a single

CMSC 430

Lecture 4, Page 15

Consider a Example

(^) right-recursive

version of the expression grammar:

goal

expr

expr

term

expr

term

expr

term

term

factor

term

factor

term

factor

factor

number

id

To choose between productions 2, 3, & 4, the parser must see past the

(^) number

or (^) id

(^) and look at the

, or

(^) / .

FIRST

FIRST

FIRST

This grammar

(^) fails

the test.

Note:

This grammar is right-associative.

CMSC 430

Lecture 4, Page 16

<^ There are two nonterminals that must be left factored:Example expr

term

expr

term

expr

term

term

factor

term

factor

term

factor

<^ Applying the transformation gives us: expr

term

expr

′ >

expr

′ >

::=

expr

expr

term

factor

term

′ >

term

′ >

::=

term

term

CMSC 430

Lecture 4, Page 17

Substituting back into the grammar yields Example 1 < goal

expr

expr

term

expr

′ >

expr

′ >

::=

expr

expr

term

factor

term

′ >

term

′ >

::=

term

term

factor

number

id

Note: Now, selection requires only a single token lookahead.

This grammar is still right-associative.

CMSC 430

Lecture 4, Page 18

Example:

Sentential form

Input

—

< goal

>

↑ x - 2 * y

1 < expr

>

↑ x - 2 * y

2 < term

> <

expr

′ >

↑ x - 2 * y

6 < factor

> <

term

′ > <

expr

′ >

↑ x - 2 * y

11

< id > <

term

′ > <

expr

′ >

↑ x - 2 * y

—

< id > <

term

′ > <

expr

′ >

x ↑

  • 2 * y

9 < id >  <

expr

′ >

x ↑

  • 2

4 < id > (^) - (^) < expr

>

x ↑

  • 2 * y

—

< id > (^) - (^) < expr

>

x -

↑ 2 * y

2 < id > (^) - (^) < term

> <

expr

′ >

x -

↑ 2 * y

6 < id > (^) - (^) < factor

> <

term

′ > <

expr

′ >

x -

↑ 2 * y

10

< id > (^) - (^) < num

> <

term

′ > <

expr

′ >

x -

↑ 2 * y

—

< id > (^) - (^) < num

> <

term

′ > <

expr

′ >

x - 2

↑

  • y

7 < id > (^) - (^) < num

> (^) * (^) < term

> <

expr

′ >

x -

↑

  • y

—

< id > (^) - (^) < num

> (^) * (^) < term

> <

expr

′ >

x -2 *

↑ y

6 < id > (^) - (^) < num

> (^) * (^) < factor

> <

term

′ > <

expr

′ > x -2 *

↑ y

11

< id > (^) - (^) < num

> (^) * (^) < id > <

expr

′ >

x -2 *

↑ y

—

< id > (^) - (^) < num

> (^) * (^) < id > <

term

′ > <

expr

′ >

x -2 * y

↑

9 < id > (^) - (^) < num

> (^) * (^) < id > <

expr

′ >

x -2 * y

↑

5 < id > (^) - (^) < num

> (^) * (^) < id >

x -2 * y

↑

CMSC 430^ The next symbol determined each choice correctly.

Lecture 4, Page 19

Question: Generality

By (^) eliminating left recursion

(^) and

(^) left factoring

, can we transform an

parsed with a single token lookahead?arbitrary context free grammar to a form where it can be predictively

Answer:

conditions.undecidable whether an equivalent grammar exists that does meet our Given a context free grammar that doesn’t meet our conditions, it is

Many

(^) context free languages

do not have such a grammar.

a n 0 b n | n ≥

1 } ⋃ { a n 1 b 2 n | n ≥

1 }

CMSC 430

Lecture 4, Page 20

The

(^) FOLLOW

(^) construction

rule

FOLLOW

goal

eof

eof

}

expr

eof

eof

}

expr

′

eof

eof

}

term

eof

eof,+,-

term

′

eof,+,-

eof,+,-

factor

eof,+,-

eof,+,-,*,/

CMSC 430

Lecture 4, Page 25

Using

(^) FIRST

and

(^) FOLLOW

For each production To build a predicative recursive-descent parser:

A

(^) α (^) and lookahead

(^) token

(^) expand

A

(^) using production if

(^) token

(^) FIRST

(α )

(^) if (^)  (^) ∈ (^) FIRST

α ) expand

A

(^) using production if

(^) token

(^) FOLLOW

A

(^) all other tokens return

(^) error

If multiple choices, the grammar is not

(^) LL(1)

(^) (predicative).

id

num

/

eof

〈 goal

〉

g (^) → (^) e

g (^) → (^) e

〈expr

〉

e (^) → (^) te ′ e (^) → (^) te ′ – – – – –

〈expr

〉′

e ′ → (^) + e e ′ → (^) - e

e ′ → (^) 

〈term

〉 t → (^) f t ′ t → (^) f t ′ – – – – –

〈term

〉′^

t′ →^ (^) 

t′ →^ (^) 

t′ →^ (^) * t t′ →^ (^) / t t′ →^ (^) 

〈 factor

〉 f (^) → (^) id

f (^) → (^) num

CMSC 430

Lecture 4, Page 26

Features LL(1) grammars

(^) input parsed from left to right

(^) leftmost derivation

(^) one token lookahead

Definition

A grammar

G

(^) is (^) LL(1)

(^) if and only if, for all non-terminals

A

, each distinct

pair of productions

A

(^) β (^) and

A

(^) γ satisfy the condition

FIRST

β ) ⋂ FIRST

(γ ) =

(^) ∅

A grammar

G

(^) is (^) LL

(1) if and only if for each set of productions

A

(^) α (^1) | α (^2) | · · · |

(^) α n

(^) FIRST

α 1 ) , FIRST

α 2 ) , · · · (^) , (^) FIRST

α n ) are all pairwise disjoint

  1. if

(^) α i ⇒ ∗  , then

(^) FIRST

α j ) ⋂ FOLLOW

A

, for all 1

(^) j ≤ (^) n, i

j .

If (^) G (^) is (^)  –free, condition 1 is sufficient.

CMSC 430

Lecture 4, Page 27

Provable facts about LL(1) grammars

LL(1)

(^) grammars:

(^) no left recursive grammar is

(^) LL(1)

(^) no ambiguous grammar is

(^) LL(1)

(^) LL(1)

parsers operate in linear time

(^) an

(^)  –free grammar where each alternative expansion for

A

(^) begins with a

distinct terminal is a

(^) simple

(^) LL(1)

(^) grammar

Not all grammars are

LL(1)

S

(^) aS

(^) | (^) a

is not

(^) LL(1)

FIRST

aS ) =

(^) FIRST

(a ) =

(^) { a }

S

(^) aS

′

S

′ ::=

(^) aS

′ | 

accepts the same language and is

(^) LL(1)

CMSC 430

Lecture 4, Page 28

LL(1)LL grammars

(^) grammars

(^) may need to rewrite grammar (left recursion, left factoring)

(^) resulting grammar larger, less maintainable

LL(k)

(^) grammars

(^) k -token lookahead, more powerful than

(^) LL(1)

(^) grammars

S^ example: ::= (^) ac

(^) | (^) abc

(^) is (^) LL(2)

Not all grammars are

LL(k)

S^ example: ::= (^) a (^) bi j

where

(^) i (^) ≥ (^) j

(^) equivalent to dangling else problem

(^) problem - must choose production after

(^) k tokens of lookahead

CMSC 430^ Bottom-up parsers avoid this problem

Lecture 4, Page 29