Jawaharlal Nehru Technological University Hyderabad
Jawaharlal Nehru Technological University Hyderabad B.Tech III COMPILER DESIGN Previous Question Paper (March/April - 2021 - R16)
Answer any five questions. All questions carry equal marks.
Main
How to design and implement a lexical analyzer? Explain with an example.
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:
- Token Specification: Define the patterns for each token type (e.g., keywords, identifiers, constants, operators) using Regular Expressions.
- 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.
- Minimization: Minimize the DFA states to ensure the most efficient implementation.
- 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:
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.
Discuss the phases of a compiler indicating the inputs and outputs of each phase in translating the statement “amount = principle + rate * 36.0”.
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:
- 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> - 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. - 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. - Intermediate Code Generation: Produces a machine-independent intermediate representation.
Output: Three-address code:t1 = inttofloat(36.0)t2 = rate * t1t3 = principle + t2amount = t3 - Code Optimization: Improves the intermediate code for faster execution and lower resource usage.
Output: Optimized three-address code (e.g., constant folding if applicable). - 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.
Test whether the grammar is LL(1) or not and construct a predictive parsing table for it?
A→ε
b→ε
Answer
Sign in to view the complete answer paper.
Construct the LR(0) items for the following grammar:
L→*R|id
R→L
Check whether the above grammar is LR(0) grammar or not.
Answer
Sign in to view the complete answer paper.
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.
Answer
Sign in to view the complete answer paper.
What is intermediate code representation? Explain quadruples, triples and indirect triples with an example.
Answer
Sign in to view the complete answer paper.
Explain various storage allocation strategies with an example.
Answer
Sign in to view the complete answer paper.
Construct a DAG for the expression: a+a*(b-c)+(b-c)*d.
Answer
Sign in to view the complete answer paper.
Explain various notations used in data flow analysis.
Answer
Sign in to view the complete answer paper.