Syntax Directed Translation




Syntax Directed Translation

  • The Principle of Syntax Directed Translation states that the meaning of an input sentence is related to its syntactic structure, i.e., to its Parse-Tree.
  • By Syntax Directed Translations we indicate those formalisms for specifying translations for programming language constructs guided by context-free grammars.
    • We associate attributes to the grammar symbols representing the language constructs.
    • Values for attributes are computed by semantic rules associated with grammar productions.
    • In the semantic rule, attribute is VAL and an attribute may hold anything like a string, a number, a memory location and a complex record.
 Syntax Directed Translation

Syntax Directed Translation

  • Evaluation of Semantic Rules may:
    • Generate code ,
    • Insert information into the symbol table ,
    • Perform semantic check ,
    • Issue error messages.
  • There are two notations for attaching semantic rules:
    • Syntax Directed Definitions - High-level specification hiding many implementation details (also called Attribute Grammars).
    • Translation Schemes - More implementation oriented: Indicate the order in which semantic rules are to be evaluated.
 Notations for Semantic Rules

Notations for Semantic Rules

Example

Production Semantic rule
E → E + T E.val := E.val + T.val
E → T E.val := T.val
T → T * F T.val := T.val + F.val
T → F T.val := F.val
F → (F) F.val := F.val
F → num F.val := num.lexval

E.val is one of the attributes of E.
num.lexval is the attribute returned by the lexical analyzer.



Related Searches to Syntax Directed Translation