Semantic analysis and intermediate code generation

//1. The semantics of the programming language needs (context-sensitive grammar) to describe

Semantic analysis, review the static semantics of each grammatical component, if the static semantics are correct, generate intermediate code equivalent to the language component, or directly generate the target code.

Dynamic semantic checking needs to generate corresponding target code, which is performed at runtime.
Static semantic checking is done at compile time.

Class check
Control flow check
Consistency check

Attribute grammar is used as a tool to describe the semantics of programming languages
and uses grammar-guided translation methods to complete the translation of grammatical components.

Grammar-guided translation is divided into bottom-up grammar-guided translation and top-down grammar-guided translation.
Bottom-up is reduction, top-down is matching

The expanded analysis stack can store three types of information,
analysis status, grammar symbols, and semantic values ​​corresponding to grammar symbols

The attributes of grammar symbols can be divided into two types: inherited attributes and comprehensive attributes.
Inherited attributes are used to convey information "from top to bottom".
Comprehensive attributes belong to the "bottom-up" transmission of information.

Attribute grammar: a context-free grammar with semantic attributes.
Attribute grammar is also a translation grammar. Attributes help specify the code generation actions in the grammar in more detail.

Abstract syntax tree

Abstract syntax tree is also called graph type.
Abstract syntax shows an operation process and at the same time describes the hierarchical structure of the source program.

A notable feature of abstract syntax trees is that they are compact, easy to construct and have a small number of nodes.

Reverse Polish

BL: Turn to a certain label
BT: Transfer if the condition is true
BF: Transfer if the condition is false
BR: Transfer if unconditional

Quaternion

Operands, left operands, right operands, results.
All operators that require only one operand use arg1.

The exercises between quaternions are realized through temporary variables.

Guess you like

Origin blog.csdn.net/weixin_44522477/article/details/112155848