CD Model Set-4 Answer Key
CD Model Set-4 Answer Key
3. Eliminate left recursion from the grammar: S->Aa / b, A->Ac / Sd/e. A CO2
To remove left recursion from the grammar of the form : A → Aα | β We rewrite the 1
production rules as:
A → βA' A'→ αA'| ε
Given Grammar: S → Aa | b A → Ac | Sd | ε
after finding indirect left recursion, grammar:
S → Aa | b
A → Ac | Aad | bd | ε here, α = c, ad, β = bd
So, Grammar after removing left recursion = S → Aa | b 1
A → bdA'
A'→ cA'| adA'| ε
4. What is handle pruning? R CO2
1
A rightmost derivation in reverse can be obtained by “handle-pruning”.
The process of discovering a handle & reducing it to the appropriate
left-hand side is called handle pruning
Construct right sentential form and handle for
grammar E → E + E,
E → E * E,
E → ( E ),
E → id
5. Write Three Address Code for the following expression-If A < B then 1 else 0 A CO3
101:if A<B then goto 104 2
102:T1=0
103:goto 105 104:T1=1
105:stop
9. Represent the following in flow graph i=1; sum=0;while (i<=10){sum+=i; i++;} A CO5
2
10. Apply the basic block concepts, how would you representing the dummy blocks S CO5
with no statements indicated in global dataflow analysis?
In global dataflow analysis in compiler design,dummy blocks are introduced to 2
represent certain program structures or behaviors that may not have a direct
equivalent in the original code.
Attributes of tokens:
• The lexical analyzer collects information about tokens into their
associated attributes. The tokens influence parsing decisions and attributes 1
influence the translation of tokens.
• Usually a token has a single attribute i.e. pointer to the symbol table
entry in which the information about the token is kept
Lexical Errors
• A character sequence that cannot be scanned into any valid token is a
lexical error.
1
• Lexical errors are uncommon, but they still must be handled by a
scanner.
• Misspelling of identifiers, keyword, or operators are considered as
lexical errors. Usually, a lexical error is caused by the appearance of some
illegal character, mostly at the beginning of a token.
Lexical error handling approaches
Lexical errors can be handled by the following actions:
• Deleting one character from the remaining input.
• Inserting a missing character into the remaining input.
• Replacing a character by another character.
• Transposing two adjacent characters.
11. A ii) Elaborate in detail about the recognition of tokens U (8) CO1
Recognition of token explains how to take the patterns for all needed
tokens. It builds a piece of code that examines the input string and finds a
prefix that is a lexeme matching one of the patterns.
Rules for conditional statement or branching statement can be given as
follows
Stmt-> if expr then stmt
| If expr then stmtelse stmt
|є
Expr->term relop term
| term
Term->id
|number
For relop, we use the comparison operations of languages like Pascal or
SQL where = is “equals” and <> is “not equals” because it presents an
interesting structure of lexemes.
The terminal of grammar, which are if, then, else, relop, id and numbers
are the names of tokens as far as the lexical analyzer is concerned, the
patterns for the tokens are described using regular definitions.
The patterns for token
digit -->[0,9]
digits -->digit+
number -->digit(.digit)?(e.[+-]?digits)?
letter -->[A-Z,a-z]
id -->letter(letter/digit)*
if --> if
then -->then
else -->else
relop -->< | > | <= | >= |= |= | <>
For easy recognition, keywords are considered as reserved words even
through their
lexeme match with the pattern for identifiers. In addition, we assign the
lexical analyzer the job of
stripping out white space, by recognizing the “token” ws defined by:
ws-> (blank |tab| newline)+
or
delim-> blank| tab | newline
ws->delim+
Here, blank, tab and newline are abstract symbols that we use to express
the ASCII characters of the same names. Token ws is different from the
other tokens in that ,when we recognize it, we do not return it to parser
,but rather restart the lexical analysis from the character that follows the
white space.
(OR)
11. B Construct the minimized DFA for the regular expression (aa*|bb*) A (16) CO1
12. A Construct a SLR parsing table for the following grammar. A (16) CO2
E→E+T|T
T → TF | F
F →F* | a | b
Solution
Step1 − Construct the augmented grammar and number the productions.
(0) E′ → E
(1) E → E + T
(2) E → T
(3) T → TF
(4) T → F
(5) F → F ∗
(6) F → a
(7) (7) F → b.
Step2 − Find closure & goto Functions to construct LR (0) items.
Box represents the New states, and the circle represents the Repeating
State.
Computation of FOLLOW:
We can find out FOLLOW(E) = {+, $}
FOLLOW(T) = {+, a, b, $}
FOLLOW(F) = {+,*, a, b, $}
(OR)
12. B i) Explain in detail about the Context-free Grammar U (8) CO2
Refer model set – 3 answer key
12. B ii) Describe about the parse tree with suitable example U (8) CO2
Refer model set – 3 answer key
E + T
T T * F
T * F F id
F id id
id
2. Traverse the parse tree in post order (left, right, root), executing the
semantic rules as you go. This will result in the postfix expression
“34*52*+”.
3. So, the infix expression “34*+52*” translates to the postfix expression
“34*52*+” using this SDT scheme. Note that this scheme assumes that
the input is a well-formed infix expression and does not handle errors
13. A ii) Explain in detail about syntax directed definitions and its types U (8) CO3
Syntax-directed definition
A syntax-directed definition (SDD) is a context-free grammar
together with attributes and rules. Attributes are associated with grammar
symbols and rules are associated with productions. If X is a symbol and a
is one of its attributes, then we write X.a to denote the value of a at a
particular parse-tree node labeled X. If we implement the nodes of the
parse tree by records or objects, then the attributes of X can be
implemented by data fields in the records that represent the nodes for X.
Attributes may be of any kind: numbers, types, table references, or
strings, for instance. The strings may even be long sequences of code, say
code in the intermediate language used by a compiler.
Annotated Parse Tree – The parse tree containing the values of attributes
at each node for given input string is called annotated or decorated parse
tree.
Inherited and Synthesized Attributes
There are two kinds of attributes for non terminals:
1. A synthesized attribute for a nonterminal A at a parse-tree node
N is defined by a semantic rule associated with the production at N. Note
that the production must have A as its head. A synthesized attribute at
node N is defined only in terms of attribute values at the children of N
and at N itself.
Example:
E --> E1 + T { E.val = E1.val + T.val}
In this, E.val derive its values from E1.val and T.val
(OR)
13. B How could you generate the intermediate code for the flow of control U (16) CO3
statement? Explain with an example
Refer model set – 3 answer key
14. A Illustrate in detail about the code generation algorithm with an U (16) CO4
example
The Code-Generation Algorithm: