u1
u1
2
Please read this disclaimer before
proceeding:
This document is confidential and intended solely for the educational purpose of
RMK Group of Educational Institutions. If you have received this document
through email in error, please notify the system manager. This document contains
proprietary information and is intended only to the respective group / learning
community as intended. If you are not the addressee you should not disseminate,
distribute or copy through e-mail. Please notify the sender immediately by e-
mail if you have received this document by mistake and delete this document
from your system. If you are not the intended recipient you are notified that
disclosing, copying, distributing or taking any action in reliance on the contents of
this information is strictly prohibited.
1 3
22CS601
Compiler Design (Lab Integrated)
Department: CSE
Created by
Date: 18.12.2024
4
CONTENTS
S. No Page No
Contents
1 Course Objectives 6
2 Pre Requisites 7
3 Syllabus 8
4 Course outcomes 10
6 Lecture Plan 12
8 Lecture Notes 14
9 Assignments 82
11 Part B Questions 89
15 Assessment Schedule 94
5
1. COURSE OBJECTIVES
6
2. PRE REQUISITES
22CS601
Semester VI
Compiler Design (Lab Integrated)
Semester V
22CS502
Theory of Computation (Lab Integrated)
Semester II
Semester III
22MA301 22CS201
Data Structures (Lab Integrated)
Discrete Mathematics
Semester I Semester II
22CS101 22IT202
Problem Solving DBMS (Lab Integrated)
using C++ (Lab
Integrated)
7
3. SYLLABUS
OBJECTIVES
Principle sources of optimization – Peep hole Optimization – DAG construction -Basic blocks and
flow graph - Optimization in Basic blocks – Data flow analysis.
8
3. SYLLABUS
LIST OF EXPERIMENTS:
1. Develop a lexical analyzer to recognize a few patterns in C. (Ex.
identifiers, constants, comments, operators etc.). Create a symbol table,
while recognizing identifiers.
2. Design a lexical analyzer for the given language. The lexical analyzer
should ignore redundant spaces, tabs and new lines, comments etc.
9
4. COURSE OUTCOME
10
5. CO - PO / PSO MAPPING
CO HKL K3 K4 K5 K5 K4, A3 A2 A3 A3 A3 A3 A2 1 2 3
K5
PO PO PO PO PO PO PO PO PO PO PO PO
-1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12
CO1 K2 3 2 1 - - - - 1 1 1 - 1 2 - -
CO2 K3 3 2 1 - - - - 1 1 1 - 1 2 - -
CO3 K3 3 2 1 - - - - 1 1 1 - 1 2 - -
CO4 K3 3 2 1 - - - - 1 1 1 - 1 2 - -
CO5 K3 3 2 1 - - - - 1 1 1 - 1 2 - -
CO6 K3 3 2 1 - - - - 1 1 1 - 1 2 - -
• Correlation Level –
• 1. Slight (Low)
• 2. Moderate (Medium)
3. Substantial (High) ,
If there is no correlation, put “-“.
11
6. LECTURE PLAN
20.12 Role of the Lexical 20.12. MD1 & Defines the role of
3 K2 T1
.2024 Analyzer, Input Buffering 2024 MD5 Lexical Analyzer.
Explains the token
21.12 Specification of Tokens - 21.12. MD1 &
4 K3 T1 specification and
.2024 Recognition of Tokens 2024 MD5 recognition of tokens
Explains how to use LEX
23.12 The Lexica Analyzer 23.12. MD1 &
5 K3 T1 tool to design Lexical
.2024 Generator LEX 2024 CO1 MD5 Analyzer.
MD1 & Defines the Finite
6 Finite Automata, K3 T1
24.12 24.12. MD5 automata and its types.
.2024 Regular Expressions 2024
to NFA
Explains the ways to
Optimization of DFA MD1 &
7 26.12 26.12. K3 T1 convert the RE to
.2024 based pattern 2024 MD5 Automata.
matches
27.12 Conversion from NFA to 27.12. MD1 & Discuss the steps to
8 K3 T1
.2024 DFA 2024 MD5 convert NFA to DFA
Apply the Minimization
02.01 02.01. MD1 &
9 Minimization of Automata K3 T1 algorithm to reduce the
.2025 2025 MD5 states in DFA.
https://forms.gle/2HmsNdtCTFCyfBSU7
Exercises:
1. Draw a DFA for the language accepting strings ending with ‘abb’ over
input alphabets ∑ = {a, b}
2. Draw a DFA for the language accepting strings not having substring ‘001’ over
input alphabets ∑ = {0, 1}
3. Construct a DFA that accepts a language L over input alphabets ∑={a, b} such that L is
the set of all strings starting with ‘ba’ or ‘ab’
Practice construction Compiler Design using Jflap software for the above language
http://www.jflap.org/
13
8. LECTURE NOTES
INTRODUCTION
Compiler is a program that can read a program in one language and translate it
into an equivalent program in another language.
Types Of Compiler:
Cross Compiler : A cross compiler is a compiler can able to create executable
code for a platform other than the one on which the compiler is running.
Source to Source compiler : It takes the source code of a Programme written in
one programming language as its input and generates the equivalent source code
in a different programming language.
OVERVIEW OF LANGUAGE PROCESSING SYSTEM
Preprocessor
1.Macro processing: A preprocessor may allow a user to define macros that are
short hands for longer constructs.
2. File inclusion: A preprocessor may include header files into the program text.
14
COMPILER
Error Message
ASSEMBLER:
15
Loader and Link-editor:
Once the assembler procedures an object program, that program must be placed
into memory and executed. The assembler could place the object program directly
in memory and transfer control to it, thereby causing the machine language
program to be execute. This would waste core by leaving the assembler in memory
while the users program was being executed. Also the programmer would have to
retranslate his program with each execution, thus wasting translation time. To
overcome this problem of wasted translation time and memory, system
programmers developed another component called loader.
A loader is a program that places programs into memory and prepares them for
execution. It would be more efficient if subroutines could be translated into object
form the loader could ”relocate” directly behind the users program. The task of
adjusting programs so that they may be placed in arbitrary core locations is called
relocation. Relocation loaders perform four functions.
TRANSLATOR
A translator is a program that takes as input a program written in one language
and produces as output a program in another language. Beside program
translation, the translator performs another very important role, the error-
detection. Any violation of the HLL (High Level Language) specification would be
detected and reported to the programmers. Important role of translator are:
16
INTERPRETER: An interpreter is a program that appears to execute a source
program as if it were machine language.
Source Program
INTERPRETER
Languages such as BASIC, COBOL, LISP can be translated using interpreters. JAVA
also uses interpreter. The process of interpretation can be carried out in following
phases.
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Direct Execution
Advantages:
execution proceeds.
Disadvantages:
17
STRUCTURE OF THE COMPILER DESIGN
Phases of a compiler:
A compiler operates in phases. A phase is a logically interrelated operation that
takes source program in one representation and produces output in another
representation. Compilation process is partitioned into PHASES OF A COMPILER,
No-of-sub processes called ‘phases’
Synthesis phase is also called Backend of the compiler, here the desired target
program constructed from the Intermediate representation.. It includes last 2
phases of compiler
Lexical Analysis
Lexical Analysis is the first phase of the compiler. Another name for lexical analysis
is Text Scanner. Lexical analysis scans the source code as a stream of characters
and converts it into meaningful lexemes. Lexical analyzer represents these lexemes
in the form of tokens. The syntax for the token is <token name, attribute value>.
18
Syntax Analysis:-
The second phase of compilation is called Syntax analysis or parsing. In this phase
syntax analysis reads the tokens as input and produce the parse tree. In syntax
analysis phase, the parser checks that the expression made by the tokens is
syntactically correct or not. The parse tree is also called the derivation tree. Parse
trees are generally constructed to check for ambiguity in the given grammar. There
are certain rules associated with the derivation tree.
Semantic Analysis:
In this phase, Semantic analysis reads the parse tree as the input and produce the
same parse tree with semantic consistency. The semantic analyzer uses the syntax
tree and the information in the symbol table to check the source program for
semantic consistency with the language definition. It also gathers type information
and saves it in either the syntax tree or the symbol table.
19
An important part of semantic analysis is type checking, where the compiler checks
that each operator has matching operands. For example, many programming
language definitions require an array index to be an integer; the compiler must
report an error if floating-point number is used to index an array.
Code Optimization :-
The machine-independent code-optimization phase attempts to improve the
intermediate code so that better target code will result. Usually better means faster,
but other objectives may be desired, such as shorter code, or target code that
consumes less power. Optimization can be assumed as something that removes
unnecessary code lines, and arranges the sequence of statements in order to speed
up the program execution without wasting resources.
Code Generation :
Code generation is the final stage of the compilation process. The code generator
takes as input an intermediate representation of the source program and maps it
into the target language. If the target language is machine code, registers or
memory locations are selected for each of the variables used by the program. Then,
the intermediate instructions are translated into sequences of machine instructions
that perform the same task.
20
Symbol table:
A Symbol table is a data structure maintained by the compiler to record the identifier
used in the source program and to collect information about various attributes of
each identifier.
Error Handlers:-
Error Handler connected with all the six phases of the compiler. Each phases of the
compiler can encounter errors. Each error is handled by error handling mechanism
and to handle the errors at the respective phase.
The lexical analysis phase detect for the token stream which violates the structure
rules or syntax rules of the language.
The semantic analysis phase detect constructs that have the right syntactic
structure but no meaning to the operation involved.
Problems:
X=Y+10
X=(a+b)*C
21
22
Compiler Construction tools:
The compiler writer, like any software developer, can profitably use modern
Software development environments containing tools such as language editors,
debuggers, version managers, profilers, test harnesses, and so on. In addition to
these general software-development tools, other more specialized tools have been
created to help implement various phases of a compiler.
These tools use specialized languages for specifying and implementing specific
components, and many use quite sophisticated algorithms. The most successful
tools are those that hide the details of the generation algorithm and produce
components that can be easily integrated into the remainder of the compiler.
23
LEXICAL ANALYSIS
Lexical analysis is the process of converting a sequence of characters into a sequence of
tokens. A program or function which performs lexical analysis is called a lexical analyzer or
scanner. A lexer often exists as a single function which is called by a parser or another
function.
THE ROLE OF THE LEXICAL ANALYZER
The lexical analyzer is the first phase of a compiler.
Its main task is used to read the input characters and produces as output a sequence of
tokens that the parser uses for syntax analysis.
source token
program
Lexical
Analyzer Parser
getnexttoken
Symbol table
Upon receiving a “get next token” command from the parser, the lexical analyzer reads
input characters until it can identify the next token.
ISSUES OF LEXICAL ANALYZER:
There are three issues in lexical analysis:
• To make the design simpler.
• To improve the efficiency of the compiler.
• To enhance the computer portability.
TOKENS
A token is a string of characters, categorized according to the rules as a symbol (e.g.,
IDENTIFIER, NUMBER, COMMA). The process of forming tokens from an input stream of
characters is called tokenization.
24
A token can look like anything that is useful for processing an input text stream or
text file. Consider this expression in the C programming language: sum=3+2;
Lexeme Token type
sum Identifier
= Assignment operator
3 Number
+ Addition operator
2 Number
; End of statement
LEXEME:
Collection or group of characters forming tokens is called Lexeme.
PATTERN:
A pattern is a description of the form that the lexemes of a token may take.
In the case of a keyword as a token, the pattern is just the sequence of characters
that form the keyword. For identifiers and some other tokens, the pattern is a more
complex structure that is matched by many strings.
25
ERROR RECOVERY STRATERGIES IN LEXICAL ANALYSIS:
The following are the error-recovery actions in lexical analysis:
1) Deleting an extraneous character.
2) Inserting a missing character.
3) Replacing an incorrect character by a correct character.
4) Transforming two adjacent characters.
5) Panic mode Recovery: Deletion of successive characters from the token until error
is resolved.
INPUT BUFFERING TECHNIQUES:
The lexical analyzer scans the input from left to right one character at a time. It uses
two pointers begin ptr(bp) and forward ptr(fp) to keep track of the pointer of the input
scanned.
Initially both the pointers point to the first character of the input string as shown
below
26
The forward ptr moves ahead to search for end of lexeme. As soon as the blank
space is encountered, it indicates end of lexeme. In above example as soon as ptr (fp)
encounters a blank space the lexeme “int” is identified.
The fp will be moved ahead at white space, when fp encounters white space, it
ignore and moves ahead. then both the begin ptr(bp) and forward ptr(fp) are set at next
token. The input character is thus read from secondary storage, but reading in this way from
secondary storage is costly, hence buffering technique is used.
A block of data is first read into a buffer, and then second by lexical analyzer.
there are two methods used in this context: One Buffer Scheme, and Two Buffer Scheme.
These are explained as following below.
One Buffer Scheme: In this scheme, only one buffer is used to store the input string but
the problem with this scheme is that if lexeme is very long then it crosses the buffer
boundary, to scan rest of the lexeme the buffer has to be refilled, that makes
overwriting the first of lexeme
27
Two Buffer Scheme: To overcome the problem of one buffer scheme, in this method two
buffers are used to store the input string. the first buffer and second buffer are scanned
alternately.
when end of current buffer is reached the other buffer is filled. the only problem with this
method is that if length of the lexeme is longer than length of the buffer then scanning input
cannot be scanned completely.
Initially both the bp and fp are pointing to the first character of first buffer. Then the fp
moves towards right in search of end of lexeme. as soon as blank character is recognized,
the string between bp and fp is identified as corresponding token. to identify, the boundary
of first buffer end of buffer character should be placed at the end first buffer.
Similarly end of second buffer is also recognized by the end of buffer mark present at the
end of second buffer. when fp encounters first eof, then one can recognize end of first buffer
and hence filling up second buffer is started in the same way when second eof is obtained
then it indicates of second buffer alternatively both the buffers can be filled up until end of
the input program and stream of tokens is identified.
This eof character introduced at the end is calling Sentinel which is used to identify the end
of buffer
28
Code to advance forward pointer:
if forward at end of first half then
begin reload second half;
forward := forward + 1 end
SENTINELS
For each character read, we make two tests: one for the end of the buffer, and one to determine
what character is read. We can combine the buffer-end test with the test for the current character
if we extend each buffer to hold a sentinel character at the end. The sentinel is a special character
that cannot be part of the source program, and a natural choice is the character eof.
Note that eof retains its use as a marker for the end of the entire input. Any eof that
appears other than at the end of a buffer means that the input is at an end.
forward : = forward + 1;
if forward ↑ = eof then begin
if forward at end of first half then begin reload second half;
forward := forward + 1
end
else if forward at end of second half then begin reload first
half;
move forward to beginning of first half end
else /* eof within a buffer signifying end of input */
terminate lexical analysis
end
29
SPECIFICATION OF TOKENS:
There are 3 specifications of Tokens:
1) Strings
2) Language
3) Regular expression
Operations on strings
The following string-related terms are commonly used:
1.A prefix of string s is any string obtained by removing zero or more symbols
from the end of string s.
For example, ban is a prefix of banana.
2.A suffix of string s is any string obtained by removing zero or more symbols
from the beginning of s. For example, nana is a suffix of banana.
3.A substring of s is obtained by deleting any prefix and any suffix from s. For
example, nan is a substring of banana.
4. The proper prefixes, suffixes, and substrings of a string s are those prefixes,
suffixes, and substrings, respectively of s that are not ε or not equal to s itself.
5.A subsequence of s is any string formed by deleting zero or more not necessarily
consecutive positions of s. For example, baan is a subsequence of banana.
30
Operations on languages:
The following are the operations that can be applied to languages:
1.Union
2.Concatenation
3.Kleene closure
4.Positive closure.
Regular Expressions:
Each regular expression r denotes a language L(r).
Here are the rules that define the regular expressions over some alphabet Σ and
the languages that those expressions denote:
1.ε is a regular expression, and L(ε) is { ε }, that is, the language whose sole
member is the empty string.
2.If „a‟ is a symbol in Σ, then „a‟ is a regular expression, and L(a) = {a}, that is,
the language with one string, of length one, with „a‟ in its one position.
3.Suppose r and s are regular expressions denoting the languages L(r) and L(s).
Then, a) (r)|(s) is a regular expression denoting the language L(r) U L(s).
31
Regular set
A language that can be defined by a regular expression is called a regular set.
If two regular expressions r and s denote the same regular set, we say they are
equivalent and Write r = s.
There are a number of algebraic laws for regular expressions that can be used to
manipulate into equivalent forms.
For instance, r|s = s|r is commutative; r|(s|t)=(r|s)|t is associative.
Regular Definitions
Giving names to regular expressions is referred to as a Regular definition.If ∑ is an
alphabet of basic symbols, then a regular definition is a sequence of definitions of
the form
d1 → r 1
d2 → r2
……… dn → rn
Shorthands
Certain constructs occur so frequently in regular expressions that it is convenient to
introduce notational shorthands for them.
32
1. One or more instances (+):
• The unary postfix operator + means “ one or more instances of” .
• If r is a regular expression that denotes the language L(r), then ( r )+ is a
regular expression that denotes the language (L (r ))+
• Thus the regular expression a+ denotes the set of all strings of one or more a‟s.
• The operator + has the same precedence and associativity as the operator *
3. Character Classes:
• The notation [abc] where a, b and c are alphabet symbols denotes the regular
expression a | b | c.
Non-regular Set:
A language which cannot be described by any regular expression is a non-regular
set. Example: The set of all strings of balanced parentheses and repeating strings
cannot be described by a regular expression. This set can be specified by a context-
free grammar.
33
RECOGNITION OF TOKENS:
term → id | num
where the terminals if , then, else, relop, id and num generate sets of strings given
by the following regular definitions:
if the n → if the n els
els → e
e →
relop → <|<=|=|<>|>|>=
id → letter(letter|digit)*
num → digit+ (.digit+)?(E(+|-)?digit+)?
For this language fragment the lexical analyzer will recognize the keywords if, then,
else, as well as the lexemes denoted by relop, id, and num. To simplify matters, we
assume keywords are reserved; that is, they cannot be used as identifiers.
Transition diagrams
It is a diagrammatic representation to depict the action that will take place when a
lexical analyzer is called by the parser to get the next token. It is used to keep
track of information about the characters that are seen as the forward pointer
scans the input.
34
FINITE AUTOMATA
Finite Automata is one of the mathematical models that consist of a number of
states and edges. It is a transition diagram that recognizes a regular expression or
grammar.
36
Let X = (a, b)
The regular expression a|b denote the language {a, b}.
(a|b)(a|b) represent {aa, ab, ba, bb} the language of all strings having
length two over the alphabet X. One more regular expressions that can
accept the same language is aa|ab|ba|bb.
a* represents the group of all strings that have zero or more a's, i.e. (e, aa,
(a|b)* represent the group of all strings having zero or more times of a or
b, i.e., all string that contains a's and b's: {e, a, b, aa, ab, ba, bb, aaa,}.
One more regular expression that accepts the same language is
(a*b*)*.
a|a*b denotes the language {a, b, ab, aab, aaan ...}, i.e., the string a, and
FINITE AUTOMATA
o At the time of transition, the automata can either move to the next state or
stay in the same state.
o Finite automata have two states, Accept state or Reject state. When the
input string is processed successfully, and the automata reached its final
state, then it will accept.
Formal Definition of FA
2
2
The algorithm constructs NFAs with only one final state. For example, the third rule
indicates that, to construct the NFA for the RE AB, we construct the NFAs for A and
B which are represented as two boxes with one start and one final state for each
box. Then the NFA for AB is constructed by connecting the final state of A to the
start state of B using an empty transition.
The next step is to convert a NFA to a DFA (called subset construction). Suppose
that you assign a number to each NFA state. The DFA states generated by subset
construction have sets of numbers, instead of just one number. For example, a DFA
state may have been assigned the set {5,6,8}. This indicates that arriving to the
state labeled {5,6,8} in the DFA is the same as arriving to the state 5, the state 6,
or the state 8 in the NFA when parsing the same input. (Recall that a particular
input sequence when parsed by a DFA, leads to a unique state, while when parsed
by a NFA it may lead to multiple states.)
First we need to handle transitions that lead to other states for free (without
consuming any input). These are the transitions. We define the closure of a NFA
node as the set of all the nodes reachable by this node using zero, one, or more
transitions. For example, The closure of node 1 in the left figure below
38
is the set {1,2}. The start state of the constructed DFA is labeled by the closure of the NFA start
state. For every DFA state labeled by some set and for every character c in the language alphabet,
you find all the states reachable by s1, s2, ..., or sn using c arrows and you union together the
closures of these nodes. If this set is not the label of any other node in the DFA constructed so far,
you create a new DFA node with this label. For example, node {1,2} in the DFA above has an arrow
to a {3,4,5} for the character a since the NFA node 3 can be reached by 1 on a and nodes 4 and 5
can be reached by 2. The b arrow for node {1,2} goes to the error node which is associated with an
empty set of NFA nodes. The following NFA recognizes , even though it wasn't constructed
corresponds to a set of some states from the NFA. In the DFA, transitions from a state S by some
symbol go to the state S that consists of all the possible NFA-states that could be reached by from
some NFA state q contained in the present DFA state S. The resulting DFA \simulates" the given NFA
in the sense that a single DFA-transition represents many simultaneous NFA-transitions. The first
concept we need is the "E-closure pronounced \epsilon closure". The " -closure of an NFA state q is
the set containing q along with all states in the automaton that are reachable by any number of " E-
transitions from q . In the following automaton, the " E-closures are given in the table to the right:
Likewise, we can done the "-closure of a set of states to be the states reachable by " - transitions
from its members. In other words, this is the union of the " -closures of its elements. To convert our
NFA to its DFA counterpart, we begin by taking the " –closure of the start state q of our NFA and
constructing a new start state S.in our DFA corresponding to that " -closure. Next, for each symbol in
our alphabet, we record the set of NFA states that we can reach from S 0on that symbol. For each
such set, we make a DFA state corresponding to its "E-closure, taking care to do this only once for
each set. 36
Construction of Optimized DFA from Regular Expression
Construction of DFA directly from a Regular Expression (RE) by computing the functions
nullable(n), firstpos(n), lastpos(n) and followpos(i)from the syntax tree.
• nullable(n): Is true for * node and node labeled with Ɛ. For other nodes it is false.
• firstpos(n): Set of positions that correspond to the first symbol of strings in n's
subtree.
• lastpos(n): set of positions that correspond to the last symbol of strings in n's
subtree.
• followpos (n): Set of next possible positions from n for valid strings.
Rules for computing nullable,firstpos and lastpos(n)
A leaf
true ∅ ∅
labeled by
A leaf with
false {i} {i}
position i
if ( nullable(c1) )
firstpos(c1) U if ( nullable(c2) )
nullable(c1) and firstpos(c2) else lastpos(c1) U
n = c1.c2 lastpos(c2) else
nullable(c2) firstpos(c1)
lastpos(c2)
Computation of followpos
The position of regular expression can follow another in the following ways:
• If n is a cat node with left child c1 and right child c2, then for every position i in
lastpos(c1), all positions in firstpos(c2)are in followpos(i).
For cat node(.), for each position i in lastpos of its left child, the firstpos of its right
child will be in followpos(i).
• If n is a star node and i is a position in lastpos(n), then all positions in firstpos(n)are
in followpos(i).
• For star node(*), the firstpos of that node is in followpos of all positions in lastpos of
that node.
40
Building Syntax tree
Example
Node followpos
1 {1, 2, 3}
2 {1, 2, 3}
3 {4}
4 {5}
5 {6}
6 -
41
Step 4
A=firstpos(n0)={1,2,3}
Move[A,a]={1,2}
=followpos(1) U followpos(3)
= {1,2,3,4}=B
Move[A,b]={2}
followpos(2)={1,2,3}=A Move[B,a]={1,3}
=followpos(1) U followpos(3)=B
Move[B,b]={2,4}
=followpos(2) U followpos(4)
={1,2,3,5}=C
Move[C,a]={1,3}=B
Mov[C,b]={2,5}
= Followpos(2)Ufollowpos(5)={1,2,3,6}---D
Mov(D,a)={1,3}=B
Mov(D,b)={2,4}=C
42
Finite Automata Model:
Input tape: It is a linear tape having some number of cells. Each input symbol is
placed in each cell.
Finite control: The finite control decides the next state on receiving particular
input from input tape. The tape reader reads the cells one by one from left to right,
and at a time only one input symbol is read.
Alphabet:
An alphabet is a finite, non-empty set of symbols. It is
denoted by Σ.Example:
i) Σ = {a,b}, an alphabet of 2
symbols a and b.
ii)Σ = {0,1}, the binary
alphabet.
2
3
Σ = {0, 1, 2}, an alphabet of 3 symbols 0, 1 and 2.
String:
Example:
i) 01101 is a string from the binary alphabet Σ = {0,1}
The string 111 is another string chosen from this alphabet
ii) If Σ = {a,b} then abab, aabba area all strings over the alphabet Σ = {a,b}.
iii) If Σ = {a} then a, aa, aaa, are all strings over the alphabet Σ = {a}.
Length of a String:
Let w be the string then “length of the string │w│ is the number of
symbols composing the string.
Example
│ ε │=0
Thus there are only two symbols, 0 and 1, in the string 01101, but
there are fivepositions for symbols and its length is 5.However you
2
4
should generally expect that “the number of symbols” can be used
Substring:
A string v appears within another string w(w=uv) is called substring of
w.
Example:
i) w=abbab be a string over an alphabet Σ = {a,b} then a,ab,abb,ba,bab,… are
all substrings of w.
ii) w=123
Prefixes = {ε, 1, 12, 123}
Suffixes = {ε, 3, 23, 123}
iii) w=abbab
Prefixes = {ε, a, ab, abb,
abba, abbab}
Reverse of a String:
i.e) w=a1a2a3…am
wR = am… a3a2a1
Example:
xy = abc123
│xy│=6 i.e) │x│=3, │y│=3 hence │xy│=│x│+│y│
Languages:
For any alphabet Σ, any subset L of Σ* is a language. A set of strings from
analphabet is called a language.
Example:
i) English: It is a language over Σ= {a,b,c,…z}.
Notations:
2. Φ: Empty language.
Example:
It contains no strings.
Power of an alphabet:
2
6
Σm denotes the set of all string over the alphabet Σ of
length m.Example:
If Σ = {0, 1} then
Σ2 = {00, 01, 10, 11} set of all string of length two over Σ = {0, 1}.
For Instance, {0,1}* = {ε, 0,1, 00, 01, 10, 11, 000, 001, 010, 011,100,101, . . . }
Σ* = Σ0 U Σ1 U Σ2 U Σ3 U . . .
Sometimes, we wish to exclude the empty string from the set of strings.
The set of nonempty strings from alphabet Σ is denoted Σ + thus two appropriate
equivalences are
Σ+ = Σ1
U Σ2 U
Σ3 U . . .
Σ* = Σ+
U {ε}
Kleen’s Closure:
Let Σ be an alphabet, then “kleen’s closure Σ*” denotes the set of all
strings
Example:
i) If Σ = {a} then Σ* = {ε,a,aa…}.
27
ii) If Σ = {0,1} then Σ*={ε,0,1,00,11,10…}
Positive Closure
Σ* = Σ+ U Σ0
Types of Automata
2
8
In the following diagram, we can see that from state q0 for input a, there is only
one path which is going to q1. Similarly, from q0, there is only one path for input b
going to q2.
Transition Table:
→q0 q0 q1
q1 q2 q1
2
9
*q2 q2 q2
Example 2:
DFA with ∑ = {0, 1} accepts all starting with 0.
Solution:
Explanation:
o In the above diagram, we can see that on given 0 as input to DFA in state
q0 the DFA changes state to q1 and always go to final state q1 on starting
input 0. It can accept 00, 01, 000, 001....etc. It can't accept any string which
starts with 1, because it will never go to final state on a string starting with
1.
Example 3:
DFA with ∑ = {0, 1} accepts all ending with 0.
Solution:
Explanation:
In the above diagram, we can see that on given 0 as input to DFA in state q0, the
DFA changes state to q1. It can accept any string which ends with 0 like 00, 10,
110, 100....etc. It can't accept any string which ends with 1, because it will never
go to the final state q1 on 1 input, so the string ending with 1, will not be accepted
or will be rejected.
Example 4:
Design a FA with ∑ = {0, 1} accepts those string which starts with 1 and ends with
0.
Solution:
3
0
The FA will have a start state q0 from which only the edge with input 1 will go to
the next state.
In state q1, if we read 1, we will be in state q1, but if we read 0 at state q1, we will
reach to state q2 which is the final state. In state q2, if we read either 0 or 1, we
will go to q2 state or q1 state respectively. Note that if the input ends with 0, it will
be in the final state.
Example 5:
Solution:
In the given solution, we can see that only input 101 will be accepted. Hence, for
input 101, there is no other path shown for other input.
Example 6:
Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive 0's.
Solution:
The strings that will be generated for this particular languages are 000, 0001, 1000,
10001, .... in which 0 always appears in a clump of 3. The transition graph is as
follows:
Example 7:
3
1
Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that does not contain
consecutive 1's.
Solution:
The stages q0, q1, q2 are the final states. The DFA will generate the strings that do
not contain consecutive 1's like 10, 110, 101,..... etc.
Example 8:
Design a FA with ∑ = {0, 1} accepts the strings with an even number of 0's
followed by single 1.
Solution:
3
2
The finite automata are called NFA when there exist many paths for specific
input from the current state to the next state.
Every NFA is not DFA, but each NFA can be translated into DFA.
NFA is defined in the same way as DFA but with the following two
exceptions, it contains multiple next states, and it contains ε transition.
In the following image, from state q0 for input a, there are two next states q1 and
q2, similarly, from q0 for input b, the next states are q0 and q1. Thus it is not fixed
or determined that with a particular input where to go next. Hence this FA is called
non-deterministic finite automata.
NFA also has five states same as DFA, but with different transition function, as
shown follows:
δ: Q x ∑ →2Q
where,
3
3
4. The final state is denoted by the double circle.
Example 1:
1. Q = {q0, q1, q2}
2. ∑ = {0, 1}
3. q0 = {q0}
4. F = {q2}
Solution:
Transition diagram:
Transition Table:
→q0 q0, q1 q1
q1 q2 q0
*q2 q2 q1, q2
In the above diagram, we can see that when the current state is q0, on input 0, the
next state will be q0 or q1, and on 1 input the next state will be q1. When the
current state is q1, on input 0 the next state will be q2 and on 1 input, the next
state will be q0. When the current state is q2, on 0 input the next state is q2, and
on 1 input the next state will be q1 or q2.
Example 2:
NFA with ∑ = {0, 1} accepts all strings with 01.
Solution:
3
4
Transition Table:
→q0 q1 ε
q1 ε q2
*q2 q2 q2
Example 3:
NFA with ∑ = {0, 1} and accept all string of length atleast 2.
Solution:
Transition Table:
→q0 q1 q1
q1 q2 q2
*q2 ε ε
Example 4:
Design a NFA for the transition table as given below:
Presen 0 1
t
State
3
5
→q0 q0, q1 q0, q2
q1 q3 ε
q2 q2, q3 q3
→q3 q3 q3
Solution:
The transition diagram can be drawn by using the mapping function as given in the
table.
Here,
1. δ(q0, 0) = {q0, q1}
2. δ(q0, 1) = {q0, q2}
3. Then, δ(q1, 0) = {q3}
4. Then, δ(q2, 0) = {q2, q3}
5. δ(q2, 1) = {q3}
6. Then, δ(q3, 0) = {q3}
7. δ(q3, 1) = {q3}
Example 5:
Design an NFA with ∑ = {0, 1} accepts all string ending with 01.
Solution:
3
6
Hence, NFA would be:
Example 6:
Design an NFA with ∑ = {0, 1} in which double '1' is followed by double '0'.
Solution:
The FA with double 1 is as follows:
Now before double 1, there can be any string of 0 and 1. Similarly, after double 0,
there can be any string of 0 and 1.
Now as 1010 could be the substring. Hence we will add the inputs 0's and 1's so
that the substring 1010 of the language can be maintained. Hence the NFA
becomes:
Transition table for the above transition diagram can be given below:
Present 0 1
State
→q1 q1 q1,
q2
q2 q3
q3 q4
q4 q5
*q5 q5 q5
Consider a string 111010,
1. δ(q1, 111010) = δ(q1, 1100)
2. = δ(q1, 100)
3. = δ(q2, 00)
3
8
Got stuck! As there is no path from q2 for input symbol 0. We can process string
111010 in another way.
Example 8:
Design an NFA with ∑ = {0, 1} accepts all string in which the third symbol from the
right end is always 0.
Solution:
Thus we get the third symbol from the right end as '0' always. The NFA can be:
The above image is an NFA because in state q0 with input 0, we can either go to
state q0 or q1.
∈-closure(1) : {1,2,3,5,8}
∈-closure(2) : { 2,3,5}
∈-closure(3) : { 3}
∈-closure(4) : { 4,7,8,2,3,5}
∈-closure(5) : { 5}
∈-closure(6) : { 6,7,8,2,3,5}
∈-closure(7) : { 7,8,2,3,5}
∈-closure(8) : { 8}
40
Steps to constructing NFA
REGULAR EXPRESSIONS
4
1
o Regular expressions are used to match character combinations in strings.
String searching algorithm used this pattern to find the operations on a
string.
For instance:
Union: If L and M are two regular languages then their union L U M is also a
union.
L U M = {s | s is in L or s is in M}
Intersection: If L and M are two regular languages then their intersection is also
an intersection.
L ⋂ M = {st | s is in L and t is in M}
Kleen closure: If L is a regular language then its Kleen closure L1* will also be a
regular language.
Example 1:
Write the regular expression for the language accepting all combinations of a's,
over the set
∑ = {a}
4
2
Solution:
All combinations of a's means a may be zero, single, double and so on. If a is
appearing zero times, that means a null string. That is we expect the set of {ε, a,
aa, aaa, ....}. So we give a regular expression for this as:
1. R = a*
Example 2:
Write the regular expression for the language accepting all combinations of a's
except the null string, over the set ∑ = {a}
Solution:
This set indicates that there is no null string. So we can denote regular expression
as:
R = a+
In the below article, we shall see some Designing of Finite Automata form the
given Regular Expression-
43
Regular Expression 1: Regular language,
L1 = a(a+b)*
Strings always start with ‘a’. Its finite automata will be like below-
In the above transition diagram, as we can see that initial state ‘Y’ on getting ‘a’
as the input it transits to a final state ‘Z’ and so on for the remaining states. Thus
this FA accepting all the strings of the given RE language.
4
4
Strings always end with ‘a’. Its finite automata will be like below-
In the above transition diagram, as we can see that initial state ‘Y’ on getting ‘a’
as the input it transits to a final state ‘Z’ and on getting ‘b’ as the input it remains
in the state of itself and so on for the remaining states. Thus this FA accepting all
the strings of the given RE language.
Strings containing ‘a’ as the alphabet. Its finite automata will be like below-
In the above transition diagram, as we can see that initial state ‘Y’ on getting ‘b’
as the input it remains in the state of itself and on getting ‘a’ as the input it
transits to a final state ‘Z’ and so on for the remaining states. Thus this FA
accepting all the strings of the given RE language.
4
5
{aab, abb, baa, bba}
Strings start and symbols.
end with different
Its finite automata will be like below-
In the above transition diagram, as we can see that initial state ‘V’ on getting ‘a’
as the input it transits to a state ‘W’ and so on for the remaining states. Thus this
Strings start and end with same symbols. Its finite automata will be like below-
In the above transition diagram, as we can see that initial and final state ‘V’ on
getting ‘a’ as the input it transits to another final state ‘W’ and so on for the
remaining states. Thus this FA accepting all the strings of the given RE language.
4
6
Conversion from NFA to DFA
Example 1
Consider a Non-deterministic finite automata (NFA) and convert that NFA into
equivalent Deterministic Finite Automata (DFA).
Solution
States\inputs a b
->q0 {q0,q1} q0
q1 q2 q1
q2 q3 q3
q3 - q2
DFA cannot have multiple states. So, consider {q0,q1} as a single state while
constructing DFA.
Let’s convert the above table into equivalent DFA
States\inputs a b
->q1 [q0,q1] q0
4
7
Convert the given NFA to DFA.
Solution: For the given transition diagram we will first construct the transition
table.
Example 2
4
8
State 0 1
Solution: For the given transition diagram we will first construct the transition
table.
δ'([q1], 0) = ϕ
δ'([q1], 1) = [q0, q1]
Similarly,
As in the given NFA, q1 is a final state, then in DFA wherever, q1 exists that state
becomes a final state. Hence in the DFA, final states are [q1] and [q0, q1].
Therefore set of final states F = {[q1], [q0, q1]}.
4
9
State 0 1
Suppose
1. A = [q0]
2. B = [q1]
3. C = [q0, q1]
Example 3
Let us consider the NDFA shown in the figure below.
5
0
q δ(q,0) δ(q,1)
a {a,b,c,d,e} {d,e}
b {c} {e}
c ∅ {b}
d {e} ∅
e ∅ ∅
Using the above algorithm, we find its equivalent DFA. The state table of the DFA is
shown in below.
q δ(q,0) δ(q,1)
[d,e] [e] ∅
[e] ∅ ∅
[c, e] ∅ [b]
[c] ∅ [b]
5
1
SCANNER GENERATOR (LEX, FLEX)
Lex (lexical analyzer generator) is a tool or software which automatically
generates a lexical analyzer (finite Automata). It takes as its input a LEX source
program and produces lexical Analyzer as its output. Lexical Analyzer will convert
the input string entered by the user into tokens as its output.
LEX is a program generator designed for lexical processing of character
input/output stream. Anything from simple text search program that looks for
pattern in its input-output file to a C compiler that transforms a program into
optimized code.
In program with structure input-output two tasks occurs over and over. It
can divide the input-output into meaningful units and then discovering the
relationships among the units for C program (the units are variable names,
constants, and strings). This division into units (called tokens) is known as lexical
analyzer or LEXING. LEX helps by taking a set of descriptions of possible tokens n
producing a routine called a lexical analyzer or LEXER or Scanner.
5
2
LEX Source Program
It is a language used for specifying or representing Lexical Analyzer.
There are two parts of the LEX source program −
Auxiliary Definitions
Translation Rules
Auxiliary Definition
It denotes the regular expression of the form.
Distinct Names [D1=R1\D2=R2\Dn=Rn][D1=R1\D2=R2\Dn=Rn] Regular
Expressions
Where
5
3
Auxiliary Definition for Signed Numbers
integer=digit digit*
sign = + | -
.
.
.
Pn {Actionn}
Where
Pi → Pattern or Regular Expression consisting of input alphabets and Auxiliary
definition names.
Actioni → It is a piece of code that gets executed whenever a token is Recognized.
Each Actioni specifies a set of statements to be executed whenever each regular
expression or pattern Pi matches with the input string.
Example
Translation Rules for "Keywords"
54
We can see that if Lexical Analyzer is given the input "begin", it will recognize the
token "begin" and Lexical Analyzer will return 1 as integer code to the parser.
Bison produces parser from the input file provided by the user. The
function yylex() is automatically generated by the flex when it is provided with a .l
file and this yylex() function is expected by parser to call to retrieve tokens from
current/this token stream.
Note: The function yylex() is the main flex function that runs the Rule Section
and extension (.l) is the extension used to save the programs.
5
5
Step 1: An input file describes the lexical analyzer to be generated named lex.l is
written in lex language. The lex compiler transforms lex.l to C program, in a file
that is always named lex.yy.c.
Step 2: The C compiler compile lex.yy.c file into an executable file called a.out.
Step 3: The output file a.out take a stream of input characters and produce a
stream of tokens.
Program Structure:
In the input file, there are 3 sections:
1. Definition Section: The definition section contains the declaration of
variables, regular definitions, manifest constants. In the definition section, text is
enclosed in “%{ %}” brackets. Anything written in this brackets is copied
directly to the file lex.yy.c
Syntax:
%{
// Definitions
%}
[0+9] either 0, + or 9
[0, 9] either 0, ‘, ‘ or 9
5
6
[0 9] either 0, ‘ ‘ or 9
[-09] either -, 0 or 9
a* 0 or more occurrences of a
a+ 1 or more occurrences of a
%{
int count = 0;
%}
/*** Rule Section has three rules, first rule
matches with capital letters, second rule
matches with any character except newline and
third rule does not take input after the enter***/
%%
[A-Z] {printf("%s capital letter\n", yytext);
count++;}
5
8
capital letter present in the given input***/
int yywrap(){}
int main(){
// Explanation:
// yywrap() - wraps the above rule section
/* yyin - takes the file pointer
which contains the input*/
/* yylex() - this is the main flex function
which runs the Rule Section*/
5
9
Example 2: Count the number of characters and number of lines in the
input
%{
int no_of_lines = 0;
int no_of_chars = 0;
%}
/***rule 1 counts the number of lines,
rule 2 counts the number of characters
and rule 3 specifies when to stop
taking input***/
%%
\n ++no_of_lines;
. ++no_of_chars;
end return 0;
%%
/*** User code section***/
int yywrap(){}
6
0
Output:
6
1
9. ASSIGNMENT : UNIT – I
Set - 1 (i) Show how an output a:= b + c * 60 get processed in compiler. Show
that output at each stage of the compiler and also show the contents of
symbol table (CO1, K2)
(Toppers)
(ii) Write a LEX program to find out total number of vowels and
consonants from the given input string. (CO1, K3)
Set - 2 (i) Describe the various phases of compiler and trace the program
segment a: = b + c * 4 for all phases (CO1, K2)
(Above (ii) Write a LEX program for counting total number of words, lines and
Average) character in the given input file. (CO1, K3)
Set - 3
(i) What is Compiler? Explain the various phases of compiler in detail.
(CO1, K1)
(Average)
(ii) Define the terms: Lexeme, token and pattern (CO1, K1)
Set - 4
(i) Explain about the grouping of phases in detail. (CO1, K1)
(Below (ii) List the various Compiler construction tools and explain in detail.
Average) (CO1, K1)
Set - 5
(i) Explain about Input Buffering techniques. (CO1, K1)
(Slow (i) Discuss the role of Lexical analyzer in detail. (CO1, K1)
Learners)
82
10. PART A : Q & A : UNIT – I
SNo Questions and Answers CO K
1 What are the two parts of a compilation? Explain
briefly.
Analysis and Synthesis are the two parts of compilation.
(Front-end & Back-end)
● The analysis part breaks up the source program K1
into constituent pieces and creates an
intermediate representation of the source program.
● The synthesis part constructs the desired target
program from the intermediate representation.
2 List the various compiler construction tools.
● Parser generators
● Scanner Generator
● Syntax-directed translation engines K1
● Automatic code generators
● Dataflow Engines
● Compiler construction tool kits
3 Differentiate compiler and interpreter.
● The machine-language target program produced
by a compiler is usually much faster than an
interpreter at mapping inputs to outputs. K1
● An interpreter, however, can usually give better
CO1
error diagnostics than a compiler, because it executes
the source program statement by statement.
4 Define tokens, Patterns, and lexemes.
● Tokens- Sequence of characters that have a collective
meaning. A token of a language is a category of its
lexemes.
● Patterns- There is a set of strings in the input for
which the same token is produced as output. This K1
set of strings is described by a rule called a
pattern associated with the token.
● Lexeme- A sequence of characters in the source
program that is matched by the pattern for a token.
5 Describe the possible error recovery actions in lexical
analyzer.
·Panic mode recovery
·Deleting an extraneous character
·Inserting a missing character K1
·Replacing an incorrect character by a correct character
·Transposing two adjacent characters
83
10. PART A : Q & A : UNIT – I
SNo Questions and Answers CO K
6 Write the regular expression for identifier.
letter -> A | B | ……| Z | a | b | …..| z
K2
digit -> 0 | 1 |…….|9
id -> letter (letter | digit ) *
7 Write the regular expression for Number.
digit -> 0 | 1 |…….|9
digits -> digit digit*
optional_fraction->.digits | € K2
optional_exponent -> (E(+|-| €)digits) | €
num -> digits optional_fractionoptional_exponent
id -> letter (letter | digit ) *
8 List the phases that constitute the front end of a
compiler.
The front end consists of those phases or parts of
phases that depend primarily on the source language
and are largely independent of the target
machine.These include
● Lexical and Syntactic analysis K1
● The creation of symbol table
● Semantic analysis CO1
● Generation of intermediate code
A certain amount of code optimization can be done by
the front end as well and includes Error handling that
goes along with each of these phases.
9 Mention the back-end phases of a compiler.
The back end of compiler includes those portions
that depend on the target machine and generally
those portions do not depend on the source
language, just the intermediate language. These K1
include Code optimization
Code generation, along with error handling and symbol-
table operations.
10 What is the role of lexical analysis phase?
Lexical analyzer reads the source program one
character at a time, and grouped into a sequence
of atomic units called tokens. Identifiers, keywords,
constants, operators and punctuation symbols such as
commas, parenthesis, are typical tokens. K1
Identifiers are entered into the Symbol Table.
Removes comments, whitespaces (blanks, newline , tab)
Keeps track of the line numbers.
Identifies Lexical Errors and appropriate error messages.
84
10. PART A : Q & A : UNIT – I
K1
86
10. PART A : Q & A : UNIT – I
Types of Translator: K1
1.Interpreter
2.Compiler
3.Assembler
.
22 Mention the uses of SYMBOL TABLE MANAGEMENT
* Symbol table is used to store all the information about
identifiers used in the program. K1
* It is a data structure containing a record for each
identifier, with fields for the attributes of the identifier.
23 What is Parser Generators?
- These produce syntax analyzers, normally from
input that is based on a context-free grammar.
K1
- It consumes a large fraction of the running
time of a compiler.
- Example - YACC CO1
(Yet Another Compiler Compiler).
24 What is Scanner Generator?
87
10. PART A : Q & A : UNIT – I
(CO1, K2)
1. Describe the various phases of compiler and trace it with the program segment
(position := initial + rate * 60).
2. Explain in detail the process of compilation. Illustrate the output of each phase of
the compilation for the input “a = (b+c) * (b+c) *2”
3. Discuss Input buffering techniques in detail.
4. Construct DFA for the Regular expression (a l b)*a.
5. Construct DFA using the regular expression (a/b)*abb.
6. Construct the NFA from the (a|b)*a(a|b) using Thompson’s construction
algorithm.
7. Construct the DFA for the augmented regular expression (a | b )* # directly
using syntax tree.
8. Write an algorithm for minimizing the number of states of a DFA.
PART C QUESTIONS
(CO1, K3)
89
12. Supportive online Certification courses
NPTEL : https://nptel.ac.in/courses/106105190
Swayam : https://www.classcentral.com/course/swayam-compiler-design-12926
coursera : https://www.coursera.org/learn/nand2tetris2
Udemy : https://www.udemy.com/course/introduction-to-compiler-construction-and-design/
Mooc : https://www.mooc-list.com/course/compilers-coursera
Edx : https://www.edx.org/course/compilers
90 55
13. Real time Applications in day to day life and to Industry
91 55
14. CONTENTS BEYOND SYLLABUS : UNIT – I
[~delim1,delim2,...,delimN] :: Token
92
Contents beyond the Syllabus
Lexical analyzer reads the characters from source code and convert it into tokens.
Keywords
Identifiers
Operators
Constants
https://www.thecrazyprogrammer.com/2017/02/lexical-analyzer-in-c.html
https://favtutor.com/blogs/lexical-analyzer-cpp
https://www.delftstack.com/howto/cpp/lexical-analyzer-cpp/
8
2
15. ASSESSMENT SCHEDULE
94
16. PRESCRIBED TEXT BOOKS & REFERENCE BOOKS
• TEXT BOOKS:
• REFERENCE BOOKS:
1.Randy Allen, Ken Kennedy, “Optimizing Compilers for Modern Architectures: A
Dependence-based Approach”, Morgan Kaufmann Publishers, 2002.
2.Steven S. Muchnick, “Advanced Compiler Design and Implementation”, Morgan
Kaufmann Publishers - Elsevier Science, India, Indian Reprint, 2003.
3.Keith D Cooper and Linda Torczon, “Engineering a Compiler”, Morgan
Kaufmann Publishers, Elsevier Science, 2004.
4.V. Raghavan, “Principles of Compiler Design”, Tata McGraw Hill Education
Publishers, 2010.
5. Allen I. Holub, “Compiler Design in C”, Prentice-Hall Software Series, 1993.
95
17. MINI PROJECT SUGGESTION
• Objective:
To design DFA, Convertors of NFA to DFA and Lex program for languages like
C,PASCAL , JAVA , etc. (Mini Project)
• Planning:
• This method is mostly used to improve the ability of students in application
domain and also to reinforce knowledge imparted during the lecture.
Projects:
Set
Number Mini Project Title
(Category)
Set - 1 Convert the following Regular Expression into DFA using JFLAP tool.
(Toppers) 10 + (0+ 11) 0* 1 (CO1, K3)
Set - 2 Prepare a project to convert Regular Expression to DFA (from compiler
(Above point of view) (CO1, K3)
Average)
Set - 3 Design a DFA that describes the behavior of a vending machine which
(Average) accepts 1 dollar and its quarter, and charges $1.25 per soda. Once the
machine receives at least $1.25, the soda can be dispensed to the user.
The machine will not dispense a soda until at least $1.25 has been
deposited, and it will not accept more money once it has already
received greater than or equal to $1.25. (CO1, K3)
Set - 4 Design a DFA to implement a Pac-Man game. The Pac-Man game play
requires the player to navigate through a maze, eating pellets and
(Below avoiding the ghosts who chase him through the maze. Occasionally,
Average) Pac-Man can turn the tables on his pursuers by eating a power pellet,
which temporarily grants him the power to eat the ghosts. When this
occurs, the ghosts' behavior changes, and instead of chasing Pac-Man
they try to avoid him. If the ghosts are eaten by the Pac-man the game
will be regenerated. (CO1, K3)
Set - 5 Write a LEX program to find biggest among three numbers
(Slow (CO1, K3)
Learners) 96
Thank you
Disclaimer:
This document is confidential and intended solely for the educational purpose of RMK Group of Educational
Institutions. If you have received this document through email in error, please notify the system manager. This
document contains proprietary information and is intended only to the respective group / learning community as
intended. If you are not the addressee you should not disseminate, distribute or copy through e-mail. Please notify
the sender immediately by e-mail if you have received this document by mistake and delete this document from
your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any
action in reliance on the contents of this information is strictly prohibited.
61
61