This document discusses top-down table-driven parsing for compilers. It describes how top-down parsers use a stack and production table to determine which grammar rules to apply based on the top of the stack and next input symbol. The document provides an example grammar for expressions and shows its parsing table and how a predictive parser would operate on a sample input. It also explains how to construct the LL parsing table from a grammar's First and Follow sets.
This document discusses top-down table-driven parsing for compilers. It describes how top-down parsers use a stack and production table to determine which grammar rules to apply based on the top of the stack and next input symbol. The document provides an example grammar for expressions and shows its parsing table and how a predictive parser would operate on a sample input. It also explains how to construct the LL parsing table from a grammar's First and Follow sets.
This document discusses top-down table-driven parsing for compilers. It describes how top-down parsers use a stack and production table to determine which grammar rules to apply based on the top of the stack and next input symbol. The document provides an example grammar for expressions and shows its parsing table and how a predictive parser would operate on a sample input. It also explains how to construct the LL parsing table from a grammar's First and Follow sets.
Kanat Bolazar February 9, 2010 2 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 3 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 and doing left-factoring may produce one Not all context free languages can have an LL(k) grammar Shift-reduce parsers are bottom-up parsers, sometimes called LR(k) Scan the input from Left to Right Produce a Rightmost derivation from the grammar Not all context free languages have LR grammars
4 (Non-recursive) Predictive Parser Replaces non-terminals on the top of the stack with the rhs of a production that can match the next input. a + b eot Input: Stack: X Y Z eot Predictive Parsing Program Parsing Table: M Output 5 Parsing Algorithm The parser starts in a configuration with S <eot> on the stack Repeat: let X be the top of stack symbol, a is the next symbol if X is a terminal symbol or <eot> if (X = a) then pop X from the stack and getnextsym else 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 6 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
7 Parsing Table The table is indexed by the set of non-terminals in one direction and the set of terminals in the other Any blank entries represent error states Non LL(1) grammars could have more than one rule in a table entry id + * ( ) Eot E E T E E T E E E+ T E E E T T F T T F T T T T F T T T F F id F ( E ) 8 Stack Input Output $E $ET $ETF $ETid $ET $E $ET + $ET $ET F $ET id $ET $ET F * $ET F $ET id $ET $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 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 9 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, eot] All other entries are errors