0% found this document useful (0 votes)
4 views6 pages

Compiler - Top Downcompiler

The document explains top-down parsing techniques, including recursive descent parsing and backtracking. It covers grammar rules, the parsing process, left recursion, and methods for eliminating left recursion and left factoring to enhance parser efficiency. Additionally, it details how a recursive descent parser operates by translating grammar rules into recursive functions to match input strings.

Uploaded by

aureaura7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Compiler - Top Downcompiler

The document explains top-down parsing techniques, including recursive descent parsing and backtracking. It covers grammar rules, the parsing process, left recursion, and methods for eliminating left recursion and left factoring to enhance parser efficiency. Additionally, it details how a recursive descent parser operates by translating grammar rules into recursive functions to match input strings.

Uploaded by

aureaura7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Top down parser

Top down parser:


A top-down parser starts with the grammar's start symbol and tries to build the
parse tree from the root down, attempting to match the input string.

Grammar:

S -> aB

B -> b

Input String:

ab

Top-Down Recursive Descent Parsing:

1. Procedure S():

o Check if the next input symbol is 'a'.

o If it is, consume 'a' (move to the next input symbol).

o Call procedure B().

2. Procedure B():

o Check if the next input symbol is 'b'.

o If it is, consume 'b'.

o If not, report an error.

Parsing Process:

1. Start with S():

o The next input symbol is 'a'.

o Consume 'a'.

o Call B().

2. B() is called:

o The next input symbol is 'b'.

o Consume 'b'.

3. Input is consumed, and all procedures succeed.

Parse Tree (Conceptual):


S

|\

a B

Top down with backtracking:


Backtracking is a problem-solving technique where you explore possible solutions
step-by-step. If a step leads to a dead end or a failure, you "backtrack" (go back) to
a previous step and try a di erent path.

The Grammar:

 S -> ab | ac

o This grammar says that the symbol 'S' can be replaced by either the
sequence 'ab' OR the sequence 'ac'. This "OR" is the key to needing
backtracking.

The Input:

 ac

o We want to see if this input string can be derived from the starting
symbol 'S' according to our grammar.

The Parsing Process:

1. First Attempt: S -> ab

o The parser starts by trying the first option, S -> ab.

o It looks at the first symbol in the input, which is 'a'. This matches the
'a' in 'ab', so that's good.

o Now, it expects the next input symbol to be 'b'.

o However, the next input symbol is 'c'. This doesn't match 'b', so the
parser has hit a dead end.

o Backtracking: Because this attempt failed, the parser has to go back


to the beginning. It "undoes" the previous steps. It basically says,
"Okay, that didn't work. Let's try something else."
2. Second Attempt: S -> ac

o Now, the parser tries the second option, S -> ac.

o It looks at the first input symbol, which is 'a'. This matches the 'a' in
'ac', so that's good.

o It looks at the next input symbol, which is 'c'. This matches the 'c' in
'ac', so that's also good.

o The entire input string has been matched successfully.

Why Backtracking?

 The parser didn't know which option to choose at first.

 It had to try one option, see if it worked, and if not, try another.

 This "trying and undoing" is what backtracking is.

Left recursion:
Left recursion occurs when a non-terminal symbol (a symbol representing a
grammar rule) can derive a string that begins with itself. Think of it as a circular
dependency in the grammar rules.

Types of Left Recursion:

1. Direct Left Recursion:

o This is the simplest form. A rule directly refers to itself at the


beginning.

o Example: A -> Aa | b

 Here, A can produce Aa, which clearly starts with A.

2. Indirect (or Mutual) Left Recursion:

o This is a bit more subtle. A non-terminal doesn't directly refer to itself,


but it does so through a chain of other non-terminals.

o Example:

 A -> Bc

 B -> Ad

 Here, A derives Bc, and B derives Ad. So, A can eventually


derive Adc, which starts with A.
Eliminating left recursion (direct and indirect)
(That symbol, ε, is pronounced "epsilon." Means empty)

1. Direct Left Recursion Elimination:

 Problem: A rule like A -> Aa | b (A starts with itself).

 Solution:

o Rewrite it as:

 A -> bA'

 A' -> aA' | ε (ε means "empty")

 Explanation:

o We make A start with the part that doesn't cause recursion (b).

o We create a new non-terminal A' to handle the repeating part (a).

2. Indirect Left Recursion Elimination:

 Problem: A chain of rules causes recursion (e.g., A -> Bc, B -> Ad).

 Solution:

o Order the non-terminals: Decide on an order (e.g., A, then B).

o Substitute:

 If a non terminal refers to a non terminal that comes before it in


the ordering, substitute the production of that earlier non
terminal into the later one.

 For example, if we have A -> Bc and B->Ad, and we want to


remove the indirect left recursion, and we have ordered the
non terminals A,B. We would substitute the production of A
into B, resulting in B->Bcd.

o Eliminate direct left recursion: Now, if you have direct left recursion,
use the method from step 1.

 Explanation:

o We rearrange the rules to remove the circular dependency.

o Then, we use the direct left recursion elimination method if needed.


Need for Left Factoring :
Imagine you have grammar rules like:

 A -> apple pie | apple cake

A top-down parser gets confused! It sees "apple" and doesn't know which rule to
pick.

Left factoring fixes this:

 Rewrite it as:

o A -> apple A'

o A' -> pie | cake

Now, the parser sees "apple", knows to go to A', and then decides between "pie" or
"cake".

Why it's needed:

 It removes ambiguity for top-down parsers when multiple rules start the
same way.

 It makes the parsers more predictable and e icient.

Recursive decent parser:


recursive descent parser is a top-down parser where each non-terminal in a
grammar is translated into a recursive function. These functions mimic the
grammar's rules, attempting to match the input string.

Implementation Using Recursive Procedures :


o Grammar:

o S -> aB

o B -> b

o Input:

o "ab"

How it Works :

o Start with "S":

o The parser begins by trying to match the rule for "S".


o The rule says "S" must start with "a".

o The parser checks the input; the first symbol is indeed "a".

o So, the parser "consumes" the "a" (moves on to the next input
symbol).

o Now, according to the rule, "S" must be followed by "B".

o The parser then moves to work on "B".

o Move to "B":

o The rule for "B" says it must be "b".

o The parser checks the next input symbol; it's "b".

o The parser "consumes" the "b".

o The input string has now been fully consumed.

o Success:

Since the parser successfully matched all the rules of the grammar with
the input string, the parse is successful.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy