What does syntax oriented translation mean?

Can someone explain in plain language what “Syntax, directed translation” means? I started reading this topic from the book of Dragons, but I could not understand. Wiki article also did not help.

+7
source share
2 answers

Simply put, "Directed Translation Syntax" means controlling the entire compilation (translation) process using a syntax recognizer (parser).

Conceptually, the process of compiling a program (translating it from source code to machine code) begins with an analyzer that creates a parsing tree and then converts this parsing tree through a sequence of transformations of a tree or graph, each of which is largely independent, which leads to the final A simplified tree or graphic that moves to receive machine code.

This view, although good in theory, has the disadvantage that if you try to implement it directly, you need enough memory to store at least two copies of the entire tree or graph. Back when the Dragon Book was written (and when a lot of this theory was hashed), computer memories were measured in kilobytes, and 64 KB was a lot. Therefore, compiling large programs can be complicated.

Using Syntax Directed Translation, you organize all the transformations of the graph around the order in which the parser recognizes the parse tree. Instead of creating a complete parsing tree, your parser builds it a bit and then passes these bits to subsequent passes of the compiler, eventually creating a small piece of machine code before continuing with the parsing process to build the next parsing tree . Since at any time only small amounts of the parsing tree (or subsequent graphs) exist, much less memory is required. Since the syntax recognizer is the main sequencer that controls all of this (deciding the order in which events occur), this is called the syntax translation mode.

Since this is such an effective way to preserve memory usage, people even redesigned languages ​​to make it simpler - the ideal creature is to have a “Single Pass” compiler that could actually do the whole process from parsing to generating machine code in one go.

Currently, memory does not have such a bonus, so there is less pressure to force everything in one pass. Instead, you usually use Syntax Direct Translation only for the front-end, parsing syntax, performing type checking and other semantic checks and a few simple conversions from the analyzer and creating some internal form (three address codes, trees or incorrect codes), and then have separate optimizations and backwards that are independent (and therefore not syntax oriented). Even so, you can argue that these later passes are at least partially syntax oriented, as the compiler can be arranged to work on large parts of the input (such as entire functions or modules) by pushing all the passes before continuing next piece of input.

Tools such as yacc are designed around the idea of ​​Directed Translation syntax — the tool generates a syntax recognizer that directly launches code fragments (“actions” in the tool language), since derivatives (fragments of the parsing tree) are recognized without ever creating the actual “tree” . These actions can directly refer to what is logically later passed to the compiler, and then returned to continue parsing. The required main loop that governs all this is the state machine for reading the parser token.

+17
source

Prior to the Dragon Book, compilers with syntax were compiled. META II and TREE META were developed in the 1960s. These are metalanguage compilers (metacompilers). They are top-down parsers using a set of parsing rules that actually function, which returns the success of a failure. Each rule is a logical expression of parsing. The top driving rule determined the contents of the source file. For example, a programming language may consist of a series of declarations. Thus, the top control rule will look like this:

program = $declarations; 

A value of zero or more. $ declare indicates that the source compiled consists of a number of declarations. ads then determined the types of ads. You may need to declare external links, declare data, declare functions or procedures.

 declarations = linkage_decl | data_decl | code_decl; 

Ad types, each of which is a separate rule. The syntax will be determined during code generation. Metacomplexors controlled semantic processing from syntax rules. First in the rule, this is itself. Later, they transformed the source into tree and list structures and passed them to semantic rules, which gave their result. For example, an arithmetic assignment operator can be transformed and passed to a semantic rule.

 asign = id '=' expr ';' :ASSIGN!2 arith_gen(*1); expr = term $(('+':ADD | '-':SUB) term !2); term = factor $(('*':MPY | '//' :REM | '/':DIV) factor!2); factor = id | number | '(' expr ')'; 

It was said that these languages ​​were similar. But this is not the case. These are top-down parsers. arith_gen will then be encoded to recognize the tree structures generated by the built-in tree, ':' and '!' operators. The tree-like notation used is a node, followed by tree branches enclosed in '[' and ']' and separated by the symbol ','. The analytical syntax above defines the mathematical hierarchy of an expression. multiplication, division and the remainder before adding or subtracting. An arithmetic assignment expression is converted to a functional list expression. The syntax directs the conversion of input to functional notation.

 x = a + (b - c) / d; 

creates a tree with functional designations: ASSIGN [x, ADD [a, DIV [SUB [b, c], d]]]

These old metacompilers do not comply with current terminology.

See the Wiki metac II and TREE META metacompiler sections, as well as a page for a discussion of additional information.

+1
source

All Articles