Theory of compilation 236360 erez petrank
This presentation is the property of its rightful owner.
Sponsored Links
1 / 88

Theory of Compilation 236360 Erez Petrank PowerPoint PPT Presentation


  • 61 Views
  • Uploaded on
  • Presentation posted in: General

Theory of Compilation 236360 Erez Petrank. Lecture 3 : Syntax Analysis: Bottom-up Parsing. Source text . txt. Executable code. exe. You are here. Compiler. Lexical Analysis. Syntax Analysis Parsing. Semantic Analysis. Inter. Rep. (IR). Code Gen. Parsers: from tokens to AST.

Download Presentation

Theory of Compilation 236360 Erez Petrank

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Theory of compilation 236360 erez petrank

Theory of Compilation 236360ErezPetrank

Lecture 3: Syntax Analysis:

Bottom-up Parsing


You are here

Source

text

txt

Executable

code

exe

You are here

Compiler

LexicalAnalysis

Syntax Analysis

Parsing

SemanticAnalysis

Inter.Rep.

(IR)

Code

Gen.


Parsers from tokens to ast

Parsers: from tokens to AST

<ID,”b”> <MULT> <ID,”b”> <MINUS> <INT,4> <MULT> <ID,”a”> <MULT> <ID,”c”>

expression

SyntaxTree

expression

term

MINUS

expression

MULT

factor

term

term

ID

term

MULT

factor

term

MULT

factor

‘c’

factor

ID

ID

factor

ID

‘b’

ID

‘a’

‘b’

‘4’

LexicalAnalysis

Syntax Analysis

Sem.Analysis

Inter.Rep.

Code Gen.


Parsing

Parsing

  • A context free language can be recognized by a non-deterministic pushdown automaton.

  • It can be recognized in exponential time via backtracking,

  • And using dynamic programming in O(n3).

  • We want efficient parsers

    • Linear in input size

    • We sacrifice generality for efficiency


Reminder efficient parsers

Reminder: Efficient Parsers

  • Top-down (predictive parsing)

already read…

to be read…

  • Bottom-up (shift reduce)


Lr k grammars

LR(k) Grammars

  • A grammar is in the class LR(K) when it can be derived via:

    • Bottom-up derivation

    • Scanning the input from left to right (L)

    • Producing the rightmost derivation (R)

    • With lookahead of k tokens (k)

  • A language is said to be LR(k) if it has an LR(k) grammar

  • The simplest case is LR(0), which we discuss next.


Lr is more powerful but

LR is More Powerful, But…

  • Any LL(k) language is also in LR(k) (and not vice versa), i.e., LL(k) ⊂ LR(k).

  • But the lookahead is counted differently in the two cases.

  • In an LL(k) derivation the algorithm sees k tokens of the right-hand side of the rule and then must select the derivation rule.

  • With LR(k), the algorithm sees all right-hand side of the derivation rule plus k more tokens.

    • LR(0) sees the entire right-side.

  • LR is more popular today.

  • We will see Yacc, a parser generator for LR parsing, in the exercises and homework.


An example a simple grammar

An Example: a Simple Grammar

E → E * B | E + B | B

B → 0 | 1

  • Let us number the rules:

    (1) E → E * B

    (2) E → E + B

    (3) E → B

    (4) B → 0

    (5) B → 1


Goal reduce the input to the initial variable

Goal: Reduce the Input to the Initial Variable

Example:

0 + 0 * 1

B + 0 * 1

E + 0 * 1

E + B * 1

E * 1

E * B

E

E

E → E * B | E + B | B

B → 0 | 1

*

E

B

We’d like to go over the input so far, and upon seeing a right-hand side of a rule, “invoke” the rule and replace the right-hand side with the left-hand side (which is a single varuable).

1

+

E

B

B

0

0


Use shift reduce

Use Shift & Reduce

E → E * B | E + B | B

B → 0 | 1

In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules. Example: “0+0*1”.

StackInputaction

0+0*1$shift

0 +0*1$reduce

B +0*1$reduce

E +0*1$shift

E+ 0*1$shift

E+0 *1$reduce

E+B *1$reduce

E *1$shift

E* 1$shift

E*1 $reduce

E*B $reduce

E $accept

E

*

E

B

1

+

E

B

B

0

0


How does the parser know what to do

How does the parser know what to do?

  • A state will keep the info gathered so far.

  • A table will tell it “what to do” based on current state and next token.

  • Some info will be kept in a stack.


Lr 0 parsing

LR(0) Parsing

Input

Action Table

Parser

Stack

Goto table

Output


States and lr 0 items

States and LR(0) Items

E → E * B | E + B | B

B → 0 | 1

  • The state will “remember” the potential derivation rules given the part that was already identified.

  • For example, if we have already identified E then the state will remember the two alternatives:

    (1) E →E * B, (2) E → E + B

  • Actually, we will also remember where we are in each of them:

    (1) E →E ●* B, (2) E → E ● + B

  • A derivation rule with a location marker is called LR(0) item.

  • The state is actually a set of LR(0) items. E.g., q13= { E → E ● * B , E → E ● + B}


Intuition

Intuition

  • We gather the input token by token until we find a right-hand side of a rule and then we replace it with the variable on the left side.

  • Going over a token and remembering it in the stack is a shift.

  • Each shift moves to a state that remembers what we’ve seen so far.

  • A reduce replaces a string in the stack with the variable that derives it.


Why do we need a stack

Why do we need a stack?

  • Suppose so far we have discovered E → B →0 and gather information on “E +”.

  • In the given grammar this can only mean

    E → E + ● B

  • Suppose state q6 represents this possibility.

  • Now, the next token is 0, and we need to ignore q6 for a minute, and work on B → 0 to obtain E+B.

  • Therefore, we push q6 to the stack, and after identifying B, we pop it to continue.

E

+

E

B

0

B

0

0 (q4,E) (q6,+) (q1,0)

top


The stack

The Stack

  • The stack contains states.

  • For readability we will also include variables and tokens. (The algorithm does not need it.)

  • Except for the q0 in the bottom of the stack, the rest of the stack will contains pairs of (state, token) or (state, variable).

  • The initial stack contains the initial variable only.

  • At each stage we need to decide if to shift the next token to the stack (and change to the appropriate state) or apply a “reduce”.

  • The action table tells us what to do based on current state and next token: s n : shift and move to qn. r m: reduce according to production rule m. (also accept and error).


The goto table

The Goto Table

  • The second table serves reduces.

  • After reducing a right-hand side to the deriving variable, we need to decide what the next state is.

  • This is determined by the previous state (which is on the stack) and the variable we got.

  • Suppose we reduce according to M →β.

  • We remove β from the stack, and look at the state q that is currently at the top. GOTO(q,M) specifies the next state.


For example

For example…

(1) E → E * B

(2) E → E + B

(3) E → B

(4) B → 0

(5) B → 1


An execution example 0 0 1

(1) E → E * B

(2) E → E + B

(3) E → B

(4) B → 0

(5) B → 1

An Execution Example: 0 + 0 * 1

q1

0

q0

q0


An execution example 0 0 11

(1) E → E * B

(2) E → E + B

(3) E → B

(4) B → 0

(5) B → 1

An Execution Example: 0 + 0 * 1

q6

+

● ● ●

q1

q4

q3

q3

0

B

E

E

q0

q0

q0

q0

q0


The algorithm formally

The Algorithm Formally

  • Initiate the stack to q0.

  • Repeat until halting:

    • Consider action [t,q] for q at the head of the stack and t the next token.

    • “Shift n”: remove t from the input and push t and then qn to the stack.

    • “Reduce m”, where Rule m is: M →β:

      • Remove |β| pairs from the stack, let q be the state at the top of the stack.

      • Push M and the state Goto[q,M] to the stack.

    • “accept”: halt successfully.

    • Empty cell: halt with an error.


Lr 0 item

LR(0) Item

To be matched

Already matched

Input

E ➞ 𝛂β

So far we’ve matched α, expecting to see β


Using lr items to build the tables

Using LR Items to Build the Tables

  • Typically a state consists of several LR items.

  • For example, if we identified a string that is reduced to E, then we may be in one of the following LR items: E → E ● + B orE → E ● *B

  • Therefore one of the states will be:q = {E → E ● + B , E → E ● * B}.

  • But if the current state includes E → E + ●B, then we must be able to let B be derived: Closure!


Adding the closure

