Loading [MathJax]/jax/output/HTML-CSS/jax.js
Close
Close Window

OpenDSA Complete Catalog

Chapter 41 Parsing

| About   «  41.1. Parsing Introduction   ::   Contents   ::   41.3. LR Parsing  »

41.2. LL Parsing

41.2.1. LL Parsing

41.2.1.1. LL(k) Parser

  • Top-down parser: starts with start symbol on stack, and repeatedly replace nonterminals until string is generated.

  • Predictive parser: predict next rewrite rule
    NOTE: use lookahead for this
  • First L of LL means that we read input string left to right

  • Second L of LL means that we produce the leftmost derivation

    Note

    ASK TO SEE IF THEY KNOW WHAT THIS IS

  • k: number of lookahead symbols used.
    Sometimes more than one symbol is needed

41.2.1.2. LL parsing process

  • Convert CFG to PDA (different method than before)

  • Use the PDA and lookahead symbols

  • Lookahead symbol is next symbol in input string

Notes:

  • The PDA is nondeterministic, so we will lookahead to the next input symbol and use it to determine which rewrite rule to use.

  • Nondeterministic, could use back-tracking, but this could take forever.

  • Remember: cannot necessarily construct a deterministic PDA from a NPDA.

41.2.1.3. Convert CFG to NPDA

NOTE: This is not the same construction method we used before. This method will apply to any CFG, even those that are not in GNF.

Idea: To derive a string with a CFG, start with the start symbol and repeatedly apply production rules until the string is derived. In order to simulate this process with an NPDA, start by pushing the start symbol on the stack. Whenever a production rule Aw would be applied, the variable A should be on top of the stack. A is popped (or replaced) and the right hand side of the rule, w, is pushed onto the stack. Whenever a terminal is on top of the stack, if it matches the next symbol in the input string, then it is popped from the stack. If it does not match, then this string is not in the language of the grammar. If starting with the start symbol S, one can apply replacement rules, match all the terminals in the input string and empty the stack, then the string is in the language.

Note

Just mention this stuff and then draw NPDA.

The constructed NPDA:

  • Three states: s,q,f
    As usual, start in state s
    Push S on stack, move into q
    All rewrite rules in state q: If left-hand-side of rewrite rule on top of stack, replace it with right-hand-side of rewrite rule and stay in state q
    Additional rules in q to recognize terminals: Read input symbol, pop input symbol, stay in state q
    Pop z from stack, move into f, accept

41.2.1.4. LL Parse Table: 2-dimensional array

When the grammar is large, the parsing routine will have many cases. Alternatively, store the information for which rule to apply in a table.

  • Rows: variables

  • Columns: terminals, $ (end of string marker)

  • LL[i,j] contains the right-hand-side of a rule. This right-hand-side is pushed onto the stack when the left-hand-side of the rule is the variable representing the i th row and the lookahead is the symbol representing the j th column.

  • If we can specify any CFG by this type of parse table, then we can use a generic parser to determine if strings are in this language.

  • Gets rid of use of states

41.2.1.5. A generic parsing routine

Idea: To replace a variable on the top of the stack with its appropriate right-hand-side, use the lookahead and the left-hand-side to look up the right-hand-side in the LL parse table. (LL[,] is the parse table.):

push(S)
read(symbol)                                         obtain the lookahead symbol
while stack not empty do
   case top-of-stack of
      terminal:
         if top-of-stack == symbol
            then { pop(); read(symbol) }             pop terminal and get next lookahead
         else
            error
      variable:
         if LL[top-of-stack, symbol] <> error
            then { pop(),                            pop the lhs
                   push(LL[top-of-stack,symbol]) }   push the rhs
            else
               error
      end case
end while
if symbol <> $, then error

Note

For previous example, try the following traces:

Parse the string: aabbb

Parse the string: b

We will use the following functions FIRST and FOLLOW to aid in computing the table.

41.2.1.6. To construct an LL parse table LL[rows,cols]

Note

Refresh memory as to what parse table is.

  1. For each rule Aw

    1. For each a in FIRST(w)
      add w to LL[A,a]
    2. If λ is in FIRST(w)
      add w to LL[A,b] for each b in FOLLOW(A)
      where bT{$}
  2. Each undefined entry is an error.

Comments:

There are some CFL’s that have no LL(k) Parser

There are some languages for which some grammars have LL(k) parsers and some don’t.

   «  41.1. Parsing Introduction   ::   Contents   ::   41.3. LR Parsing  »

Close Window