Top Down Parsing, Predictive Parsing

Tanzeela_Hussain 23,937 views 35 slides Feb 06, 2016
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

Parsing, Top Down parsing, Predictive Parsing


Slide Content

Group Members: Hira Shahzad Javeria Khalid Tanzeela Hussain Presented To: Ms. Sania Batool

Compiler Construction

parsing The term parsing comes from Latin pars meaning “ part ”. Parsing is a process that constructs a syntactic structure (i.e. parse tree) from the stream of tokens . Parsing is the process of determining if a string of tokens can be generated by a grammar . For any context-free grammar there is a parser that takes at most Ο(n 3 ) time to parse a string of n tokens. Parsing a string with a CFG: Finding a derivation of the string consistent with the grammar The derivation gives us a PARSE TREE

Parsing techniques Syntax analyzers follow production rules defined by means of context-free grammar . The way the production rules are implemented (derivation) divides parsing into two types : Top-down parsing and Bottom-up parsing.

Types of parsing Parsing Bottom-up parsing Top-Down Parsing Predictive parsing Recursive decent parsing Recursive predictive parsing Non-Recursive predictive parsing Shift reduce parsing LALR parsing Canonical parsing SLR parsing Operator Precedence parsing

TOP DOWN PARSING   Top-down parsers  build parse trees from the  top (root)  to the  bottom (leaves) . A top-down parse corresponds to a preorder traversal of the parse tree ­ A leftmost derivation is applied at each derivation step Top-Down Parsing may need to backtracking Two top-down parsing are further sub-divided into the following categories:    Predictive Parsing . Recursive Descent Parsing

Example: Consider the following Grammar: < program>  begin < stmts > end $ < stmts >  SimpleStmt ; < stmts > < stmts >  begin < stmts > end ; < stmts > < stmts >  € Input : begin SimpleStmt ; SimpleStmt ; end $

recursive decent parser This parsing technique recursively parses the input to make a parse tree A recursive-descent parser consists of several small functions, one for each nonterminal in the grammar  A procedure is associated with each nonterminal of a grammar .. Recursive descent parsing involves backtracking . For an input string: read S → rXd X → oa X → ea

Predictive parsing Predictive parser, has the capability to predict which production is to be used to replace the input string. The predictive parser does not suffer from backtracking . T he predictive parser uses a look-ahead pointer, which points to the next input symbols. To make the parser back-tracking free, the predictive parser puts some constraints on the grammar. It accepts only a class of grammar known as LL(k) grammar . Hence, Predictive Parser is also known as LL(1) Parser .

LL(1) Parser LL(1) Parser accepts LL(1) grammar. LL(1) grammar is a subset of context-free grammar but with some restrictions to get the simplified version In LL(1) parser, t he first L in LL(1) is parsing the input from left to right, the second L in LL(1) stands for left-most derivation and the 1 means one input symbol of look ahead.

Constructing predictive parser Following are the steps for constructing predictive parser. Removing unreachable productions. Removing ambiguity from the Grammar. Eliminating left recursion. Left Factoring of a grammar. First and Follow Constructing a parse table

Removing unreachable productions An unreachable production is one that cannot possibly appear in the parse tree rooted at the start symbol. For example, in the following grammar : S  A (1) A a (2) B b (3) Production (3) is unreachable because the non-terminal B does not appear on the right side of any production. A non-terminal can be unreachable either it appears on the right side of any production. if it is on the right side of unreachable non-terminal.

Data Structures: A stack A List for Reachable Non-Terminals Method: Initially both the stack and list are Empty. Step 1: Start symbol to the list of reachable non-terminal also push onto the stack . Step 2: While (The stack is not Empty) { P= POP one Item of the stack for (Each non-terminal X on right hand side are P) { If (X is not in the list of reachable non -terminals ) { Push X; Add X to the list of Reachable non-terminal; } } } Step 3: Remove all the productions from the grammar where L-H-S is not in the list of reachable non –terminals. Algorithm to remove unreachable production

Grammer : S  aB | bA A a | bAA | aS B b | aBB | bS C aD | bS | € D bD | € After Removing Unreachable Productions we have : S  aB | bA A a | bAA | aS B b | aBB | bS

Eliminating ambiguity A grammar that produces more than one parse tree for some sentence (input string) is said to be ambiguous. Ambiguity can be remove only by constructing a new grammar. Note: For left associative, replace right non-terminal. For right associative, replace left no-terminal. If a grammar contains more than one operators, ambiguity will be removed first from the production involving the operator having the lowest precedence.

Example: S  S+S | S-S | S*S | S/S | NUM The operators with lower precedence will deal first: S  S+S’ | S-S’ | S’ S’  S | S*S | S/S | NUM Replace S by S’ from R-H-S S’  S’ | S’*S’ | S’/S’ | NUM After Eliminating the redundant productions i -e S’  S’ , We will get : S’  S ’*S’ | S’/S’ | NUM  S’  S’*S ’’ | S’/S ’’ | S’’ S’’ S’ | NUM Replace again S’ by S’’ from R-H-S S’’ S ’’ | NUM After Eliminating the redundant productions i -e S ’’  S ’’ , We will get : S ’’ NUM