Adding the Closure

  • If the current state has the item E → E + ● B, then it should be possible to add derivation of B, e.g., to 0 or 1.

  • Generally, if a state includes an item in which the dot stands before a non-terminal, then we add all the rules for this non-terminal with a dot in the beginning.

  • Definition: the closure set for a set of items is the set of items in which for every item in the set of the form A → 𝛂 ● B β, the set contains an item B → ● δ, for each rule of the form B → δin the grammar.

  • Building the closure set is iterative, as δ may also begin with a variable.


Closure an example

Closure: an Example

  • For example, consider the grammar:and the set: C = { E → E + ● B }

  • C’s closure is: clos(C) = { E → E + ● B ,

    B → ● 0 ,

    B → ● 1 }

  • And this will be the state that will be used for the parser.

E → E * B | E + B | B

B → 0 | 1


Extended grammar

Extended Grammar

  • Goal: easy termination.

  • We will assume that the initial variable only appears in a single rule. This guarantee that the last reduction can be (easily) determined.

  • A grammar can be (easily) extended to have such structure.

    Example: the grammar (1) E → E * B

    (2) E → E + B

    (3) E → B

    (4) B → 0

    (5) B → 1

  • Can be extended into :

  • (0) S → E

  • (1) E → E * B

  • (2) E → E + B

  • (3) E → B

  • (4)B → 0

  • (5)B → 1


The initial state

The Initial State

  • To build the action/goto table, we go through all possible states possible during derivation.

  • Each state represents a (closure) set of LR(0) items.

  • The initial state q0 is the closure of the initial rule.

  • In our example the initial rule is S → ● E, and therefore the initial state is

    q0 = clos({S → ● E }) =

    {S → ● E , E → ● E * B , E → ● E + B ,

    E → ● B , B → ● 0 , B → ● 1}

    We go on and build all possible continuations by seeing a single token (or variable).


The next states

The Next States

  • For each possible terminal or variable x, and each possible state (closure set) q,

    • Find all items in the set of q in which the dot is before an x. We denote this set by q|x

    • Move the dot ahead of the x in all items in q|x.

    • Find the closure of the obtained set: this is the state into which we move from q upon seeing x.

  • Formally, the next set from a set S and the next symbol X

    • step(S,X) = { N➞αXβ | N➞αXβ in the item set S}

    • nextSet(S,X) = -closure(step(S,X))


The next states1

The Next States

  • Recall that in our example: q0 = clos({S → ● E }) =

    {S → ● E , E → ● E * B , E → ● E + B ,E → ● B , B → ● 0 , B → ● 1}

    Let us check which states are reachable from it.


States reachable from q 0 in the example

States reachable from q0 in the example:

State q1:

x = 0

q0|0= { B →● 0 }

q1= clos(q0|0)

= { B → 0● }

State q2:

x = 1

q0|1= { B → ● 1 }

q2= clos({ B → 1 ● })

= { B → 1 ∙ }

State q4:

x = B

q0|B = { E → ∙ B }

q4 = clos ({ E → B ∙ })

= {E →B∙}

State q3:

x = E

q0|E= {S → ∙ E , E → ∙ E * B , E → ∙ E + B}

q3 = clos ({S → E ∙ , E → E ∙ * B , E → E ∙ + B})

={S → E∙, E → E∙*B , E → E ∙ + B}


From these new states there are more reachable states

From these new states there are more reachable states

  • From q1, q2, q4, there are no continuations because the dot is in the end of any item in their sets.

  • From state q3 we can reach the following two states:

מצב 5q:

x = *

q3|* = { E → E ∙ * B }

q5= clos ({ E → E * ∙ B })

= { E → E * ∙ B , B → ∙ 0 , B → ∙ 1 }

מצב 6q:

q6= { E → E + ∙ B , B → ∙ 0 , B → ∙ 1 }


Finally

Finally,

  • From q5 we can proceed with x=0, or x=1, or x=B.

  • But for x=0 we reach q1 again and for x=1 we reach q2.

  • For x=B we get q7:

  • Similarly, from q6 with x=B we get q8:

  • These two states have no continuations. (Why?)

State q7:

clos(q7) = { E → E * B ∙ }

State q8:

clos(q8) = { E → E + B ∙ }


Building the tables

Building the Tables

  • A row for each state.

  • If qj was obtained when x comes after qi, then in row qi and column x, we write j.


