Jawaharlal Nehru Technological University Hyderabad

Jawaharlal Nehru Technological University Hyderabad B.Tech III COMPILER DESIGN Previous Question Paper (March/April - 2021 - R16)

B.Tech | III | II | R16 | March/April - 2021

Answer PaperPaper Code: 136AQMaximum Marks: 75Time: 3 hours

Answer any five questions. All questions carry equal marks.

Main

1

How to design and implement a lexical analyzer? Explain with an example.

15 marks

Answer

A lexical analyzer, or scanner, is the first phase of a compiler that reads the input source code as a stream of characters and groups them into meaningful sequences called tokens. The design and implementation process involves three primary stages: defining the lexical structure, constructing the transition diagram, and implementing the scanner.

The design process follows these steps:

  1. Token Specification: Define the patterns for each token type (e.g., keywords, identifiers, constants, operators) using Regular Expressions.
  2. Transition Diagram Construction: Convert the regular expressions into a Non-deterministic Finite Automaton (NFA) using Thompson's construction, then convert the NFA into a Deterministic Finite Automaton (DFA) using the subset construction algorithm.
  3. Minimization: Minimize the DFA states to ensure the most efficient implementation.
  4. Implementation: The scanner can be implemented as a hand-coded transition table or by using automated tools like Lex or Flex, which generate the C code based on the provided regular expression rules.

Consider the example of recognizing an identifier in a programming language. The pattern is defined as a letter followed by zero or more letters or digits. The regular expression is:

letter (letter | digit)*
. The lexical analyzer implements this by transitioning from a start state to an accepting state upon reading a letter, and remaining in the accepting state as long as subsequent characters are letters or digits. If a character like a space or operator is encountered, the scanner stops, returns the identifier token, and retracts the pointer if necessary to begin the next token scan.

In practice, the lexical analyzer maintains a symbol table to store identifiers and uses a buffer to handle lookahead characters, ensuring that the longest possible match (the 'maximal munch' rule) is always selected for each token.

2

Discuss the phases of a compiler indicating the inputs and outputs of each phase in translating the statement “amount = principle + rate * 36.0”.

15 marks

Answer

A compiler translates source code into machine code through a series of sequential phases. The translation of the statement amount = principle + rate * 36.0 is processed as follows:

  1. Lexical Analysis (Scanner): Reads the source code as a stream of characters and groups them into meaningful sequences called lexemes, producing tokens.
    Input: amount = principle + rate * 36.0
    Output: <id, 1> <=> <id, 2> <+> <id, 3> <*> <real, 36.0>
  2. Syntax Analysis (Parser): Checks the token stream against the grammar of the language to construct a hierarchical structure, typically a Syntax Tree.
    Output: A tree where the multiplication node is a child of the addition node, which is a child of the assignment node.
  3. Semantic Analysis: Checks for semantic consistency, such as type checking. It ensures that the operands of the arithmetic operators are compatible.
    Output: An annotated syntax tree where the integer 36 is converted to a float to match the type of the other operands.
  4. Intermediate Code Generation: Produces a machine-independent intermediate representation.
    Output: Three-address code:
    t1 = inttofloat(36.0)
    t2 = rate * t1
    t3 = principle + t2
    amount = t3
  5. Code Optimization: Improves the intermediate code for faster execution and lower resource usage.
    Output: Optimized three-address code (e.g., constant folding if applicable).
  6. Code Generation: Translates the optimized intermediate code into target machine code or assembly language.
    Output: Target assembly instructions (e.g., LOAD, MULT, ADD, STORE).

Additionally, the Symbol Table Manager and Error Handler interact with all phases to maintain information about identifiers and report syntax or semantic errors respectively.

3

Test whether the grammar is LL(1) or not and construct a predictive parsing table for it?

S→AaAb|BaBa|a|b
A→ε
b→ε
15 marks

Answer

Sign in to view the complete answer paper.

4

Construct the LR(0) items for the following grammar:

S→ L=R|R
L→*R|id
R→L

Check whether the above grammar is LR(0) grammar or not.

15 marks

Answer

Sign in to view the complete answer paper.

5

State and explain the syntax directed translation scheme for the desk calculator give the parse tree and the translation for the string (7 + 4) * 249/3 + 26.

15 marks

Answer

Sign in to view the complete answer paper.

6

What is intermediate code representation? Explain quadruples, triples and indirect triples with an example.

15 marks

Answer

Sign in to view the complete answer paper.

7

Explain various storage allocation strategies with an example.

15 marks

Answer

Sign in to view the complete answer paper.

815 marks
a

Construct a DAG for the expression: a+a*(b-c)+(b-c)*d.

7 marks

Answer

Sign in to view the complete answer paper.

b

Explain various notations used in data flow analysis.

8 marks

Answer

Sign in to view the complete answer paper.

About this paper

This page contains the question paper of COMPILER DESIGN from Jawaharlal Nehru Technological University Hyderabad for B.Tech CSE III II, March/April - 2021, R16.

For the solved question paper with answers, switch to the Answer Paper tab.

Other papers in COMPILER DESIGN

Loading Question Papers

Fetching data from database...