JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD
JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD B.Tech III PRINCIPLES OF COMPILER CONSTRUCTION Previous Question Paper (August/September - 2021 - R18)
Answer any five questions. All questions carry equal marks.
Main
Discuss various phases of compiler in detail. Give the output of each phase of the compiler for the statement.
Answer
A compiler is a program that translates source code into machine code through several distinct phases. For the statement position = initial + rate * 60, the phases are:
- Lexical Analysis: Reads the source code and converts it into a stream of tokens. Output: <id, 1> <=> <id, 2> <+> <id, 3> <*> <60>
- Syntax Analysis: Groups tokens into a hierarchical structure, typically a parse tree. Output: A tree with an assignment node at the root, with 'position' on the left and an expression tree on the right.
- Semantic Analysis: Checks for semantic consistency, such as type checking. Output: The tree is updated, for example, by inserting a conversion node to change the integer 60 to a float.
- Intermediate Code Generation: Produces a machine-independent intermediate representation. Output: t1 = inttofloat(60); t2 = id3 * t1; t3 = id2 + t2; id1 = t3
- Code Optimization: Improves the intermediate code for efficiency. Output: t1 = id3 * 60.0; id1 = id2 + t1
- Code Generation: Translates the optimized code into target machine code. Output: MOVF id3, R2; MULF #60.0, R2; ADDF id2, R2; MOVF R2, id1
The Symbol Table Manager and Error Handler interact with all phases throughout the process.
Write briefly about the specification of tokens.
Answer
The specification of tokens involves defining the patterns that identify valid lexemes in a source language. This is formally achieved using Regular Expressions, which provide a concise notation for describing sets of strings.
Key aspects include:
- Alphabet: A finite set of symbols (e.g., ASCII characters).
- Strings and Languages: Sequences of symbols from the alphabet; a language is a set of such strings.
- Regular Definitions: Used to simplify complex patterns by giving names to regular expressions (e.g., defining 'letter' as [a-zA-Z] and 'digit' as [0-9]).
- Token Patterns: Specific rules such as identifier = letter(letter|digit)* or number = digit+(.digit+)?(E(+|-)?digit+)?.
These specifications are used by the lexical analyzer generator (like Lex or Flex) to construct a deterministic finite automaton (DFA) that recognizes the tokens during the scanning process.
Briefly explain about compiler construction tools.
Answer
Sign in to view the complete answer paper.
Convert the following regular expression to DFA.
Answer
Sign in to view the complete answer paper.
Check whether the following grammar is LALR(1) or not?
Answer
Sign in to view the complete answer paper.
What is FIRST and FOLLOW? Specify the steps to compute FIRST and FOLLOW with an example.
Answer
Sign in to view the complete answer paper.
What are the difficulties in top down parsing? And give solutions to overcome these difficulties.
Answer
Sign in to view the complete answer paper.
Construct LR (0) parser for the following grammar.
Answer
Sign in to view the complete answer paper.
Create three address codes for the statements:
if c < d then
x := y + z
else
x := y - z
Answer
Sign in to view the complete answer paper.
Construct the syntax directed definition for desk calculator.
Answer
Sign in to view the complete answer paper.
Explain what are semantic errors and how semantic analysis is performed?
Answer
Sign in to view the complete answer paper.
Apply the S-attributed definition and construct syntax tree for a simple expression, grammar involving only the binary operators + and -. As usual, these operators are at the same precedence level and are jointly left associative. All non-terminals have one synthesized attribute node, which represents a node of the syntax tree.
Answer
Sign in to view the complete answer paper.
Distinguish the static and dynamic storage allocation.
Answer
Sign in to view the complete answer paper.
For the following expression obtain optimal code using:
i) Only two registers
ii) Only one register
Answer
Sign in to view the complete answer paper.
Compare local optimization with global optimization. Give suitable examples.
Answer
Sign in to view the complete answer paper.
Explain global data flow analysis with necessary equations.
Answer
Sign in to view the complete answer paper.