Category:CSCE 434 Exam 1

From Notes
Jump to navigation Jump to search
Review Slides

« previous | Friday, October 25, 2013 | next »

Location HRBB 126
Date Friday, October 25, 2013
Time 11:30–12:20

Lexical Analysis (Scanning)

Syntax Analysis (Parsing)

LL Parsing

  • Top-down parsers
  • build parse tree from root to leaves

LR Parsing

  • Bottom-up parsers
  • build parse tree from leaves to root

LR(1)

  • scan input from left to right (L)
  • build rightmost derivation in reverse (R)
  • use a single token lookahead to disambiguate (1)
  • Simple, Table-driven, shift-reduce skeleton
  • grammatical knowledge encoded in (parse lookup) tables
  • In general, LR parsers are practical, efficient, and easy to build

Skeleton Parser code:

def lr_parse(toks):
    stack = Stack();
    tokiter = iter(toks)
    tok = tokiter.next()

    while True:
        curr_state = stack.top()

        # Possible values are:
        #   ("shift", state_id),
        #   ("reduce", production{lhs,rhs}), or
        #   ("accept", None)
        behavior, payload = action[curr_state, tok]

        if behavior == "shift":
            next_state = payload

            stack.push(tok)
            stack.push(next_state)
            tok = tokiter.next()

        elif behavior == "reduce":
            A = payload['lhs']
            b = payload['rhs']

            stack.pop(2*len(b))
            curr_state = stack.top()
            stack.push(A)
            push goto[curr_state, A]

        elif behavior == "accept"
            return

        else:
            error()

This has a running time of Θ(k+l), where k is the number of shifts (the length of the input string), and l is the number of reduces (depends on the grammar).


Example Tables

The following grammar:

1 <goal>   ::= <expr>
2 <expr>   ::= <term> + <expr>
3            | <term>
4 <term>   ::= <factor> * <term>
5            | <factor>
6 <factor> ::= id

gets translated into the following action and goto tables:

  action goto
id + * $ (eof) expr term factor
S0 (shift, S4) S1 S2 S3
S1 (accept, _)
S2 (shift, S5) (reduce, expr::=term)
S3 (reduce, term::=factor) (shift, S6) (reduce, term::=factor)
S4 (reduce, factor::=id) (reduce, factor::=id) (reduce, factor::=id)
S5 (shift, S4) S7 S2 S3
S6 (shift, S4) S8 S3
S7 (reduce, expr::=term+expr)
S8 (reduce, term::=factor*term) (reduce, term::=factor*term)


First and Follow Sets

Used to build LR(1) tables.

First Set

Given a terminal or non-terminal grammar symbol α, the first set first(α) is the set of tokens (terminal symbols) that are the first symbol in all possible strings derivable from α.

If α*ϵ, then ϵ is a member of first(α)

Building instructions:

  1. If X is a terminal, then first(X)={X}.
  2. If X::=ϵ, then ϵfirst(X).
  3. If X::=Y1Y2Yk, then put first(Y1) in first(X) (i.e. first(X)first(Y1)).
  4. In the rule above, suppose the first k nonterminals Y1Yk all have ϵ in their first-sets , this tells us that if afirst(Yk+1), then a is a valid member of first(X) and each first(Yi)|i=1k

Note: The definition given here is suitable for LR(1) grammars. In general for LR(k), we define firstk(α) as the leading k tokens (not just 1) that begin strings derived from α.

Follow Set

Given a non-terminal grammar symbol A, the follow set follow(A) is the set of terminals that can appear immediately after A:

Suppose both αAβ and αAγ appear as valid sentential forms (RHS of some other productions). Then β and γ are both members of the follow set of A.

Building instructions:

  1. Place eof in follow(goal) (a.k.a start... same concept)
  2. If A::=αBβ, then all members of first(β)—except ϵ—are also in follow(B).
  3. If A::=αB, then anything that follows A can follow B (i.e. put all members of follow(A) in follow(B))
  4. (A combination of the above two rules) If A::=αBβ and ϵfirst(β), then all members of follow(A) are in follow(B).
Example

In the grammar above, the first and follow sets of each symbol are:

Symbol first follow
goal {id} {eof}
expr {id} {eof}
term {id} {eof,+}
factor {id} {eof,+,*}
+ {+}
* {*}
id {id}


LR(k) Items

Table construction algorithms use LR(k) items to represent the set of possible states in a parse.

an LR(k) item is a pair [α,β] where

  • α is a production rule from G with a at some position in the RHS
  • β is a lookahead string containing k symbols (terminals or eof)

(two cases of interest here are when k=0 and k=1:

  • LR(0) items play a key role in SLR(1) table construction
  • LR(1) items play a role in LR(1) and LALR(1) construction

A sentential form is a partially expanded parse (mixed terminals and nonterminals) that might appear as the RHS of a production rule, for example. In particular, a right-sentential form is a sentential form in which only the right side has been expanded.

A handle is a substring in a rule. For example, A::=β (or simply just β) provides a handle for αβω since β could be reduced to form αAω.

A viable prefix is the prefix of a right sentential form that could potentially appear on the stack of a shift-reduce parser. In other words, it does not continue past the end of the handle for that sentential form. Note that in the example above, ω must consist only of terminal symbols, and viable prefixes consist of initial substrings of αβ.

Table Construction Algorithms

  1. SLR(1)
    • grammar space: smallest class of grammars
    • table size: smallest number of states
    • performance: simple, fast construction
  2. LR(1)
    • grammar space: full set of LR(1) grammars
    • table size: largest number of states
    • performance: slow, large construction
  3. LALR(1)
    • grammar space: intermediate sized set of grammars
    • table size: same as SLR(1) — small
    • performance: canonical construction is slow and large

Better construction techniques exist

Media in category "CSCE 434 Exam 1"

The following 13 files are in this category, out of 13 total.