CAT Short Key Material
CAT Short Key Material
3.
14.
15.
Q.No Questions
PART-B
Illustrate the various phases of compiler and trace it with the program segment
(position: =initial rate*60)
1. Lexical Analysis (Scanner)
o Breaks source code into tokens (keywords, identifiers, literals, etc.).
o Removes whitespace/comments.
o Output: Token stream.
2. Syntax Analysis (Parser)
o Checks structure against grammar rules.
1. o Builds a parse/syntax tree.
o Detects syntax errors (e.g., missing semicolons).
3. Semantic Analysis
o Validates meaning (e.g., type checking, variable declaration).
o Uses a symbol table to track identifiers.
o Output: Annotated syntax tree.
4. Intermediate Code Generation
o Converts to platform-independent intermediate code (e.g., three-address
code, abstract syntax tree).
oPrepares for optimization.
5. Optimization
o Improves code efficiency (e.g., dead code elimination, loop
optimization).
o Works on intermediate code.
6. Code Generation
o Produces target machine/assembly code.
o Manages memory/registers and hardware-specific details.
NFA
6.
DFA
DFA Optimization
Infer how to convert the given NFA to a DFA.
For the given transition diagram we will first construct the transition table.
State 0 1
→q0 q0 q1
q1 {q1, q2} q1
δ'([q2], 0) = [q2]
7. δ'([q2], 1) = [q1, q2]
Now we will obtain δ' transition on [q1, q2].
Transition table for the given Non-Deterministic Finite Automata (NFA) is-
8.
Step-01: Let Q’ be a new set of states of the Deterministic Finite Automata (DFA). Let
T’ be a new transition table of the DFA.
Step-02: Add transitions of start state q0 to the transition table T’
Step-05:
Since no new states are left to be added in the transition table T’, so we stop. States
containing q2 as its component are treated as final states of the DFA. Finally,
Transition table for Deterministic Finite Automata (DFA) is
Q.No Questions
PART C
1. Create a Deterministic Finite Automata for the given regular expression. (0+1)* 01
Build an NFA from a regular expression using Thompson's construction.
2.
i)(a/b)* abb (a/b)*.
ii)ab*/ab
6.
7.
Let Σ={ a, b}
Write down the language generated by the following
i) a/b
ii) a*
iii) (a/b)*
iv) (a/b)(a/b)
Given the alphabet Σ = {a, b}, let's analyze the languages generated by each regular
8. expression:
1. a / b
o This denotes either 'a' or 'b'.
o Language: {a, b}
2. a*
o This denotes zero or more occurrences of 'a'.
o Language: {ε, a, aa, aaa, aaaa, ...} (all possible repetitions of 'a',
including the empty string).
3. (a / b)*
o This denotes zero or more occurrences of 'a' or 'b', meaning any string
over {a, b}, including the empty string.
o Language: {ε, a, b, aa, ab, ba, bb, aaa, aab, ...} (all possible strings of any
length over {a, b}).
4. (a / b)(a / b)
o This denotes any two-character string, where each character is either 'a'
or 'b'.
o Language: {aa, ab, ba, bb}
What is meant by handle pruning?
• An Handle of a string is a sub string that matches the right side of production and
9. whose reduction to the nonterminal on the left side of the production represents one
step along the reverse of a rightmost derivation.
• The process of obtaining rightmost derivation in reverse is known as Handle Pruning.
Derive the string and construct a syntax tree for the input string ceaedae using the
grammar
S->SaA|A,
A->AbB|B,
B- >cSd|e
10.
Compute FIRST for all the non-terminals for the following grammar.
S→ (L) | a
L→ L, S | S
11.
Answer:
FIRST(S) = { (, a }
FIRST(L) = { (, a }
Draw the transition diagram for identifier and keyword
12.
Q.N
Questions
o
PART B
Check whether the following grammar can be implemented using predictive parser.
Check whether the string “abfg” is accepted or not using predictive parsing.
A→A
A→aB|Ad
B→bBC|f
C→g
1.
Construct LR (0) parsing table for the grammar. Also assess whether the input
2. string “aabb” is accepted or not.
S→AA
A→aA|b
Analyze and evaluate the leftmost derivation, rightmost derivation, and derivation
tree for the string "aaabbabbba" with respect to the given grammar.
S→aB | bA
A→aS| bAA|a
B→bS | aBB | b
3.
Analyze and evaluate the predictive parsing table for the grammar:
S → (L) | a
L → L, S | S
Then, determine if the string "(a, (a, (a, a)))" will be accepted.
Given Grammar:
1. S → (L) | a
2. L → L, S | S
Step 1: Compute FIRST and FOLLOW Sets
FIRST Sets:
• FIRST(S): { '(', 'a' }
• FIRST(L): { '(', 'a' } (since L → S and FIRST(S) = { '(', 'a' })
4.
FOLLOW Sets:
• FOLLOW(S): { ')', ',', '$' }
• FOLLOW(L): { ')' }
Step 2: Construct the Predictive Parsing Table
Non-Terminal ( a ) , $
S S → (L) S→a - - -
L L→S L→S - L → L, S -
• The table is conflict-free, making it LL(1) valid.
5.
Construct LR(0) items for this following grammar and draw the transition Representing
transition among CLR items
S→CC
C→cC
C→d
7
Generate SLR Parsing table for the following grammar.
S->Aa|bAc|Bc|bBa
A-> d
B->d
8
Q.N
Questions
o
PART- C
Create a Parsing table for the grammar and find states made by predictive parser on input
“id + id * id” and find FIRST and FOLLOW.
E -> E + T | T
T -> T * F | F
F -> (E)
F-> id
1.
Compose the analysis to determine if the following grammar is LL(1):
S → iEtS | iEtSeS | a
E→b
Additionally, formulate the FIRST and FOLLOW procedures.
2.
UNIT III : INTERMEDIATE CODE GENERATION
Q.N
Questions
o
PART A
Write SDT for Arithmetic and Boolean expressions
Arithmetic
E → E + T { E.val = E₁.val + T.val; }
E → E - T { E.val = E₁.val - T.val; }
E→T { E.val = T.val; }
Boolean
E → id { E.val = lookup(id.lexeme); } // Fetch value from symbol table
E → num { E.val = num.lexval; }
2.
5.
6.
3.