Unambiguous Grammar: S  S+S’ | S-S’ | S’ S ’  S’*S’’ | S’/S’’ | S’’ S ’’ NUM

Transition diagrams Transition diagrams can describe predictive parsers, just like they can describe lexical analyzers, but the diagrams are slightly different . For Predictive Parser For Lexical Analyzer There is one diagram foe Every Non-terminal There is one Diagram for the Entire language construct. The labels of edge was terminals and Non-terminals. The label of edges are only terminals.

Construction Eliminate left recursion from G Left factor G For each non-terminal A, do Create an initial and final (return) state For each production A -> X1 X2 … Xn , create a path from the initial to the final state with edges X1 X2 … Xn . Simplify the transition Diagram, if possible.

Example of transition diagrams An expression grammar with left recursion and ambiguity removed: E -> T E’ E’ -> + T E’ | ε T -> F T’ T’ -> * F T’ | ε F -> ( E ) | id Corresponding transition diagrams

Types of predictive parsing Following are the two types of Predictive Parsing: Recursive Predictive Parsing Non-Recursive Predictive Parsing Here, we discuss in Detail Non-Recursive Predictive Parsing Technique.

Non-recursive predictive parsing A non-recursive  predictive parser  is an efficient way of implementing by handling the stack of activation records explicitly.

Cont.. The predictive parser has an input , a stack, a parsing table , and an output . The input contains the string to be parsed, followed by $, the right end marker . The stack contains a sequence of grammar symbols, preceded by $, the bottom-of-stack marker . Initially the stack contains the start symbol of the grammar preceded by $. The parsing table is a two dimensional array M[A ,a] , where A is a nonterminal, and a is a terminal or the symbol $. The parser is controlled by a program that behaves as follows: The program determines  X , the symbol on top of the stack, and a, the current input symbol . These two symbols determine the action of the parser .

Cont.. There are three possibilities: If  X  = a = $, the parser halts and announces successful completion of parsing . If  X  = a ≠ $, the parser pops  X  off the stack and advances the input pointer to the next input symbol. If   X  is a nonterminal , the program consults entry M[ X , a] of the parsing table M. This entry will be either an X-production of the grammar or an error entry. §   If M[ X , a ] = { X  →  UVW }, the parser replaces  X  on top of the stack by  WVU  (with  U  on top). §  If M[ X , a ] = error, the parser calls an error recovery routine.

Predictive parsing algorithm repeat       begin             let  X  be the top stack symbol and a the next input symbol;             if   X  is a terminal or $ then                     if   X  =  a  then                           pop  X  from the stack and remove a from the input                    else                         ERROR( )   else  /*  X  is a nonterminal */                    if   M[ X , a ] =  X   →   Y 1 , Y 2 , … , Y k   then                          begin                              pop  X  from the stack;                              push  Y k , Y k-1 , … ,Y 1  onto the stack ,  Y 1  on top                          end                     else                         ERROR( )        end until       X  = $ /* stack has emptied */

Example: Use the table-driven predictive parser to parse id + id * id Assuming parsing table Initial stack is $E Initial input is id + id * id $ Grammar: E  TE’ E’  +TE’ | € T  FT’ T’  *FT’ | € F  (E) | id

  Error recovery in predictive parsing An error is detected during predictive parsing when the terminal on top of the stack does not match input symbol or when nonterminal A is on top of the stack, a is the next input symbol, and the parsing table entry M[A , a ] is empty . Following two Error Recovery Routines are handled in predictive parser. Panic-mode error recovery : It is based on the idea of skipping symbols on the input until a token in a selected set of synchronizing tokens appears. Its effectiveness depends on the choice of synchronizing set . The sets should chosen so that the parser recovers quickly from errors that are likely to occur in practice.

Cont.. Phrase=level recovery: §  It is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. §  These routines may change, insert, or delete symbols on the input and issue appropriate error messages. §  They may also pop from the stack. §  In any event, sure that there is no possibility of an infinite loop. §  Checking that any recovery action eventually results in an input symbol being consumed is a good way to protect against such loops.

Difference between Predictive parser and recursive decent parser Predictive Parsing Recursive-Descent Parsing Its Non-Recursive (Table Driven) Predictive Parser, which is also known as LL(1) parser Its has a set of recursive procedures to process the input. No backtracking is Needed. Backtracking is needed. Needs a special form of grammars (LL(1) grammars) and Widely used It is a general parsing technique, but not widely used. Its an efficient technique. Its n ot an efficient Technique. Predictive parsers operate in linear time It operates in exponential time

Bottom-up Parsing B ottom-up parsing starts with the input symbols and tries to construct the parse tree up to the start symbol . Example: Input string : a + b * c Production rules: S → E E → E + T E → E * T E → T T → id

example Let us start bottom-up parsing a + b * c Read the input and check if any production matches with the input : a + b * c T + b * c E + b * c E + T * c E * c E * T E S