3. 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.
4.
5.
6. Source Program
Lexical Analysis
Syntax Analysis
Intermediate code
Trade Management Error Handing
generation
Code Optimization
Code generation
Target Program
7. 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.
8. 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.
9. – 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.
10. There are Two basic types of Parsers:
a) Bottom-up
b) 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)
11. Also Know as
“Shift-reduce”
parsing
Bottom-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 the
non-terminal at the left side of that production.
There are also two more actions: accept and error.
12. 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.
13. 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
14. 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.
15. 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.
16. Bottom –Up Parser:
Consider for Example :-
Grammar
S aABe
A Abc | b
B d
parse
The input string
: abbcde.
17. 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
Bd
18. 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
Bd b
19. 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
Bd b
20. 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
Bd b
21. Shift c
Reduce from Abc to A
INPUT: a A b c d e $ OUTPUT:
Production
A
S aABe
Bottom-Up Parsing
A Abc A b c
Program
A b
Bd b
22. 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
Bd b
23. 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
Bd b
24. 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
Bd b
25. 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
Bd b
26. 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
Bd b