Table Driven Parser - Lecture Notes | CIS 631, Study notes of Computer Science

Material Type: Notes; Class: Compiler Design; Subject: Computer & Info Science; University: Syracuse University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-urs
koofers-user-urs 🇺🇸

10 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Table Driven Parsers
Both top-down and bottom-up parsers can be written that
explicitly manage a stack while scanning the input to
determine if it can be correctly generated from the grammar
productions
In top-down parsers, the stack will have non-terminals which can
be expanded by replacing it with the right-hand-side of a production
In bottom-up parsers, the stack will have sequences of terminals
and non-terminals which can be reduced by replacing it with the
non-terminal for which it is the rhs of a production
Both techniques use a table to guide the parser in deciding
what production to apply, given the top of the stack and the
next input
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Table Driven Parser - Lecture Notes | CIS 631 and more Study notes Computer Science in PDF only on Docsity!

Table Driven Parsers

•^

Both top-down and bottom-up parsers can be written thatexplicitly manage a stack while scanning the input todetermine if it can be correctly generated from the grammarproductions^ –

In top-down parsers, the stack will have non-terminals which canbe expanded by replacing it with the right-hand-side of a production

-^

In bottom-up parsers, the stack will have sequences of terminalsand non-terminals which can be reduced by replacing it with thenon-terminal for which it is the rhs of a production

•^

Both techniques use a table to guide the parser in decidingwhat production to apply, given the top of the stack and thenext input

Top-down and Bottom-up Parsers •^

Predictive parsers are top-down, non-backtracking^ –

Sometimes called LL(k)

  • Scan the input from Left to right• Generates a Leftmost derivation from the grammar• k is the number of lookahead symbols to make parsing

deterministic

–^

If a grammar is not in an LL(k) form, removing left recursion anddoing left-factoring may produce one

  • Not all context free languages can have an LL(k) grammar

•^

Shift-reduce parsers are bottom-up parsers, sometimescalled LR(k)^ –

Scan the input from Left to Right

-^

Produce a Rightmost derivation from the grammar

•^

Not all context free languages have LR grammars

Parsing Algorithm •^

The parser starts in a configuration with S on thestackRepeat:

let X be the top of stack symbol, a is the next symbolif X is a terminal symbol or

if (X = a) then pop X from the stack and getnextsymelse error

else

// X is a non-terminal

