BOTTOM-UP PARSER
A compiler is a computer program (or set ofprograms) that transforms source codewritten in a programminglanguage(the sourc...
Source Program                    Lexical Analysis                    Syntax Analysis                   Intermediate codeT...
Lexical Analysis Phase:–  The Lexical Analyzer reads the characters in the source program andgroups them into a stream of ...
Code Optimization:–   Code Optimization in an optional phase designed to improve theintermediate code so that the ultimate...
–   A Parser for grammar G is a program that takes as input a string w andproduce as output either a parse tree for w, if ...
There are Two basic types of Parsers:a) Bottom-upb) Top-down–    As name indicates Bottom-Up Parsers build parse trees fro...
Also Know as                                             “Shift-reduce”                                                 pa...
Shift-Reduce ParsingThere are four possible action of a shift-parse action are as follows:-     1. Shift : The next input ...
A shift-reduce parser tries to reduce the given input string   into the starting symbol.        A string reduced to () th...
HANDLE• Informally, a handle of a string is a substring that matches the right  side of a production rule.   – But not eve...
HANDLE PRUNING:A right-most derivation in reverse can be obtained by handle-pruning.       S=0  1  2  ...  n-1  ...
Bottom –Up Parser: Consider for Example :-     Grammar                   S  aABe                   A  Abc | b           ...
Bottom-Up Parser Example                           Shift a                 INPUT:     a    b   b   c   d   e   $   OUTPUT:...
Bottom-Up Parser Example                           Shift b                           Reduce from b to A                 IN...
Bottom-Up Parser Example                           Shift A                 INPUT:    a    A    b   c   d   e   $   OUTPUT:...
Bottom-Up Parser Example                           Shift b                 INPUT:    a    A    b   c   d   e   $   OUTPUT:...
Shift c                       Reduce from Abc to A             INPUT:   a   A   b   c   d       e   $   OUTPUT:Production ...
Bottom-Up Parser Example                           Shift A                 INPUT:    a    A    d   e   $      OUTPUT:  Pro...
Bottom-Up Parser Example                           Shift d                           Reduce from d to B                 IN...
Bottom-Up Parser Example                           Shift B                 INPUT:    a    A    B   e   $       OUTPUT:  Pr...
Bottom-Up Parser Example                           Shift e                           Reduce from aABe to S                ...
Bottom-Up Parser Example                           Shift S                           Hit the target $                 INPU...
Complier designer
Complier designer
Complier designer
Complier designer
Upcoming SlideShare
Loading in …5
×

Complier designer

585 views
506 views

Published on

