JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD B.Tech III COMPILER DESIGN Previous Question Paper (August - 2022 - R18)
Answer any five questions
All questions carry equal marks
Main
What are the advantages of a compiler over an interpreter?
Answer
Compilers offer several advantages over interpreters, primarily centered on execution efficiency and software distribution:
- Execution Speed: Compilers translate the entire source code into machine code before execution, allowing the program to run significantly faster than interpreted code, which must be translated line-by-line during runtime.
- Optimization: Compilers perform sophisticated global code optimizations, such as constant folding, loop unrolling, and register allocation, which are generally not feasible in interpreters.
- Intellectual Property Protection: Compiled code is distributed as binary executables, making it difficult for end-users to view or modify the original source code, whereas interpreters typically require the source code to be present.
- Error Detection: Compilers perform comprehensive static analysis, catching syntax and type errors across the entire program before execution begins.
- Resource Efficiency: Once compiled, the program does not require the compiler or an interpreter to be present in memory during execution, reducing the overall system overhead.
Draw the structure of a compiler and describe various phases in the compilation process mention the output of the following statement: id1=id2+id3*50 at each phase.
Answer
A compiler is structured as a series of phases that transform the source program into target machine code. The process is divided into the front-end (analysis) and back-end (synthesis).
Phases of Compilation:
- Lexical Analysis: Converts the stream of characters into tokens.
- Syntax Analysis: Checks the structure against grammar rules and builds a parse tree or abstract syntax tree.
- Semantic Analysis: Checks for type consistency and semantic correctness.
- Intermediate Code Generation: Produces a machine-independent representation (e.g., Three-Address Code).
- Code Optimization: Improves the intermediate code for speed and space.
- Code Generation: Translates the optimized intermediate code into target machine code.
Output for id1 = id2 + id3 * 50:
| Phase | Output |
|---|---|
| Lexical Analysis | <id, 1> <=, > <id, 2> <+, > <id, 3> <*, > <num, 50> |
| Syntax Analysis | Parse Tree (Hierarchical structure with * as child of +) |
| Semantic Analysis | Annotated Parse Tree (Type checking: 50 treated as float if id3 is float) |
| Intermediate Code | t1 = id3 * 50 t2 = id2 + t1 id1 = t2 |
| Code Optimization | t1 = id3 * 50 id1 = id2 + t1 |
| Code Generation | MOV R1, id3 MUL R1, 50 ADD R1, id2 MOV id1, R1 |
Design the LEX program that recognizes the tokens of a C language and returns the token found.
Answer
Sign in to view the complete answer paper.
Give the DFA and NFA to accept the strings containing a, b such that the string contains even number of a’s and odd number of b’s.
Answer
Sign in to view the complete answer paper.
Remove the left recursion for the following grammar and also find FIRSTS and FOLLOWs.
T → T * F | F
F → (E) | id
Answer
Sign in to view the complete answer paper.
Write the steps/algorithm to construct the predictive parser table and explain with an example.
Answer
Sign in to view the complete answer paper.
Construct the Recursive Descent Parser with backtracking for the following grammar:
Answer
Sign in to view the complete answer paper.
Compute LR(0) items for the following grammar and construct SLR parser table:
L → *R | id
R → L
Answer
Sign in to view the complete answer paper.
Construct the syntax directed definition to convert infix notation into postfix notation.
Answer
Sign in to view the complete answer paper.
Describe different ways of implementing intermediate code generation of a three-address statement.
Answer
Sign in to view the complete answer paper.
Explain how an L-attributed grammar is converted into a translation scheme.
Answer
Sign in to view the complete answer paper.
Compare and contrast S-Attributed definitions with L-Attributed definitions.
Answer
Sign in to view the complete answer paper.
How is stack storage allocation strategy different from heap allocation strategy? Describe them mentioning their merits and demerits.
Answer
Sign in to view the complete answer paper.
Explain the foundations and basic notations used in data-flow analysis for optimizations with examples.
Answer
Sign in to view the complete answer paper.