Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Compiler Design  >  Introduction & Difference Between Top Down & Bottom Up Parsing

Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE) PDF Download

BOTTOM UP PARSING:
Bottom-up parser builds a derivation by working from the input sentence back towards the start symbol S. Right most derivation in reverse order is done in bottom-up parsing.
(The point of parsing is to construct a derivation. A derivation consists of a series of rewrite steps)
S⇒r0⇒r1⇒r2⇒- - - ⇒rn-1⇒rn⇒sentence            
Bottom-up

Assuming the production A →b, to reduce ri ri-1 match some RHS b against ri then replace b with its corresponding LHS, A.

In terms of the parse tree, this is working from leaves to root.

Example – 1: S→if E then S else S/while

 E do S/ print E→ true/ False/id

 Input: if id then while true do print else print.


Parse tree:
Basic idea: Given  input  string  a,  “reduce”  it  to  the  goal  (start)  symbol,  by  looking  for substring that match production RHS.
 

                                        Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE)

⇒ if E then S else S  
lm    
⇒ if id then S else S  
lm    
⇒ if id then while E do S else S  
lm    
⇒ if id then while true do S else S  
lm    
⇒ if id then while true do print else S  
lm    
⇒ if id then while true do print else print  
lm    
⇒ if E then while true do print else print  
rm    
⇒ if E then while E do print else print    
rm  
⇒ if E then while E do S else print  
rm
⇒ if E then S else print  
rm    
⇒ if E then S else S  
rm    
S  
rm

Topdown Vs Bottom-up parsing:

Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE)
 

→ Bottom-up can parse a larger set of languages than topdown.    
→Both work for most (but not all) features of most computer languages.  

Example – 2:

                                                                                          Right-most derivation

S → aAcBe          llp  : abbcde/ S              → aAcBe
A→ Ab/b                                                    →aAcde
B→d
                                                                   →aAbcde
                                                                   →abbcde


Bottom-up approach

Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE)
 

Steps correspond to a right-most derivation in reverse.

(must choose RHS wisely)
Example – 3:
 S →aABe
 A →Abc/b
 B →d
 1/p:abbcde 

Right most derivation:
S →aABe
→aAde Since ( ) B →d
→aAbcde Since ( )A →Abc
→abbcde Since ( ) A →b
Parsing using Bottom-up approach:
 

Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE)
 

S parsing is completed as we got a start symbol Hence the 1/p string is acceptable.

Example – 4 E→E+E

E→E*E

E→(E)

E→id 1/p: id1+id2+id3
 

Right most derivation 
E →E+E 
→E+E*E 
→E+E*id3 →E+id2*

id3 →id1+id2*id3

Parsing using Bottom-up approach:

Go from left to right
id1+id2*id3
E+id2*id3                                                               
E+E*id3
E*id3
       
         E*E
E→id
E→id
E→E+E
E→id
E
                       

= start symbol, Hence acceptable.

The document Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Introduction & Difference Between Top Down & Bottom Up Parsing - Compiler Design - Computer Science Engineering (CSE)

1. What is the difference between top-down and bottom-up parsing?
Ans. Top-down parsing involves starting with the root node and breaking it down into smaller sub-trees, while bottom-up parsing starts with the smallest sub-trees and combines them to form larger sub-trees and eventually the root node. In other words, top-down parsing is a process of constructing a parse tree from the top to the bottom, whereas bottom-up parsing is a process of constructing a parse tree from the bottom to the top.
2. What is the advantage of top-down parsing over bottom-up parsing?
Ans. Top-down parsing has the advantage of being able to detect syntax errors early in the parsing process, as it starts at the top of the parse tree and works its way down. This allows the parser to quickly identify any errors in the input and stop parsing before wasting resources on invalid code. Bottom-up parsing, on the other hand, can only detect syntax errors once it has constructed the entire parse tree, which can be a more time-consuming process.
3. What is backtracking in top-down parsing?
Ans. Backtracking in top-down parsing refers to the process of undoing a decision made by the parser and exploring an alternative path in the parse tree. This is necessary when the parser encounters a non-terminal symbol that can be expanded into multiple productions, but none of the productions match the input. The parser must then backtrack to the previous decision point and try a different production. Backtracking can be time-consuming and can lead to inefficient parsing if not implemented correctly.
4. What is the difference between LL and LR parsing?
Ans. LL parsing is a top-down parsing technique that uses a left-to-right scan of the input and leftmost derivation to construct a parse tree. It is called LL because it reads the input from left to right and constructs a leftmost derivation. LR parsing, on the other hand, is a bottom-up parsing technique that uses a right-to-left scan of the input and a rightmost derivation to construct a parse tree. It is called LR because it reads the input from left to right and constructs a rightmost derivation. LL parsing is generally simpler and easier to understand, while LR parsing is more powerful and can handle a wider range of grammars.
5. What is the difference between parsing and lexical analysis?
Ans. Parsing and lexical analysis are two distinct phases of the compiler design process. Lexical analysis is the process of breaking up the input stream of characters into meaningful tokens, which are then passed on to the parser for further processing. The lexer, or scanner, is responsible for identifying the different types of tokens in the input, such as keywords, identifiers, constants, and operators. Parsing, on the other hand, is the process of analyzing the syntactic structure of the input stream and constructing a parse tree that represents the relationships between the different tokens. The parser is responsible for enforcing the grammar rules of the language and identifying any syntax errors in the input.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Important questions

,

Exam

,

past year papers

,

Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE)

,

pdf

,

ppt

,

Extra Questions

,

Summary

,

Free

,

Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE)

,

mock tests for examination

,

MCQs

,

Semester Notes

,

Previous Year Questions with Solutions

,

study material

,

Introduction & Difference Between Top Down & Bottom Up Parsing | Compiler Design - Computer Science Engineering (CSE)

,

video lectures

,

practice quizzes

,

shortcuts and tricks

,

Sample Paper

,

Viva Questions

,

Objective type Questions

;