Building the tables accept

Building the tables: accept

  • Add “acc” in column $ for each state that has S → E ● as an item.


Building the tables shift

Building the Tables: Shift

  • Any number n in the action table becomes s n.


Building the tables reduce

Building the Tables: Reduce

  • For any state whose set includes the item A → α●, such that A → α is production rule m (m>o) :

  • Fill all columns of that state in the action table with “r m”.


Note lr 0

Note LR(0)

  • When a reduce is possible, we execute it without checking the next token.


Another example

Another Example

Z ➞ exprEOF

expr ➞ term | expr+ term

term ➞ ID | (expr)

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

(just shorthand of the grammar on the top)


Example parsing with lr items

Example: Parsing with LR Items

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

Input: i + i $

}

Z ➞ E $

E ➞ T

E ➞ E + T

Closure

T ➞ i

T ➞ (E )


Input i i

Input: i + i $

Shift

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

i

Z ➞ E $

T ➞ i

Reduce item!

E ➞ T

E ➞ E + T

T ➞ i

T ➞ (E )


Input i i1

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

T

i

Z ➞ E $

Reduce item!

E ➞ T

E ➞ T

E ➞ E + T

T ➞ i

T ➞ (E )


Input i i2

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

T

i

Z ➞ E $

Reduce item!

E ➞ T

E ➞ T

E ➞ E + T

T ➞ i

T ➞ (E )


Input i i3

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

+

T

i

Z ➞ E $

Z ➞ E$

E ➞ T

E ➞ E + T

E ➞ E+T

T ➞ i

T ➞ (E )


Input i i4

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

+

i

T

i

Z ➞ E $

Z ➞ E$

E ➞ E+T

E ➞ T

T ➞ i

E ➞ E + T

E ➞ E+T

T ➞ (E )

T ➞ i

T ➞ (E )


Input i i5

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

+

i

$

T

i

Reduce item!

Z ➞ E $

Z ➞ E$

E ➞ E+T

E ➞ T

T ➞ i

T ➞ i

E ➞ E + T

E ➞ E+T

T ➞ (E )

T ➞ i

T ➞ (E )


Input i i6

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

+

T

T

i

i

Z ➞ E $

Z ➞ E$

E ➞ E+T

E ➞ T

T ➞ i

E ➞ E + T

E ➞ E+T

T ➞ (E )

T ➞ i

T ➞ (E )


Input i i7

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

+

T

$

T

i

i

Reduce item!

Z ➞ E $

Z ➞ E$

E ➞ E+T

E ➞ E+T

E ➞ T

T ➞ i

E ➞ E + T

E ➞ E+T

T ➞ (E )

T ➞ i

T ➞ (E )


Input i i8

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

$

E

+

T

T

i

i

Z ➞ E $

Z ➞ E$

E ➞ T

E ➞ E + T

E ➞ E+T

T ➞ i

T ➞ (E )


Input i i9

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

E

$

E

+

T

T

i

Reduce item!

i

Z ➞ E $

Z ➞ E$

Z ➞ E$

E ➞ T

E ➞ E + T

E ➞ E+T

T ➞ i

T ➞ (E )


Input i i10

Input: i + i $

Z ➞ E $

E ➞ T | E + T

T ➞ i | ( E )

Z

E

$

Reducing the initial rule

means accept

E

+

T

i

T

Z ➞ E $

i

Z ➞ E$

Z ➞ E$

E ➞ T

E ➞ E + T

E ➞ E+T

T ➞ i

T ➞ (E )


View as an lr 0 automaton

View as an LR(0) Automaton

q6

q7

E ➞ T

T

q0

T

T ➞ (E)

E ➞ T

E ➞ E + T

T ➞ i

T ➞ (E)

Z ➞ E$

E ➞ T

E ➞ E + T

T ➞ i

T ➞ (E)

(

q5

i

i

T ➞ i

E

(

(

E

i

q3

q1

q8

Z ➞ E$

E ➞ E+ T

T ➞ (E)

E ➞ E+T

E ➞ E+T

T ➞ i

T ➞ (E)

+

+

)

q9

q2

$

q4

T

T ➞ (E) 

Z ➞ E$

E ➞ E + T


Goto action table

GOTO/ACTION Table

Z ➞ E $

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

rn = reduce using rule number n

sm = shift to state m


Goto action table1

Z ➞ E $

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

GOTO/ACTION Table

top is on the right


Are we done

Are we done?

  • Can make a transition diagram for any grammar

  • Can make a GOTO table for every grammar

  • But the states are not always clear on what to do

  • Cannot make a deterministic ACTION table for every grammar


Lr 0 conflicts

LR(0) Conflicts

q0

T

Z ➞ E$

E ➞ T

E ➞ E + T

T ➞ i

T ➞ (E)

T ➞ i[E]

(

q5

i

T ➞ i

T ➞ i[E]

E

Shift/reduce conflict

Z ➞ E $

E ➞ T

E ➞ E + T

T ➞ i

T ➞ ( E )

T ➞ i[E]


View in action goto table

View in Action/Goto Table

  • shift/reduce conflict…


Lr 0 conflicts1

LR(0) Conflicts

q0

T

Z ➞ E$

E ➞ T

E ➞ E + T

T ➞ i

V ➞ i

T ➞ (E)

(

q5

i

T ➞ i

V ➞ i

E

reduce/reduce conflict

Z ➞ E $

E ➞ T

E ➞ E + T

T ➞ i

V ➞ i

T ➞ (E)

Can there be a shift/shift conflict?


View in action goto table1

View in Action/Goto Table

  • reduce/reduce conflict…


Lr 0 conflicts2

LR(0) Conflicts

  • Any grammar with an -rule cannot be LR(0)

  • You should always be able to tell if an (invisible)  should be reduced to A, without looking at the next token.

  • Inherent shift/reduce conflict

    • A➞  - reduce item

    • P➞αAβ – shift item

    • A➞  can always be predicted from P➞αAβ (and should be predicted without looking at any token).


Back to action goto table

Back to Action/Goto Table

  • Note that reductions ignore the input…


Slr grammars

SLR Grammars

  • A string should not be reduced to a non-terminal N if the look-ahead is a token that cannot follow N

  • A reduce item N➞ 𝛂 is applicable only when the look-ahead is in FOLLOW(N)

  • Differs from LR(0) only on the originally “reduce” rows.

  • We will not reduce sometimes and will shift (or do nothing = error) instead.


Goto action table2

GOTO/ACTION Table

Z ➞ E

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

Rule 1 can only be used with the end of input “$” sign.


Goto action table3

GOTO/ACTION Table

Z ➞ E

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

The things that can follow E are ‘+’ ‘)’ and ‘$’.


Goto action table4

GOTO/ACTION Table

Z ➞ E

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

The things that can follow E are ‘+’ ‘)’ and ‘$’.


Goto action table5

GOTO/ACTION Table

Z ➞ E

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

Same for T


Goto action table6

GOTO/ACTION Table

Z ➞ E

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

Same for T


The shift reduce conflict

The shift/reduce conflict

q0

T

Z ➞ E$

E ➞ T

E ➞ E + T

T ➞ i

T ➞ (E)

T ➞ i[E]

(

q5

i

T ➞ i

T ➞ i[E]

E

Shift/reduce conflict

Z ➞ E $

E ➞ T

E ➞ E + T

T ➞ i

T ➞ ( E )

T ➞ i[E]


Now let s add t i e

Now let’s add “T ➞ i[E]”

Z ➞ E

E ➞ T

E ➞ E + T

T ➞ i

T ➞( E )

T ➞ i[E]


Slr check next token when reducing

SLR: check next token when reducing

  • Simple LR(1), or SLR(1), or SLR.

  • Example demonstrates elimination of a shift/reduce conflict.

  • Can eliminate reduce/reduce conflict when conflicting rule’s variables satisfy FOLLOW(T) ≠ FOLLOW(V).

  • But cannot solve all conflicts.


Consider the following grammar

Consider the following grammar

(0) S’ → S

(1) S → L = R

(2) S → R

(3) L → * R

(4) L → id

(5) R → L


Theory of compilation 236360 erez petrank

q3

R

S → R 

q0

S’ → S

S → L = R

S → R

L → * R

L →  id

R → L

S

q1

S’ → S

q9

S → L = R 

q2

L

S → L = R

R → L 

=

R

q6

S → L = R

R →  L

L → * R

L → id

q5

id

*

L → id 

id

*

id

q4

L → * R

R → L

L → * R

L → id

*

L

q8

R → L 

L

q7

L → * R 

R


Shift reduce conflict

Shift/reduce conflict

  • S → L  = R vs. R → L 

  • FOLLOW(R) contains =

    • S ⇒ L =R ⇒ * R = R

  • SLR cannot resolve the conflict either

(0) S’ → S

(1) S → L = R

(2) S → R

(3) L → * R

(4) L → id

(5) R → L

q2

S → L = R

R → L 

=

q6

S → L = R

R →  L

L → * R

L → id


Resolving the conflict

Resolving the Conflict

  • In SLR: a reduce item N ➞ α is applicable only when the look-ahead is in FOLLOW(N).

  • But there is a full sentential form that we have discovered so far.

  • We can ask what the next token may be given all the derivation made so far.

    • For example, even looking at the follow of the entire sentential form is more restrictive than looking at the follow of the last variable.

    • In a way, FOLLOW(N) merges look-ahead for all alternatives for N

      follow(σA)  follow(A)

    • LR(1) keeps look-ahead with each LR item


Lr 1 item

LR(1) Item

  • LR(1) item is a pair: [ LR(0) item , Look-ahead token ]

  • Meaning: We matched the part left of the dot, looking to match the part on the right of the dot, followed by the look-ahead token.

  • Example: the production L ➞ id yields the following LR(1) items

[L → ● id, *]

[L → ● id, =]

[L → ● id, id]

[L → ● id, $]

[L → id ●, *]

[L → id ●, =]

[L → id ●, id]

[L → id ●, $]

(0) S’ → S

(1) S → L = R

(2) S → R

(3) L → * R

(4) L → id

(5) R → L


Creating the states for lr 1

Creating the states for LR(1)

  • We start with the initial state: q0 will be the closure of: (S’ → ● S , $)

  • Closure for LR(1):

  • For every [A → α ● Bβ , c] in the state:

    • for every production B→δ and every token b in the grammar such that b  FIRST(βc)

    • Add [B → ● δ , b] to S


Closure of s s

Closure of (S’ → ● S , $)

  • We would like to add rules that start with S, but keep track of possible lookahead.

  • (S’ → ● S , $)

  • (S → ● L = R , $):Rules for S

  • (S → ● R , $)

  • (L → ●* R , = ) :Rules for L

  • (L → ●id , = )

  • (R → ● L , $ ) :Rules for R

  • (L → ● id , $ ) :More rules for L

  • (L → ● * R , $ )

(0) S’ → S

(1) S → L = R

(2) S → R

(3) L → * R

(4) L → id

(5) R → L


The state machine

מצב 3

(S → R ∙ , $)

The State Machine

מצב 0

(S’ → ∙ S , $)

(S → ∙ L = R , $)

(S → ∙ R , $)

(L → ∙ * R , = )

(L → ∙ id , = )

(R → ∙ L , $ )

(L → ∙ id , $ )

(L → ∙ * R , $ )

R

(0) S’ → S

(1) S → L = R

(2) S → R

(3) L → * R

(4) L → id

(5) R → L

מצב 1

(S’ → S ∙ , $)

S

מצב 2

(S → L ∙ = R , $)

(R → L ∙ , $)

L

=

*

מצב 6

(S → L = ∙ R , $)

(R → ∙ L , $)

(L → ∙ * R , $)

(L → ∙ id , $)

מצב 4

(L → * ∙ R , =)

(R → ∙ L , =)

(L → ∙ * R , =)

(L → ∙ id , =)

(L → * ∙ R , $)

(R → ∙ L , $)

(L → ∙ * R , $)

(L → ∙ id , $)

id

מצב 5

(L → id∙ , $)

(L → id∙ , =)

*

id

L

מצב 7

(L → * R ∙ , =)

(L → * R ∙ , $)

מצב 8

(R → L ∙ , =)

(R → L ∙ , $)

R


Theory of compilation 236360 erez petrank

מצב 3

(S → R ∙ , $)

The State Machine

מצב 0

(S’ → ∙ S , $)

(S → ∙ L = R , $)

(S → ∙ R , $)

(L → ∙ * R , = )

(L → ∙ id , = )

(R → ∙ L , $ )

(L → ∙ id , $ )

(L → ∙ * R , $ )

R

מצב 9

(S → L = R ∙ , $)

מצב 1

(S’ → S ∙ , $)

S

מצב 2

(S → L ∙ = R , $)

(R → L ∙ , $)

L

=

R

*

מצב 6

(S → L = ∙ R , $)

(R → ∙ L , $)

(L → ∙ * R , $)

(L → ∙ id , $)

מצב 4

(L → * ∙ R , =)

(R → ∙ L , =)

(L → ∙ * R , =)

(L → ∙ id , =)

(L → * ∙ R , $)

(R → ∙ L , $)

(L → ∙ * R , $)

(L → ∙ id , $)

id

מצב 12

מצב 5

(L → id∙ , $)

(L → id∙ , =)

L

*

id

מצב 10

*

id

מצב 11

L

מצב 7

(L → * R ∙ , =)

(L → * R ∙ , $)

מצב 8

(R → L ∙ , =)

(R → L ∙ , $)

R


Theory of compilation 236360 erez petrank

מצב 3

(S → R ∙ , $)

The State Machine

R

מצב 9

(S → L = R ∙ , $)

מצב 11

(L → id ∙ , $)

מצב 1

(S’ → S ∙ , $)

S

id

R

מצב 2

(S → L ∙ = R , $)

(R → L ∙ , $)

מצב 12

(R → L ∙ , $)

L

=

id

מצב 6

(S → L = ∙ R , $)

(R → ∙ L , $)

(L → ∙ * R , $)

(L → ∙ id , $)

L

id

מצב 5

(L → id∙ , $)

(L → id∙ , =)

מצב 10

(L → * ∙ R , $)

(R → ∙ L , $)

(L → ∙ * R , $)

(L → ∙ id , $)

L

*

*

R

id

L

R

מצב 8

(R → L ∙ , =)

(R → L ∙ , $)

id

מצב 13

(L → * R ∙ , $)

מצב 7

(L → * R ∙ , =)

(L → * R ∙ , $)


Back to the conflict

Back to the conflict

  • Is there a conflict now?

q2

(S → L ∙ = R , $)

(R → L ∙ , $)

=

(S → L = ∙ R , $)

(R → ∙ L , $)

(L → ∙ * R , $)

(L → ∙ id , $)

q6


Bottom up parsing

Bottom-up Parsing

  • LR(K)

  • SLR

  • LALR

  • All follow the same pushdown-based algorithm

  • Differ on type of “LR Items”


Building the tables1

Building the Tables

  • Similarly to SLR, we start with the automaton.

  • Turn each transition in a token column to a shift.

  • The variables columns form the gotosection.

  • The “acc” is put in the $ column, for any rule that contains (S’ → S● , $) .

  • For any state that contains an item of the form [A →β●,a], and any rule of the form A →βwhose number is m, use “reduce m” for the row of this state and the column of token a.


Building the table

Building the Table


Theory of compilation 236360 erez petrank

LALR

  • LR tables have large number of entries

  • Often don’t need such refined observation (and cost)

  • LALR idea: find states with the same LR(0) component and merge their look-ahead component as long as there are no conflicts

  • LALR not as powerful as LR(1)

  • For a language like C:

    • SLR cannot handle some of the structures and requires hundreds of states.

    • CLR handles everything but requires thousands of states.

    • LALR handles everything with hundreds of states (not much higher than SLR).


Chomsky hierarchy

Chomsky Hierarchy

Turing machine

Recursively enumerable

Linear-bounded non-deterministic Turing machine

Context sensitive

Context free

Regular

Non-deterministic pushdown automaton

Finite-state automaton


Grammar hierarchy

Grammar Hierarchy

Non-ambiguous CFG

CLR(1)

LALR(1)

LL(1)

SLR(1)

LR(0)


Summary

Summary

  • Bottom up derivation

  • LR(k) can decide on a reduce after seeing the entire right side of the rule plus k look-ahead tokens.

  • particularly LR(0).

  • Using a table and a stack to derive.

  • LR Items and the automaton.

  • Creating the table from the automaton.

  • LR parsing with pushdown automata

  • LR(0), SLR, LR(1) – different kinds of LR items, same basic algorithm

  • LALR: in the exercise.


Next time

Next time

  • Semantic analysis


  • Login