0% found this document useful (0 votes)
35 views1 page

Compiler Design (All Modules) - 16

Uploaded by

ag2896323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views1 page

Compiler Design (All Modules) - 16

Uploaded by

ag2896323
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

By understanding these error recovery techniques, parser designers can create robust systems that gracefully handle

unexpected input, ensuring smoother program execution and data processing even when errors occur.

Q11: Syntax directed tramlation: Slrtax directed definitions, Synthesized and inherited attributes, Construction of syntax
trees

Ans: Syntax-directed translation (SDT) bridges the gap between the syntax (structure) and semantics (meaning) of a
programming language. It allows us to define the grammar of a language while simultaneously specifying actions to be
taken during parsing, essentially translating the input program into another form (like machine code or an intermediate
representation).

Here's a breakdown of key concepts in SDT:

1. Syntax Directed Definitions:

 Combine a context-free grammar (CFG) with semantic rules to define the language and its translation behavior.
 The CFG defines the legal structures (syntax) of the program.
 Semantic rules are attached to grammar productions and specify actions to be performed during parsing. These
actions can:
o Synthesize attributes: Compute and assign a value to an attribute associated with a grammar symbol
(often used for bottom-up parsing).
o Inherit attributes: Access and utilize the values of attributes inherited from parent nodes in the parse
tree (often used for top-down parsing).

2. Synthesized and Inherited Attributes:

 Synthesized attributes: Represent values computed during parsing based on information from child nodes in the
parse tree. They are typically assigned a value at the bottom-up parsing stage. (e.g., The type of an expression
can be synthesized based on the types of its operands)
 Inherited attributes: Represent information passed down from parent nodes in the parse tree to guide the
computation of synthesized attributes in child nodes. They are typically used during top-down parsing. (e.g., A
loop counter variable's initial value might be inherited by all statements within the loop's body)

3. Construction of Syntax Trees:

 During parsing, an abstract syntax tree (AST) is often constructed to represent the program's structure.
 The AST captures the relationships between grammar symbols based on the parsing process.
 Semantic rules can be attached to nodes in the AST to perform further analysis or code generation.

Benefits of SDT:

 Allows for a clear separation between syntax and semantics, promoting modularity and maintainability.
 Enables efficient parsing and code generation by associating actions with grammar productions.
 Provides a powerful framework for building compilers and interpreters.

Example (Illustrative, not specific to a particular language):

Program -> statement_list


statement_list -> statement ; statement_list | statement
statement -> id = expression

// Semantic rule (synthesized attribute): compute the type of the expression and assign it to the type attribute of the "id"
symbol
expression: ... { type(id) = type(expression) }

In this simplified example, the semantic rule attached to the expression production synthesizes the type of the expression
and assigns it to the type attribute of the id symbol. This information can be used later for type checking or code
generation.

You might also like