When to use an abstract or concrete syntax tree?

I do research in compilers. The lexer seems to be very straightforward: take the "sentence" and divide it into words (or tokens). A parser is required to ensure proper grammar. The parser usually takes markers and builds a tree, resulting in the root node (words in sentences, paragraphs, pages, etc.).

From this question, it would seem that the parser will build AST. AST contains only what is needed to execute the code, so things like brackets are not needed because operator precedence is built into AST. AST probably needs a compiler.

But what about converting code from one language to another? Taking a finished language (grammar) or existing grammar and converting it to another, where the rules of operator precedence may or may not be different? Is operator priority "built-in" in CST?

As an example, I can say that I composed the language and wanted to translate it into PHP code. The ternary operator in most languages ​​has associativity from right to left. PHP incorrectly uses associativity from left to right ( more on this here ). I want “my language” to be used from right to left, but as a result, the PHP code must use parentheses to get the correct result in PHP (with Wikipedia link , the result should be “train” instead of “horse”).

So, to translate into CST language would be better? Is operator priority usually built into the CST? Is there something in between? Are there examples of comparing both trees with a simple algebraic equation? Any examples illustrating the ternary operator?

(Is “transcode” the correct term for “translating a programming language”? A Google search causes media conversion.)

What I'm trying to understand is: when is it more appropriate to use one over the other?

+5
source share
1 answer

, , - , . , , langauge , , (, , ).

, . () , .

: , . , PHP- , , .

, , , , . , . , , " , , "; " ". , , , .

, , , AST, , , , , , "", . ; , , , , , . ( ) ; . . ; - ( ). ; , .

- . "" , , , . DMS Software Reengineering Toolkit - . ( ) langauge ( ASTT). , . , .

: , . , SO-: fooobar.com/questions/62557/...

: .

Re "Transcoder": , "-". 40 . "", SO: Cobol/PL1 Java , IMHO - NACA. , ; , , . , - ; " , , ". .

+7

All Articles