DA Sample
DA Sample
DA Sample
CODE:
(PYTHON CODE)
NO_OF_CHARS = 256
i=0
# ns stores the result which is next state
return TF
def main():
txt = "AABAACAADAABAAABAA"
pat = "AABA"
search(pat, txt)
if __name__ == '__main__':
main()
2. Explain with examples the real time applications of Finite
Automata, Regular Expressions, and Context free
grammars.
i. L = {abnam : n ≥ 2,m ≥ 3}
M = {Q, q0, F, δ, Σ}
Q = {q0, q1, q2, q3, q4, q5, q6, q7}
q0 = {q0}
Σ = {a,b}
F = {q6}
δ: Q * Σ Q
Soln:
M = {Q, q0, F, δ, Σ}
Q = {q0, q1, q3, q2}
q0 = {q0}
Σ = {a, b}
F = {q2}
δ: Q * Σ 2Q
5. State whether L={anbn | n > 0} is non regular.
Soln:
Case 1:
u = aa ; v = ab ; w = bb
If i = 1
uv1w = aaabbb
aaabbb ∈ L
If i = 2
uv2w = aaababbb
aaababbb ∈ L
Case 2:
u = a ; v = aa ; w =bbb
If i = 2
uv2w = aaaaabbb
Soln:
Soln:
Any set that represents the value of the Regular Expression is called a
Regular Set.
A regular expression is an algebraic formula whose value is a pattern
consisting of a set of strings, called the language of the expression.
Make a state table for given diagram and use equivalence partitioning to
reduce the diagram into minimum DFA.
Minimized DFA:
9. Every non deterministic finite automaton is a deterministic
finite automaton. If yes, justify your answer with suitable
example.
Soln:
NO, Every NFA is not DFA. Because a finite automata is said to be non-
deterministic if for a particular input symbol, the machine can move
to any combination of the states in the machine.
In other words, the exact state to which the machine moves cannot be
determined. While DFA has definite combination of states in the
machine for a particular input symbol.
Therefore, DFA is a subset of NFA and vice versa is not true. Hence
every NFA can never be DFA.
Eg:
It is a NFA but not a DFA because for input symbol “b”, there is no
resultant state.
10.
Soln:
Soln:
12.
Solution:
Soln:
Lexical Analysis:
Lexical Analysis is the first phase when compiler scans the source code.
This process can be left to right, character by character, and group these
characters into tokens. The source program is grouped in meaningful
sequences by identifying the tokens. It makes the entry of the
corresponding tickets into the symbol table and passes that token to
next phase.
Example:
x = y * 20
Tokens
X identifier
= Assignment operator
Y identifier
* Multiplication operator
20 Number
Syntax Analysis
Syntax analysis is the second phase of compilation process. The main
aim of this phase is to make sure that the source code was written by
the programmer is correct or not. It is based on the rules based on the
specific programming language by constructing the parse tree with the
help of tokens. It also determines the structure of source language and
grammar or syntax of the language.
Example:
Example:
float a = 10.5;
float b = a*10;
In the above code, the semantic analyzer will typecast the integer 10 to
float 10.0 before multiplication
Example:
t1 = int_to_float(5)
t2 = rate * t1
t3 = count + t2
total = t3
Code Optimization
This phase removes unnecessary code line and arranges the sequence
of statements to speed up the execution of the program without wasting
resources. The main goal of this phase is to improve on the intermediate
code to generate a code that runs faster and occupies less space.
Example:
a = int_to_float(10)
b=c*a
d=e+b
f=d
will be converted to
b =c * 10.0
f = e+b
Code Generation
Code generation is the last and final phase of a compiler. It gets inputs
from code optimization phases and produces the page code or object
code as a result. The objective of this phase is to allocate storage and
generate relocatable machine code. It also allocates memory locations
for the variable.
Example:
a = b + 60.0
MOVF a, R1
MULF #60.0, R2
ADDF R1, R2
----------------------------------------------------
The number 60
Example:
position: = initial + rate * 60
In the expression, the phrase rate*60 is a logical unit because the usual
conventions of arithmetic expressions tell us that multiplication is
performed before addition.
Example: