100% found this document useful (1 vote)
47 views16 pages

Compiler Design Concepts and Techniques

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
47 views16 pages

Compiler Design Concepts and Techniques

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

UNIT-1

1.) Differetiate between one pass and multi-pass compiler?

2.) Construct minimum state DFA for regular expression (0 + 1)* 00 + 01.

3.) What is Lex? Explain with suitable code.

4.) Write short notes on:

(i) Compiler writing tools

(ii) Role of Lexical Analyzer

5.) What is a compiler?

6.) Explain the different phases of compiler with example.

7.) Convert b (a + b)* a into a DFA.

8.) Consider the following program:

main()

int x, y, z;

z = x + y;

List down the lexemes, tokens and the attributes of the tokens, at the end of
lexical analysis of the above program.

9.) Explain the various phases of compiler with the help of a diagram. Explain
the kind of transformation done on the source program by the individual
phases of the compiler on the statement position = initial + rate * 60.

10.) Write short notes on:

(i) Single pass vs Multi pass compilers


(ii) Bootstrapping

11.) What is symbol table?

12.) Explain the phases that constitute the front end of a compiler.

13.) What is LEX compiler? Write a LEX program to recognize tokens identifier,
number and relational operator.

14.) Construct a DFA over alphabet ∑ = {0,1}, which accepts the set of strings
either start with 01 or end with 01.

15.) Define compiler, interpreter and translator.

16.) Describe the different phases of a compiler for given input string

(b + c) * (b + c) * 2.

17.) Describe the various components of a compiler and also draw the block
diagram of a compiler.

18.)
19.)

20.) Given the language L = {ab, aa, baa}, derive L*. Explain each step in details.

UNIT-2
1.) Differentiate between top down and bottom up parser.

2.) Why there is a need to eliminate left recursion in top down parsing,
construct predictive parsing table for:

S → iEtSS1/a

S1 → eS/ε

E→b

3.) Consider the following grammar:

S → AaAb/ BbBa

A→ε

B→ε
Test whether the grammar is LL(1) or not and construct a predictive
parsing table for it.

4.) ) Write an algorithm to obtain the First() and following table, left factoring
the given grammar:

E→E+T/T

T→ T* F / F

F→ (E) / id

Also find first and follow ( ).

5.) What do you mean by left recursion? Support your answer with example.

6.) Give the rules to find out FIRST and FOLLOW of Non terminals of grammar.
For the given grammar find out FIRST and follow:

E → TE1

E1 → + TE1/ ε

T → FT1

T1 → *FT1

F → (E)/ id

7.) Construct SLR table for the given grammar:

S → AA

S → aA

A→b

8.) Define context free grammar.

9.) Explain shift-Reduce Parser and its working. Parse

the string id + id * id for the grammar


E → E + T/T using

T → T * F/F

F → id

10.) Consider the following grammar:

S→ CC

C → cC

C→d

Draw SLR Parse table.

11.) Write the four component of content free grammar

12.) Consider the following grammar:

S → 1 AB/ ε

A → 1 AC/OC

B → OS

C→1

and test that whether the grammar is LL(1) or not.

13.) Consider the following grammar:

S → AA

A → aA

A→b

and construct the LALR passing table.

14.) Explain the YACC tool in detail.

15.) Compare the Shift Reduce Parser with predictive parser.

16.) Test whether the grammar is LL.(1) or not and construct a predictive
parsing table for it.
S → AaAB/BbBa

A→ε

B→ε

17.) Construct the SLR parsing table for the following

grammar:

S → xAy/xBy/xAz

A → As/b

B→b

18.) Let a Context Free Grammar CFG {N, T, P, S} be N = {S), T= (a, b), Starting
symbol = S. P =S → SS | aSb | ε. One derivation from the above CFG is
"abaabb". Draw the tree representation.

19.) Convert the following CFG into CNF:

S → ASA / aB

A → B/S

B → b/ε.

20.) Explain the SLR algorithm with an example.

UNIT-3
1.) Describe Boolean expression and procedure calls.

2.) What is the role of semantic analyzer? Explain synthesized and inherited
attributes with example.

3.) What is 3 Address code? Translate the expression.


(a + b)*(c + d)+(a + b + c) into:

(i) Quadruples

(ii) Tripal

(iii) Indirect Tripal

Representation with brief explanation.

4.) Write short notes on:

(i) Translation of assignment strategies

(ii) Control flow

5.) For the given grammar

E → E1 + T

E → E1 - T

E→T

T → T1 * F

T →T1/F

T→F

F → (E)

F → num

Give the semantic rule to represent syntax directed definition for arithmatic
expression and draw annotated parse tree for 4-6/3+ 5.

6.) For the given arithmetic expression,

Q : = - b * (c + d)

give the quadruple, triple and indirect triple representation with brief
explanation.

7.) What is dependency graph? Give example.


8.) Explain S-attributed and L-attributed definitions.

Give examples to support your answer.

9.) What are quadruples, triples and indirect triples. Give their examples.

Give the quadruples, triple and indirect triple representation of the statement

X = (a + b)* + -c/d.

10.) Translate the expression a < b and c > d to three-address code.

11.) Write the three address code for the following program fragment:

while (P < q and r > s) do

if P = 1 then q = q + 1

else

while (p < = s) do

P=P+3

12.) Translate the given expression:

A = - b * (c + d)

Into

(i) Quadruples

(ii) Triples

(iii) Indirect Triples

13.) Write short notes on:

(i) Postfix Notation

(ii) Translation of procedure calls.

14.) Define Annotated Parse tree.

15.) For given production:


E → E1 + T

E → E1 - T

E→T

T → (E)

T → id

T → num

Construct the annotated parse tree x + 6 + x.

16.) Consider the following grammar:

E→E+T

E→T

T→T*F

T→F

F → id

And write:

(i) Postfix representation

(ii) Syntax tree representation

(iii) Three address code representation

17.) Compare Parse Tree and Syntax Tree.

18.) Explain in details the construction of a syntax tree for a binary infix
arithmetic expression:

a + a * (b – c) + (b - c) * d

19.) Describe in detail the outcome of Lexical Analysis, Semantic Analysis,


Intermediate Code, Code Optimization and Code Generator, while the
translation of an assignment statement: p = x + v * 60.
20.) What is top down evaluation? Explain.

UNIT-4
1.) What is dynamic storage allocation? Explain allocation strategies.

2.) What is different parameter passing method. Explain with example.

3.) What is the use of symbol table. describe ways to implement symbol table
and write the various fields of symbol table

4.) Explain about Activation tree.

5.) Define local and global variable.

6.) Explain about Activation Record.

7.) Explain run time storage organization.

8.) Explain activation record and its fields.

9.) Discuss the following parameter passing techniques with suitable example:

(i) Call by value

(ii) Call by reference

(iii) Call by name

10.) Explain different data structures used for symbol table organization

11.) Differentiate between static, stack and heap allocation strategies.

12.) Draw the activation tree for the following code:

...

printf("Enter Your Name:");

scanf("%s", username);

show_data(username);

printf("Press any key to continue...");


int show_data(char *user)

printf("Your name is %s", username);

return 0;

13.) Explain Structural, Linear and Hybrid types of intermediate


representations with examples.

14.) Explain in details Lexically-Scoped Symbol Tables.

15.) Explain in detail the strategies for reducing fragmentation in heap


memory.

16.) Explain the working of procedures and its activations and run-time. Write
with suitable example.

17.) Give a brief explanation on static allocation and dynamic allocation with
respective diagram.

18.) Discuss the role of symbol table in run- time environment with example.

UNIT-5
1.) Define the term loop optimization explain any two loop optimization
techniques.

2.) Explain in brief issues in the design of code gencrator.

3.) Write short notes on:

(i) Global data flow analysis

(ii) Register allocation


4.) What is the code optimization? Write a rule of basic block, translate the
following code to basic block and flow graph:

1 PROD = 0

21=1

3 T2 = addr(A)-4

4 T4 = addr(B)-4

5 T1 = 4*i

6 13 = T2[T1]

7 T5 = T4[T1]

8 T6 = T3*T5

9 PROD = PROD + T6

10 i = i + 1

11 IF I < = 20 GOTO 5.

5.) Translate the following code to basic block and flow graph

Sum : = 0

U:=1

While (I <= 10) do

Sum : = sum + a [ 2 * I ]

I:=I+i

Avg = sum/ i

6.) Generate the code sequence for assignment

a: = x - y + x - z + x - z .
7.) What is basic block?

8.) Explain various code optimization techniques.

9.) Consider the following expression, apply the algorithm of code generation
on it.

a = (p + q) - ((r + s) - t)

10.) Describe the flow-graph.

11.) Construct the following expression with its three

address code:

a = (p + q) - ((r+ s) - t)

t1 = p+ q

t2 = r + s

t3 = t2 – t

a = t1 +t2

generate the target code for the above expressions.

12.) For given structure:

Exp(x)

int p = 1;

for(I = 2; i <= x; I ++)

p=p*i

return(p);

Construct basic blocks and control data flow.


13.) What is Global Data Flow Analysis perform with one suitable example.

14.) Explain the Partition Algorithm for Basic Blocks.

15.) Create a Control Flow Graph for the following code and explain the steps
in details:

// assume an external input-output array: int a []

void quicksort( int m, int n) {

Int i, j, v, x; // temps

if (n <= m) return;

i=m-1

j=n

v = a[n];

while(1) {

do i =i + 1;

while( a[i]<v];

do j=j-1;

while(a[j]>v);

if(i>=j) break;

x = a[i];

a[j];

a[j] = x;

} //end while

X = a[i];

a[i] = a[n];

a[n] = x;
quicksort(m,j);

quicksort(i+1,n);

} //end quicksort

16.)

17.) Explain with examples, Target language, IR type, Selection of instruction,


Register allocation and Ordering of instructions with respect to Code
Generator.

18.) Write the differences between local and global optimization.

19.) Explain the following optimizations with example:

(i) Finding local common subexpression

(ii) Dead code elimination

20.) Write short notes on:


(i) The target machine

(ii) Loop in variant computation

Common questions

Powered by AI

In lexical analysis, a lexeme is the smallest sequence of characters that form a meaningful unit such as keywords, identifiers, and operators. Tokens are abstract categories corresponding to these lexemes, such as INTEGER, IDENTIFIER, or PLUS, representing high-level language constructs. Attributes provide additional information about a token which might be needed by the parser or later phases, such as the type of a variable or the value of a constant .

S-attributed definitions involve only synthesized attributes, where attributes of a parent node are derived from the attributes of its children, making them suitable for bottom-up parsing strategies. L-attributed definitions can have both synthesized and inherited attributes, where an attribute of a node can depend on its siblings and parent node, suitable for more complex calculations in top-down parsing. An example of S-attributed can be evaluating arithmetic expressions, while computing type-checking rules in expressions often require L-attributed attributes .

A one-pass compiler processes the source code in a single pass, reading each line only once, which typically limits its capacity to perform complex analysis and optimization. It generates the target code immediately. On the other hand, a multi-pass compiler processes the source code multiple times, allowing each pass to focus on specific tasks such as syntax analysis, semantic analysis, optimization, and code generation. This layered approach enables more sophisticated analysis and optimization, as each pass can build upon the work of the previous ones .

Register allocation in a compiler is performed during code generation by assigning a limited number of CPU registers to hold operands of program variables and intermediates. This allocation impacts performance critically, as efficient register use reduces the need for slower memory access. Techniques such as graph coloring are employed to minimize register usage conflicts. Poor allocation can lead to register spills, where excess values are temporarily stored in memory, negatively impacting execution speed .

Challenges in designing code generators include generating efficient code that optimizes register allocation, instruction selection, and dealing with target architecture specifics like addressing modes. These challenges can be addressed by using techniques such as peephole optimization to improve instruction sequences, leveraging heuristics for better register allocation, and adapting code generation strategies to suit specific processor architecture efficiently. Abstract syntax trees (ASTs) are often used to guide these transformations .

To construct a minimum-state DFA for the regular expression (0 + 1)* 00 + 01, one needs to first convert the regular expression into an NFA using basic constructions for union, concatenation, and Kleene star, followed by applying the subset construction algorithm to transform the NFA into a DFA. Then, minimize the DFA by combining states that are distinguishable to find the smallest DFA accepting the same language .

A Control Flow Graph (CFG) helps in visualizing the flow of control between different instruction blocks in a program, highlighting opportunities for optimization by identifying redundant computations, unreachable code, and enabling loop transformations. For instance, a CFG can expose an idle loop with no effect on program variables, allowing a compiler to perform dead code elimination. In loops, CFGs guide the reordering of instructions to minimize costly operations .

Shift-Reduce Parsers operate using a stack to recognize language constructs by shifting input onto the stack and reducing based on grammar rules, handling conflicts like 'shift-reduce' and 'reduce-reduce' using precedence rules. Predictive Parsers, on the other hand, predict which production to use by looking ahead at the input, relying on non-left recursive grammars and typically requiring FIRST and FOLLOW sets for decision-making. The major difference lies in Shift-Reduce Parsers working better for bottom-up parsing and Predictive Parsers for top-down parsing .

Eliminating left recursion is crucial for top-down parsers, especially recursive descent parsers, as left-recursive grammar can lead to infinite recursion during parsing. To construct a predictive parsing table, one first must convert the grammar to a non-left-recursive form and remove any ambiguity. Then, compute the FIRST and FOLLOW sets, and use these sets to fill in the entries of the parsing table, with each entry indicating which production to use based on the current input symbol and the non-terminal being expanded .

The process begins with defining semantic rules associated with each grammar production to evaluate the expression's semantics directly. For 4-6/3+5, the grammar rules would include E → E + T, E - T, or T, and similar rules for T and F (factors). These rules specify how to evaluate or annotate nodes during parsing. The annotated parse tree for 4-6/3+5 follows these semantic rules to calculate intermediate values at each node based on arithmetic operations, ultimately leading to the expression's evaluation at the root .

You might also like