Syntax-directed translation refers to a method of
compiler
In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
implementation where the source language translation is completely driven by the
parser
Parsing, syntax analysis, or syntactic analysis is a process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar by breaking it into parts. The term '' ...
.
A common method of syntax-directed translation is translating a string into a sequence of actions by attaching one such action to each rule of a
grammar
In linguistics, grammar is the set of rules for how a natural language is structured, as demonstrated by its speakers or writers. Grammar rules may concern the use of clauses, phrases, and words. The term may also refer to the study of such rul ...
.
Thus, parsing a string of the grammar produces a sequence of rule applications. SDT provides a simple way to attach
semantics
Semantics is the study of linguistic Meaning (philosophy), meaning. It examines what meaning is, how words get their meaning, and how the meaning of a complex expression depends on its parts. Part of this process involves the distinction betwee ...
to any such
syntax
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
.
Overview
Syntax-directed translation fundamentally works by adding actions to the productions in a
context-free grammar
In formal language theory, a context-free grammar (CFG) is a formal grammar whose production rules
can be applied to a nonterminal symbol regardless of its context.
In particular, in a context-free grammar, each production rule is of the fo ...
, resulting in a Syntax-Directed Definition (SDD).
[Aho, Alfred V. Compilers: Principles, Techniques, & Tools. Boston: Pearson/Addison Wesley, 2007.] Actions are steps or procedures that will be carried out when that production is used in a derivation. A grammar specification embedded with actions to be performed is called a ''syntax-directed translation scheme''
[ (sometimes simply called a 'translation scheme'.)
Each symbol in the grammar can have an ''attribute'', which is a value that is to be associated with the symbol. Common attributes could include a variable type, the value of an expression, etc. Given a symbol ''X'', with an attribute ''t'', that attribute is referred to as ''X''.''t''
Thus, given actions and attributes, the grammar can be used for translating strings from its language by applying the actions and carrying information through each symbol's attribute.
]
Metacompilers
Early metacompilers use the terms syntax-driven and syntax-directed translation in their descriptions. They have metaprogramming
Metaprogramming is a computer programming technique in which computer programs have the ability to treat other programs as their data. It means that a program can be designed to read, generate, analyse, or transform other programs, and even modi ...
language features for outputting code.
See metacompiler, META II
META II is a Domain-specific language, domain-specific programming language for writing compilers. It was created in 1963–1964 by Dewey Val Schorre at University of California, Los Angeles (UCLA). META II uses what Schorre called ''Syntax (progr ...
, and TREE-META.
See also
* Attribute grammar
References
{{Reflist
Compiler construction