Professional Documents
Culture Documents
Unit I
Unit I
Ratish Srivastava
Asst. Prof.
CSE Dept.
UCER, Prayagraj
Introduction
Translator:
• A program written in high-level language is called as source
code. To convert the source code into machine code, translators
are needed.
• A translator takes a program written in source language as input
and converts it into a program in target language as output.
Purpose of Translator
• Translating the high-level language program input into an
equivalent machine language program.
• Providing diagnostic messages wherever the programmer
violates specification of the high-level language program.
• Different Types of Translators
Compiler, Interpreter and Assembler
Compiler Design, KCS-502 2
Introduction
• Compiler:
– 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 target language,
often having a binary form known as object code)
Source Target
Compiler
Program Program
Errors
Fig.: A Compiler
Source Program
Interpreter Output
Input
Fig.: An Interpreter
Preprocessor
Source program
Compiler
Assembler
Tokens
Code Optimizer
Parser
[Syntax Analyzer]
Optimized Intermediate Code
Parse tree
Code Generator
Semantic Process
[Semantic analyzer] Target machinecode
– The syntax analysis can find out that the tokenized version
of the source program cannot be parsed by the
grammatical rules.
• Example:
1 position real var
2 initial real var
3 rate real var
4 60 int const
Compiler Design, KCS-502 31
Grouping of Phases into Passes
• The discussion of phases deals with the logical organization of a
compiler. In an implementation, activities from several phases may
be grouped together into a pass that reads an input file and writes
an output file.
• Then the syntax of the line is analyzed and the tree structure and
some tables containing information about each token are built.
• The same process is repeated for each line of code until the entire
program is compiled.
Solution:
We have the input alphabets are ∑ = {a, b, c}
The objective of the problem is to find out the regular
expression for all strings containing exactly one ‘a’.
For this, first find out the regular expression for any strings at
all over the given ∑, which does not contain any ‘a’. It is-
(b + c)*
Thus, to write a regular expression which denote all string
containing exactly on ‘a’ we simply put the regular expression
for any string at all which does not contain any ‘a’ on the both
side of ‘a’. Thus, the resultant regular expression is-
(b + c)* a (b + c)*
Compiler Design, KCS-502 51
Regular expression examples
Solution:
We have the input alphabets are ∑ = {a, b}
According to the problem given, the resultant regular expression
represent all strings which have three consecutive b’s (which means,
strings always have a substring of b’s of length 3 i.e bbb) over the
given input alphabets ∑ = {a, b}.
The regular expression for any string at all over the given input
alphabets ∑ = {a, b}. It is (a + b)*
Thus, to write a regular expression which denote all string with three
consecutive b’s we simply put the regular expression for any string at
all over the given ∑ on the both side of three consecutive b’s, i.e., bbb.
Thus, the resultant regular expression is-
(a + b)* bbb (a +b)*
Solution:
The regular expression which fulfill the
requirement of given question can be written as-
00 (0 + 1)*
Solution:
The regular expression which fulfill the
requirement of given question can be written as-
1 (0 + 1)* 00
Solution:
The regular expression which fulfill the
requirement of given question can be written as-
(0 + 1)*(010 + 0010)
Step-02:
• Add start state of the NFA to Q’.
• Add transitions of the start state to the transition table T’.
• If start state makes transition to multiple states for some input
alphabet, then treat those multiple states as a single state in
the DFA.
Step-04:
• Keep repeating Step-03 until no new state is present in the
transition table T’.
• Finally, the transition table T’ so obtained is the complete
transition table of the required DFA.
State / Alphabet a b
→q0 q0 q0, q1
q1 – *q2
*q2 – –
Step-02:
Add transitions of start state q0 to the transition table T’.
State / Alphabet a b
State / Alphabet a b
State / Alphabet a b
State / Alphabet a b
Step-01:
Eliminate all the dead states and inaccessible states from
the given DFA (if any).
• Dead State
All those non-final states which transit to itself for all
input symbols in ∑ are called as dead states.
• Inaccessible State
All those states which can never be reached from the
initial state are called as inaccessible states.
Compiler Design, KCS-502 71
How To Minimize DFA?
Step-02:
• Draw a state transition table for the given DFA.
• Transition table shows the transition of all states on all
input symbols in Σ.
Step-03:
Now, start applying equivalence theorem.
• Take a counter variable k and initialize it with value 0.
• Divide Q (set of states) into two sets such that one set
contains all the non-final states and other set contains
all the final states.
• This partition is called P0.
Compiler Design, KCS-502 72
How To Minimize DFA?
Step-04:
• Increment k by 1.
• Find Pk by partitioning the different sets of Pk-1 .
• In each set of Pk-1 , consider all the possible pair of
states within each set and if the two states are
distinguishable, partition the set into different sets
in Pk.
Step-05:
• Repeat step-04 until no change in partition occurs.
• In other words, when you find Pk = Pk-1, stop.
Step-06:
• All those states which belong to the same set are
equivalent.
• The equivalent states are merged to form a single state
in the minimal DFA.
Number
Numberof
ofstates
statesin
inMinimal
MinimalDFA
DFA==Number
Numberof
ofsets
sets in
in PPkk
Step-02:
Draw a state transition table-
a b
→q0 q1 q2
q1 q1 q3
q2 q1 q2
q3 q1 *q4
*q4 q1 q2
Compiler Design, KCS-502 76
PRACTICE PROBLEMS BASED ON
MINIMIZATION OF DFA
Step-03:
Now using Equivalence Theorem, we have-
P0 = { q0 , q1 , q2 , q3 } { q4 }
P1 = { q0 , q1 , q2 } { q3 } { q4 }
P2 = { q0 , q2 } { q1 } { q3 } { q4 }
P3 = { q0 , q2 } { q1 } { q3 } { q4 }
Example:
Example:
S → a / ab / abc / abcd
• For example,
A→Aα
Here A can be any non-terminal and α denotes
some input string.
digit [0-9]
letter [a-zA-Z]
%%
({letter}|{digit})* printf(“id: %s\n”, yytext);
\n printf(“new line\n”);
%%
main()
{
yylex();
}
• What is YACC ?
– Tool which will produce a parser for a given
grammar.
– YACC (Yet Another Compiler Compiler) is a
program designed to compile a LALR(1)
grammar and to produce the source code of
the syntactic analyzer of the language
produced by this grammar.
int main(void)
{
yyparse();
return 0;
}