Published in: Education
1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
585
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
60
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Complier designer

  1. 1. BOTTOM-UP PARSER
  2. 2. A compiler is a computer program (or set ofprograms) that transforms source codewritten in a programminglanguage(the source language) into anothercomputer language. The most commonreason for wanting to transform source codeis to create an executable program.
  3. 3. Source Program Lexical Analysis Syntax Analysis Intermediate codeTrade Management Error Handing generation Code Optimization Code generation Target Program
  4. 4. Lexical Analysis Phase:– The Lexical Analyzer reads the characters in the source program andgroups them into a stream of tokens in which each token represents a logicallycohesive sequence of characters, such as, An identifier, A keyword, Apunctuation character.Syntax Analysis Phase:– The Syntax Analyzer groups tokens together into syntactic structure.Consider, for Ex: the three tokens representing A+B might be grouped into asyntactic called an expression.Intermediate Code Generator:– The Intermediate Code Generator uses the structure produce by the syntaxanalyzer to create a stream of simple instructions. The primary differencebetween the intermediate code and assembly code is that the intermediatecode need not specify the register to be used for each operation.
  5. 5. Code Optimization:– Code Optimization in an optional phase designed to improve theintermediate code so that the ultimate program runs faster. Its output isanother intermediate code program that does the same job as the originalintermediate code.Code Generation:– The final phase of the compiler is the generation of target code, consistingnormally of relocatable machine code or assembly code. Memory locations areselected for each of the variables used by the program. Then, the eachintermediate instruction is translated into a sequence of machine instructionsthat perform the same task.
  6. 6. – A Parser for grammar G is a program that takes as input a string w andproduce as output either a parse tree for w, if w is a sentence of G, or an errormessage indicating that w is not a sentence of G.
  7. 7. There are Two basic types of Parsers:a) Bottom-upb) Top-down– As name indicates Bottom-Up Parsers build parse trees from the Bottom(leaves) to the Top (root), while top-down parsers starts with the root and workdown to the leaves. In both the cases the input to the parser is being scannedfrom left to right, considering ONE symbol at a time. A Bottom-Up Parsers try to find the right-most derivation of the given inputin Reverse order. S  ...   (the right-most derivation of )  (the bottom-up parser finds the right-most derivation in the reverse order)
  8. 8. Also Know as “Shift-reduce” parsingBottom-Up Parsing The bottom-up parsing method is also know as Shift-reduce parsing because its consist of two main actions. i.e shift and reduce.At each shift action, the current symbol in the input string is pushed to a stack.At each reduction step, the symbols at the top of the stack will replaced by thenon-terminal at the left side of that production.There are also two more actions: accept and error.
  9. 9. Shift-Reduce ParsingThere are four possible action of a shift-parse action are as follows:- 1. Shift : The next input symbol is shifted onto the top of the stack. 2. Reduce: Replace the handle on the top of the stack by the non-terminal. 3. Accept: Successful completion of parsing. 4. Error: Parser discovers a syntax error, and calls an error recovery routine.
  10. 10. A shift-reduce parser tries to reduce the given input string into the starting symbol. A string reduced to () the starting symbol At each reduction step, a substring of the input matching tothe right side of a production rule is replaced by the non-terminal atthe left side of that production rule. If the substring is chosen correctly, the right mostderivation of that string is created in the reverse order. Rightmost Derivation: S Shift-Reduce Parser finds:   ...  S
  11. 11. HANDLE• Informally, a handle of a string is a substring that matches the right side of a production rule. – But not every substring matches the right side of a production rule is handle• A handle of a right sentential form  ( ) is a production rule A   and a position of  where the string  may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of . S  A  • If the grammar is unambiguous, then every right-sentential form of the grammar has exactly one handle.• We will see that  is a string of terminals.
  12. 12. HANDLE PRUNING:A right-most derivation in reverse can be obtained by handle-pruning. S=0  1  2  ...  n-1  n=  input stringStart from n, find a handle Ann in n,and replace n in by An to get n-1.Then find a handle An-1n-1 in n-1,and replace n-1 in by An-1 to get n-2.Repeat this, until we reach S.
  13. 13. Bottom –Up Parser: Consider for Example :- Grammar S  aABe A  Abc | b B d parse The input string : abbcde.
  14. 14. Bottom-Up Parser Example Shift a INPUT: a b b c d e $ OUTPUT: Production S  aABe Bottom-Up Parsing A  Abc Program A b Bd
  15. 15. Bottom-Up Parser Example Shift b Reduce from b to A INPUT: a b b c d e $ OUTPUT: Production S  aABe Bottom-Up Parsing A  Abc A Program A b Bd b
  16. 16. Bottom-Up Parser Example Shift A INPUT: a A b c d e $ OUTPUT: Production S  aABe Bottom-Up Parsing A  Abc A Program A b Bd b
  17. 17. Bottom-Up Parser Example Shift b INPUT: a A b c d e $ OUTPUT: Production S  aABe Bottom-Up Parsing A  Abc A Program A b Bd b
  18. 18. Shift c Reduce from Abc to A INPUT: a A b c d e $ OUTPUT:Production AS  aABe Bottom-Up Parsing A  Abc A b c Program A b Bd b
  19. 19. Bottom-Up Parser Example Shift A INPUT: a A d e $ OUTPUT: Production A S  aABe Bottom-Up Parsing A  Abc A b c Program A b Bd b
  20. 20. Bottom-Up Parser Example Shift d Reduce from d to B INPUT: a A d e $ OUTPUT: Production A B S  aABe Bottom-Up Parsing A  Abc A b c d Program A b Bd b
  21. 21. Bottom-Up Parser Example Shift B INPUT: a A B e $ OUTPUT: Production A B S  aABe Bottom-Up Parsing A  Abc A b c d Program A b Bd b
  22. 22. Bottom-Up Parser Example Shift e Reduce from aABe to S INPUT: a A B e $ OUTPUT: S Production e a A B S  aABe Bottom-Up Parsing A  Abc A b c d Program A b Bd b
  23. 23. Bottom-Up Parser Example Shift S Hit the target $ INPUT: S $ OUTPUT: S Production e a A B S  aABe Bottom-Up Parsing A  Abc A b c d Program A b Bd b

×