Construct a script engine (5) that uses a C language to check the syntax, generate a syntax tree, and generate code.

Source: Internet
Author: User
Construct a script engine using C language (5)

Author: kevin_qing

Please note

Syntax check, constant merging and the new idiom method tree are implemented in reduce normalization functions.

Syntax tree node Definition

Struct gtreenode {

Uint32_t type;

Uint32_t value;

};

Struct gtreenode1: Public gtreenode {

Gtreenode * Child1;

};

Struct gtreenode2: Public gtreenode1 {

Gtreenode * child2;

};

Struct gtreenode3: Public gtreenode2 {

Gtreenode * Child3;

};

Struct gtreenoden: Public gtreenode {

Array <gtreenode> Childs;

};

Type indicates the node type, such as if switch op_2 op_1 smt_seq (statement sequence) cpd_smt (composite statement) Case VAR numbers.

Value is a value of the corresponding type, such as the case label, string ID, constant value, and op_2 operation.

Gtreenode1 corresponds to the 1 RMB operation, such as not

Gtreenode2 corresponds to 2 meta operations, such as IF-SMT, +-*/% and other arithmetic logic operations

Gtreenode2 corresponds to a 3 yuan operation, typically a IF-SMT-ELSE-SMT

Sequence of gtreenoden statements

The syntax tree is generated inside the Statute function, and sub-nodes are added through stacks. Similar to token.

When a syntax tree node is generated, you can perform a semantic check and check whether the operands are constants. Instead, you can perform a constant merge optimization code.

When generating code, traverse the syntax tree in sequence. It is mainly because lable requires backtracking, and there are no other difficulties.

To be continued

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.