Complier designer

27
BOTTOM-UP PARSER

Transcript of Complier designer

Page 1: Complier designer

BOTTOM-UP PARSER

Page 2: Complier designer
Page 3: Complier designer

A compiler is a computer program (or set of programs) that transforms source code written in a programming language(the source language) into another computer language. The most common reason for wanting to transform source code is to create an executable program.

Page 4: Complier designer
Page 5: Complier designer
Page 6: Complier designer

Lexical Analysis

Syntax Analysis

Code generation

Code Optimization

Intermediate code generation

Trade Management Error Handing

Source Program

Target Program

Page 7: Complier designer

Lexical Analysis Phase:– The Lexical Analyzer reads the characters in the source program and groups them into a stream of tokens in which each token represents a logically cohesive sequence of characters, such as, An identifier, A keyword, A punctuation 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 a syntactic called an expression.

Intermediate Code Generator:– The Intermediate Code Generator uses the structure produce by the syntax analyzer to create a stream of simple instructions. The primary difference between the intermediate code and assembly code is that the intermediate code need not specify the register to be used for each operation.

Page 8: Complier designer

Code Optimization:– Code Optimization in an optional phase designed to improve the intermediate code so that the ultimate program runs faster. Its output is another intermediate code program that does the same job as the original intermediate code.

Code Generation:– The final phase of the compiler is the generation of target code, consisting normally of relocatable machine code or assembly code. Memory locations are selected for each of the variables used by the program. Then, the each intermediate instruction is translated into a sequence of machine instructions that perform the same task.

Page 9: Complier designer

– A Parser for grammar G is a program that takes as input a string w and produce as output either a parse tree for w, if w is a sentence of G, or an error message indicating that w is not a sentence of G.

Page 10: Complier designer

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 work down to the leaves. In both the cases the input to the parser is being scanned from left to right, considering ONE symbol at a time.

A Bottom-Up Parsers try to find the right-most derivation of the given input in Reverse order.

S ... (the right-most derivation of ) (the bottom-up parser finds the right-most derivation in the reverse order)

Page 11: Complier designer

Bottom-Up Parsing

The bottom-up parsing method is also know as Shift-reduce parsing

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 the non-terminal at the left side of that production.There are also two more actions: accept and error.

Page 12: Complier designer

Shift-Reduce Parsing

There 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.

Page 13: Complier designer

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 to the right side of a production rule is replaced by the non-terminal at the left side of that production rule.

If the substring is chosen correctly, the right most derivation of that string is created in the reverse order.

Rightmost Derivation: S

Shift-Reduce Parser finds: ... S

Page 14: Complier designer

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.

Page 15: Complier designer

HANDLE PRUNING:

A right-most derivation in reverse can be obtained by handle-pruning.

S=0 1 2 ... n-1 n=input string

Start from n, find a handle Ann in n, and replace n in by An to get n-1. Then find a handle An-1n-1 in n-1, and replace n-1 in by An-1 to get n-2. Repeat this, until we reach S.

Page 16: Complier designer

Bottom –Up Parser:

S aABe

A Abc | b

B d

Grammar

The input string

: abbcde.

parse

Consider for Example :-

Page 17: Complier designer

a dbb cINPUT:

Bottom-Up Parsing

Program

e OUTPUT:$

Production

S aABe

A Abc

A b

B d

Bottom-Up Parser Example

Shift a

Page 18: Complier designer

a dbb cINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A

b

$

Production

S aABe

A Abc

A b

B d

Bottom-Up Parser Example

Shift bReduce from b to A

Page 19: Complier designer

a dbA cINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A

b

$

Production

S aABe

A Abc

A b

B d

Bottom-Up Parser Example

Shift A

Page 20: Complier designer

a dbA cINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A

b

$

Production

S aABe

A Abc

A b

B d

Bottom-Up Parser Example

Shift b

Page 21: Complier designer

a dbA cINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A

b

$

Production

S aABe

A Abc

A b

B d

c

A

b

Shift cReduce from Abc to A

Page 22: Complier designer

a dAINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A c

A

b

$

Production

S aABe

A Abc

A b

B d

b

Bottom-Up Parser Example

Shift A

Page 23: Complier designer

a dAINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A c

A

b

$

Production

S aABe

A Abc

A b

B d

b

B

d

Bottom-Up Parser Example

Shift d Reduce from d to B

Page 24: Complier designer

a BAINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A c

A

b

$

Production

S aABe

A Abc

A b

B d

b

B

d

Bottom-Up Parser Example

Shift B

Page 25: Complier designer

a BAINPUT:

Bottom-Up Parsing

Program

e OUTPUT:

A c

A

b

$

Production

S aABe

A Abc

A b

B d

b

B

d

a

S

e

Bottom-Up Parser Example

Shift e Reduce from aABe to S

Page 26: Complier designer

SINPUT:

Bottom-Up Parsing

Program

OUTPUT:

A c

A

b

$

Production

S aABe

A Abc

A b

B d

b

B

d

a

S

e

Bottom-Up Parser Example

Shift S

Hit the target $

Page 27: Complier designer