Professional Documents
Culture Documents
3) Define DFA
4)Define NFA
7) Define tokens,,
8)Role of lexical analyzer ?
9 QUESTION..
10_INPUT BUFFERING EXPLAIN ??
11 question-error recovery technique for parser
MODULE 3
1) DEFINE CFG
2) DEFINE AMIGUITY?
1 Define PDA.
7 question
8 turning machine
9)short notes
10 issues of code generator
11 recursive language
12) short notes -
Comparison:
16TH QUESTION
.Explain synthesized attribute, inherited attribute, S-attributed definition and L-attributed
definition with example for each.
Attribute Grammars: Attribute Grammars are formalisms used to define attributes associated
with the nodes of a syntax tree in a programming language compiler. These attributes can be
classified into two main categories: synthesized attributes and inherited attributes. Additionally,
S-attributed and L-attributed definitions are special cases of attribute grammars that impose
restrictions on how attributes are computed and propagated.
1. Synthesized Attribute:
o Definition: Synthesized attributes are attributes associated with a node in the
syntax tree that are computed solely from attributes of its children nodes.
o Example: In a simple arithmetic expression syntax tree, the attribute value of a
node representing an arithmetic operation can be a synthesized attribute
computed from the value attributes of its child nodes representing operands.
2. Inherited Attribute:
o Definition: Inherited attributes are attributes associated with a node in the syntax
tree that are passed down from its parent node or other ancestor nodes.
o Example: Consider a syntax tree representing a programming language function.
The attribute returnType of a function node can be an inherited attribute
passed down from the parent node representing the function declaration.
3. S-Attributed Definition:
o Definition: An attribute grammar is said to be S-attributed if every attribute is
synthesized and can be evaluated in a single depth-first traversal of the syntax
tree.
o Example: In an S-attributed grammar for a simple arithmetic expression, the
attribute type of a node representing an expression can be synthesized from
the type attributes of its child nodes representing operands.
4. L-Attributed Definition:
o Definition: An attribute grammar is said to be L-attributed if it is S-attributed and
allows inherited attributes to be used in the definition of synthesized attributes.
o Example: In an L-attributed grammar for a programming language, the
attribute scope of a node representing a block can be inherited from the parent
node representing the enclosing scope and used to synthesize attributes such
as variableType within the block.
Summary:
• Synthesized attributes are computed from child nodes, while inherited attributes are
passed down from parent or ancestor nodes.
• S-attributed grammars allow only synthesized attributes and can be evaluated in a single
traversal.
• L-attributed grammars extend S-attributed grammars by allowing inherited attributes to
influence synthesized attributes, enabling more complex attribute computations.
These concepts play a crucial role in the design and implementation of compilers and language
processors, facilitating the analysis and transformation of programs during compilation.
17TH QUESTION
What is three address code? Explain different ways of representing three
address codes with examples.
Three Address Code (TAC): Three Address Code is an intermediate representation used in
compilers to break down complex expressions into simpler operations with at most three
operands. Each instruction in TAC typically contains three addresses: two for operands and one
for the result. TAC simplifies code generation and optimization by providing a structured format
for expressing computations.
Different Ways of Representing Three Address Codes:
1. Quadruples:
o In quadruples representation, each TAC instruction is represented by a tuple of
four fields: operator, operand1, operand2, and result.
o Example:
o 1. ADD, A, B, T1
o 2. SUB, T1, C, T2
o 3. MUL, T2, D, T3
2. Triples:
o Triples representation extends quadruples by grouping related instructions into
triples, where each instruction has three fields: operator, operand1, and operand2.
o Example:
o 1. ADD, A, B
o 2. SUB, T1, C
o 3. MUL, T2, D
3. Indirect Triples:
o Indirect triples representation uses pointers to operands instead of actual
operand values, reducing redundancy in the code.
o Example:
o 1. ADD, &A, &B
o 2. SUB, &T1, &C
o 3. MUL, &T2, &D
4. DAG (Directed Acyclic Graph):
o DAG representation optimizes common subexpressions by creating a directed
acyclic graph where nodes represent expressions and edges represent
dependencies.
o Example:
o +
o / \
o A B
In this DAG, the expression "A + B" is represented with nodes for A, B, and the
addition operation.
5. Syntax Trees:
o Syntax trees represent TAC as a tree structure where internal nodes represent
operators and leaf nodes represent operands.
o Example:
o +
o / \
o A *
o / \
o B C
Each representation of TAC has its advantages and use cases in compiler design and
optimization. The choice of representation depends on the specific requirements of the compiler
and the optimizations to be applied during code generation.