Top Down Parsing dokkaras com TopDown Parsing TopDown

  • Slides: 13
Download presentation
Top Down Parsing dokkaras. com

Top Down Parsing dokkaras. com

Top-Down Parsing • Top-Down parsing is an attempt to find a leftmost derivation for

Top-Down Parsing • Top-Down parsing is an attempt to find a leftmost derivation for an input string starting from the start symbol. 1. E E+E 2. E id E=>E+E=>id+id • The input is scanned from left to right. id+id--- is the input terminal string then the left most id is the one that is first considered • Recursive descent is a general form of Top-Down parsing. 2

Recursive Descent Parsers A recursive-descent parsing program consists of a set of procedures, one

Recursive Descent Parsers A recursive-descent parsing program consists of a set of procedures, one for each nonterminal (Variable). void A() { choose an A-production, A X 1 X 2…. . Xk; for (i=1 to k) { if (Xi is a nonterminal) call procedure Xi() else (if Xi equals the current input symbol a) advance the input to the next symbol else an error has occurred

Recursive Descent Parsers-with Backtracking Recursive descent parsing technique may involve backtracking. The productions are

Recursive Descent Parsers-with Backtracking Recursive descent parsing technique may involve backtracking. The productions are applied based on the terminals in the input string from left to right. Example: 1. S a. Ca Input String 2. C bb w = aba 3. C b S S Backtrack S /| a C a a. C a / | b b b 4

Predictive Parsers ( Recursive Descent-No Backtracking) • Prerequisites for predictive parsing(changes made to the

Predictive Parsers ( Recursive Descent-No Backtracking) • Prerequisites for predictive parsing(changes made to the grammar) • Elimination of Left Recursion from the Grammar • Left Factoring the Grammar 5

Left Recursive Grammars- I • A grammar is left recursive if it has a

Left Recursive Grammars- I • A grammar is left recursive if it has a non terminal A such that there is a derivation A Aα, for some string α • Top-down parsers can loop forever when facing a left-recursive rule. A A A α α Therefore, such rules need to be eliminated.

Left Recursive Grammars- II • A left-recursive rule such as A A α |

Left Recursive Grammars- II • A left-recursive rule such as A A α | β can be eliminated by replacing it by: • A β A’ • A’ α A’ | є where A’ is a new non-terminal and є is the empty string Example Left Recursive Grammar 1. E E + T | T 2. T T * F | F 3. F ( E ) | id After elimination of left recursion 1. E T E’ E’ + TE’ | є 2. T F T’ T’ * F T’ | є 3. F (E) | id

Left-Recursive Grammars -III • The general procedure for removing direct left recursion • Group

Left-Recursive Grammars -III • The general procedure for removing direct left recursion • Group the A-rules as A Aα 1 |… | Aαm | β 1 | β 2 |…| βn where none of the β’s begins with A • Replace the original A-rules with • A β 1 A’ | β 2 A’ | … | βn A’ • A’ α 1 A’ | α 2 A’ | … | αm A’|Ɛ 8

Left-Recursive Grammars IV • The procedure discussed will not eliminate indirect left recursion of

Left-Recursive Grammars IV • The procedure discussed will not eliminate indirect left recursion of the kind: S Aa|b A Sd|Ac|є To eliminate the indirect left recursion Arrange the variables in some order A 1, A 2, …An Let us assume the order of the variable in above example is S, A

Left-Recursive Grammars V Example S Aa|b A Sd|Ac|є For i: =1 to n do

Left-Recursive Grammars V Example S Aa|b A Sd|Ac|є For i: =1 to n do begin For j: =1 to i-1 do begin the productions of the form Ai Ajγ where Ai is place higher than Aj in the order are considered A Sd in the example These productions are replaced as follows: Ai δ 1γ| δ 2γ|…| δkγ where Aj δ 1| δ 2|…. | δk are all the current Aj productions A Aad|bd in the example 10

Left-Recursive Grammars VI The productions then become S Aa|b A Aad|bd|Ac|є After eliminating indirect

Left-Recursive Grammars VI The productions then become S Aa|b A Aad|bd|Ac|є After eliminating indirect left recursion After eliminating direct left recursion S Aa|b ; A bd. A’|A’; A’ c. A’|ad. A’|є 11

Left-Factoring a Grammar • There might be a case where the right alternative production

Left-Factoring a Grammar • There might be a case where the right alternative production to use among many for a nonterminal A is not clear. A αβ 1 | αβ 2 … | αβn | γ Example: S i. Et. S | i. Et. Se. S |a • In such cases we rewrite the A productions such that α the common part is separated. That is called as left factoring. A α A’ | γ A’ is a new nonterminal A’ β 1 | β 2 | … | βn • In the previous example the productions are rewritten as follows: S i. Et. SS’|a and S’ ϵ|e. S

Thank You dokkaras. com

Thank You dokkaras. com