if M[X, a] = X ->Y1 Y2 … Yk{

pop X from the stack;push Y1 Y2 … Yk on the stack with Y1 on top;output the production

else error

Until stack is empty

Example from the dragon book (Aho et al) •^

The expression grammar

E

E + T | T

T

T * F | F

F

( E ) | id

•^

Can be rewritten to eliminate the left recursion

E

T E’

E’

+ T E’ |

T

F T’

T’

* F T’ |

F

( E ) | id

E

T E’

T

F T’

F^

id

T’

e

E’

+ T’ E’

T

F T’

F^

id

T’

* F T’

F^

id

T’

e

E’

e

id + id * id $id + id * id $id + id * id $id + id * id $

  • id * id $+ id * id $+ id * id $

id * id $id * id $id * id $

  • id $* id $

id $id $

$E $E’T$E’T’F$E’T’id$E’T’$E’$E’T +$E’T$E’T F$E’T’ id$E’T’$E’T’ F *$E’T’ F$E’T’ id$E’T’$E’$

Output

Input

Stack

Constructing the LL parsing table •^

For each production of the form N

E in the grammar

–^

For each terminal a in First(E),

add N

E to M[N, a]

–^

If

ε

is in First(E), for each terminal b in Follow(N),

add N

E to M[N, b]

–^

If

ε

is in First(E) and eot is in Follow(N),

add N

E to M[N, b]

–^

All other entries are errors

Shift Reduce Parser actions •^

During the parse^ –

the stack has a sequence of terminal and non-terminal symbolsrepresenting the part of the input worked on so far, and

-^

the input has the remaining symbols

•^

Parser actions^ –

Reduce: If the stack has a sequence FE and there is a productionN

E, we can replace E by N to get FN on the stack

–^

Shift: If there is no possible reduction, transfer the next inputsymbol to the top of the stack

-^

Error: otherwise it is an error

•^

If, after a reduce, we get the start symbol on the top of thestack and there is no more input, then we have succeeded.

•^

Parser example: pg. 3 of Stanford notes 9

Handles •^

During the parse, the term handle refers to a sequence ofsymbols on the stack that^ –

Matches the rhs of a production

-^

Will be a step along the path of producing a correct parse tree

•^

Finding the handle, i.e. identifying when to reduce, is thecentral problem of bottom-up parsing

•^

Note that ambiguous grammars do not fit (as they didn’t fortop down parsing, either) because there may not be a uniquehandle at one step^ –

E.g. dangling else problem

LR Parser •^

A Shift Reduce parser that encodes the stack with astate on the top of the stack^ –

The TOS state and the next input symbol are used to look upthe parser’s actions and goto function from the table

a^

b

eot

Input:

Stack:

X Y Z eot

LR Parser

ParsingTable: M

Output

S1S2S

Types of LR parsers •^

LR parsers can work on more general grammars than LLparsers^ –

Has more history on the stack to make decisions than top-down

•^

LR parsers have different ways to generate the action andgoto tables

•^

Types of parsers listed in order of increasing power (abilityto handle grammars) and decreasing efficiency (size of theparsing tables becomes very large)^ –

LR(0)

–^

SLR

–^

LALR(1)

–^

LR(1)

16

Closure and Goto operations •^

Closure is defined to construct a configurating set for each item. For thestarting item, N

W .Y

-^

N



W .Y is in the set

-^

If Y begins with a terminal, we are done

-^

If Y begins with a non-terminal N’, add all N’ productions with the dot at thestart of the rhs, N’



.Z

•^

For each configurating set and grammar symbol, the goto operation givesanother configurating set.^ –

If a set of items I contains items of the form N



W. x Y, where W and Y

are sequences but x is a single grammar symbol, the goto(I,x) contains

N



W x. Y

•^

To create the family of configurating sets for a grammar, add an initialproduction S’

S, and construct sets from

S’

. S

•^

Use the sets for parser states – states that end with a dot will be reduce

-^

Example pg. 11 of Stanford Notes 9

Limitations of LR(0) •^

Since there is no look-ahead, the parser must know whetherto shift or reduce based on the parsing stack so far^ –

A configurating set can have only shift or reduce and not both basedon the input

•^

Problematic examples^ –

Epsilon rules create shift/reduce conflict if there are other rules

-^

Items like these have shift/reduce conflicts:

• T

id.

• T

id. [ E ]

–^

Items like these have reduce/reduce conflicts

• E

V. = E,

V

id. , T

id.

LR(1) Parsing •^

Although SLR(1) is using 1 lookahead symbol, it is still notusing all of the information that could be obtained in aparsing state by keeping track of what path led to that item

•^

In LR(1) parsing tables, we keep the lookahead in theparsing state and separate those states, so that they can havemore detailed successor states

•^

Lead to larger numbers of states (in thousands, instead ofhundreds) for programming language parsers

•^

Example pg. 7 of Stanford Notes 12

LALR(1) parsing •^

Compromises between the simplicity of SLR and thepowere of LR(1) by merging similar LR(1) states.^ –

Identify a core of configurating sets and merge states that differ onlyby lookahead

  • This is not just SLR because LALR will have fewer reduce

actions, but it may introduce reduce/reduce conflicts that LR(1)did not have

•^

Constructing LALR(1) parsing tables^ –

is not usually done by brute force to construct LR(1) and then mergesets

-^

As configurating sets are generated, a new configurating set isexamined to see if it can be merged with an existing one