Syntax-Directed Translation | Compiler Design - Computer Science Engineering (CSE) PDF Download

Syntax Directed Translation

Background : Parser uses a CFG(Context-free-Grammer) to validate the input string and produce output for next phase of the compiler. Output could be either a parse tree or abstract syntax tree. Now to interleave semantic analysis with syntax analysis phase of the compiler, we use Syntax Directed Translation.

Definition
Syntax Directed Translation are augmented rules to the grammar that facilitate semantic analysis. SDT involves passing information bottom-up and/or top-down the parse tree in form of attributes attached to the nodes. Syntax directed translation rules use 1) lexical values of nodes, 2) constants & 3) attributes associated to the non-terminals in their definitions.

Example

E -> E+T | T
T -> T*F | F
F -> INTLIT

This is a grammar to syntactically validate an expression having additions and multiplications in it. Now, to carry out semantic analysis we will augment SDT rules to this grammar, in order to pass some information up the parse tree and check for semantic errors, if any. In this example we will focus on evaluation of the given expression, as we don’t have any semantic assertions to check in this very basic example.

E -> E+T     { E.val = E.val + T.val }   PR#1
E -> T { E.val = T.val } PR#2
T -> T*F { T.val = T.val * F.val } PR#3
T -> F { T.val = F.val } PR#4
F -> INTLIT { F.val = INTLIT.lexval } PR#5

For understanding translation rules further, we take the first SDT augmented to [ E -> E+T ] production rule. The translation rule in consideration has val as attribute for both the non-terminals – E & T. Right hand side of the translation rule corresponds to attribute values of right side nodes of the production rule and vice-versa. Generalizing, SDT are augmented rules to a CFG that associate 1) set of attributes to every node of the grammar and 2) set of translation rules to every production rule using attributes, constants and lexical values.

Let’s take a string to see how semantic analysis happens – S = 2+3*4. Parse tree corresponding to S would be

To evaluate translation rules, we can employ one depth first search traversal on the parse tree. This is possible only because SDT rules don’t impose any specific order on evaluation until children attributes are computed before parents for a grammar having all synthesized attributes. Otherwise, we would have to figure out the best suited plan to traverse through the parse tree and evaluate all the attributes in one or more traversals. For better understanding, we will move bottom up in left to right fashion for computing translation rules of our example.

Syntax directed translation,Compiler Design,GATE,CSE,ITE

Above diagram shows how semantic analysis could happen. The flow of information happens bottom-up and all the children attributes are computed before parents, as discussed above. Right hand side nodes are sometimes annotated with subscript 1 to distinguish between children and parent.
Additional Information
Synthesized Attributes are such attributes that depend only on the attribute values of children nodes.
Thus [ E -> E+T { E.val = E.val + T.val } ] has a synthesized attribute val corresponding to node E. If all the semantic attributes in an augmented grammar are synthesized, one depth first search traversal in any order is sufficient for semantic analysis phase.

Inherited Attributes are such attributes that depend on parent and/or siblings attributes.
Thus [ Ep -> E+T { Ep.val = E.val + T.val, T.val = Ep.val } ], where E & Ep are same production symbols annotated to differentiate between parent and child, has an inherited attribute val corresponding to node T.

The document Syntax-Directed Translation | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Syntax-Directed Translation - Compiler Design - Computer Science Engineering (CSE)

1. What is syntax-directed translation in computer science engineering?
Ans. Syntax-directed translation is a technique used in computer science engineering to automatically generate code or perform other operations based on the syntax of a programming language. It involves associating semantic actions with the production rules of a grammar, allowing for the translation of source code into target code or other desired outputs.
2. How does syntax-directed translation work?
Ans. Syntax-directed translation works by attaching semantic actions to the production rules of a grammar. These semantic actions define the operations or transformations that need to be performed during the translation process. As the input source code is parsed according to the grammar, the semantic actions associated with each production rule are executed, resulting in the desired translation or output.
3. What are the advantages of syntax-directed translation?
Ans. Syntax-directed translation offers several advantages in computer science engineering. Firstly, it provides a systematic and efficient approach to automatically generating code or performing other operations based on the syntax of a programming language. Additionally, it allows for the separation of concerns, as semantic actions can be defined separately from the grammar itself. This makes it easier to modify or extend the translation process without affecting the underlying grammar.
4. Can syntax-directed translation handle complex programming languages?
Ans. Yes, syntax-directed translation can handle complex programming languages. By defining appropriate production rules and associating semantic actions with them, syntax-directed translation can effectively handle the various constructs and features of a programming language. The flexibility of this technique allows for the translation of even highly complex language constructs into target code or other desired outputs.
5. What is the role of semantic actions in syntax-directed translation?
Ans. Semantic actions play a crucial role in syntax-directed translation. They define the operations or transformations that need to be performed during the translation process. These actions can involve generating code, modifying data structures, performing calculations, or any other operation required to achieve the desired translation or output. By associating semantic actions with the production rules of a grammar, syntax-directed translation ensures that the appropriate actions are executed at the right points during the parsing and translation process.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

past year papers

,

Syntax-Directed Translation | Compiler Design - Computer Science Engineering (CSE)

,

ppt

,

study material

,

Exam

,

Summary

,

practice quizzes

,

mock tests for examination

,

Semester Notes

,

Free

,

Objective type Questions

,

Previous Year Questions with Solutions

,

Extra Questions

,

MCQs

,

Sample Paper

,

pdf

,

shortcuts and tricks

,

Syntax-Directed Translation | Compiler Design - Computer Science Engineering (CSE)

,

Important questions

,

Syntax-Directed Translation | Compiler Design - Computer Science Engineering (CSE)

,

video lectures

,

Viva Questions

;