Compilier Construction 2 Paper Solution
Compilier Construction 2 Paper Solution
b) What is a cross-compiler?
A cross-compiler is a compiler that generates executable code for a platform different from the one
on which it is running.
1. Lexical Analysis
2. Syntax Analysis
3. Semantic Analysis
5. Code Optimization
6. Code Generation
e) What is a parser?
A parser is a component of the compiler that checks the syntax of a source code by analyzing token
sequences to produce a parse tree.
f) LALR is the best bottom-up parsing method. State and justify True/False.
True. LALR parsers are memory efficient and powerful enough to handle most practical grammars.
2. Loop unrolling
4. Strength reduction
j) What is a sentinel?
A sentinel is a special value that indicates the end of a data structure or simplifies boundary
condition handling.
S → AB
A → BS | b | ε
B' → SSB' | ε
Solution:
FIRST:
FIRST(A) = {b, ε}
FIRST(B) = {a, b}
FOLLOW:
FOLLOW(S) = {$}
Auxiliary Tasks:
Example:
For (a + b) * (a + b),
e) Construct the local & non-local variable scope or accessibility table for the following blocks:
A: X, Y, Z
B: a
C: H, Z
D: i, j
A X, Y, Z None
B a X, Y, Z
C H, Z X, Y, a
D i, j X, Y, Z
A → Ab | b
B → aB | a
Solution:
%{
#include<stdio.h>
%}
%%
[0-9]+ {
sum += num;
\n {
sum = 0;
.;
%%
int main() {
yylex();
return 0;
}
int yywrap() {
return 1;
S → aA | SbB
A → aA | bB
B→b
Solution:
#include<stdio.h>
#include<stdlib.h>
char *input;
void match(char c) {
if (*input == c)
input++;
else {
exit(1);
void S() {
if (*input == 'a') {
match('a');
A();
S();
match('b');
B();
} else {
printf("Error in S\n");
exit(1);
void A() {
if (*input == 'a') {
match('a');
A();
B();
void B() {
match('b');
int main() {
input = "aabb$";
S();
if (*input == '$')
printf("Parsing Successful\n");
else
Continuing with Compiler Construction Paper-2, solving all the questions comprehensively:
a) Define cross-compiler.
A cross-compiler generates code for a platform different from the one it runs on.
c) What is sentinel?
A sentinel is a special value or symbol used to indicate the end of a data structure or simplify
boundary conditions in an algorithm.
1. Simple LR (SLR)
2. Look-Ahead LR (LALR)
3. Canonical LR
j) Define DAG.
A Directed Acyclic Graph (DAG) is used in code optimization to identify and eliminate redundant
computations.
/ \
* *
/\ /\
b ++ d
/\/\
a ca c
Basic Tasks:
Auxiliary Tasks:
L-attributed Grammar: Allows both synthesized and inherited attributes, but inherited
attributes depend only on the parent and left siblings.
Starts with the start symbol. Starts with the input tokens.
Constructs parse tree from root. Constructs parse tree from leaves.
S → 0A2
A → 1A1 | ε
Solution:
o FIRST(S) = {0}
o FOLLOW(S) = {$}
o FIRST(A) = {1, ε}
o FOLLOW(A) = {2, 1}
%{
#include<stdio.h>
%%
[0-9]+ {
sum += num;
\n {
sum = 0;
.;
%%
int main() {
yylex();
return 0;
int yywrap() {
return 1;
S → aSa | sb | ss | b
Solution:
#include <stdio.h>
#include <stdlib.h>
char *input;
void S();
void match(char c) {
if (*input == c)
input++;
else {
exit(1);
void S() {
if (*input == 'a') {
match('a');
S();
match('a');
match('s');
match('b');
} else {
printf("Error in S\n");
exit(1);
int main() {
input = "asa$";
S();
if (*input == '$')
printf("Parsing Successful\n");
else
return 0;
a) Write the steps of creating a lexical analyzer using Lex. Explain the Lex library functions.
Steps:
Lex Functions:
Grammar:
S → Aa | Ab | Bb | Ba
A→ε
B→ε
Solution:
Conclusion:
The grammar is LALR(1) because merging states does not introduce conflicts.
Q4c: For the input expression (2 + 3) * (3 + 4), design SDD and draw the annotated tree using the
given grammar.
Grammar:
L→E
E→E+T|T
T→T*F|F
F → (E) | digit
Semantic Rules:
Annotated Tree:
For (2 + 3) * (3 + 4):
L.val = 35
E.val = 35
/\
E.val=5 T.val=7
| |
T.val=5 F.val=7
/\ /\
F.val=2 F.val=3
E → E + E | E * E | id
Symbol id + * ( ) $
id - >>- >>
+ < ><<>>
* < >><>>
( < <<<=-
) - >>- >>
$ < <<<- -
Q5b: Construct triple and indirect triple for the following string.
Expression: a + b + c + d + e ↑ f & x + b + c
Solution:
1. Triple Representation:
1 + a b
2 + 1 c
3 + 2 d
4 + 3 e↑f
5 & 4 x
6 + 5 b+c
1 (+, a, b)
2 (+, 1, c)
3 (+, 2, d)
4 (+, 3, e ↑ f)
5 (&, 4, x)
Index Statement Pointer
6 (+, 5, b + c)