Small White said the compiler principle of the simplest minus-c language compiler

Source: Internet
Author: User
Tags eol

Brief introduction

The following 8 speaks of using a manually constructed syntax tree to parse the C language code below:

    a1    0    input(x)    while(a <= x){       a       aa+1;    }    print(sum)

and a compiler should not rely on the user to manually build the corresponding language syntax tree, we need a support to automatically build a syntax tree strategy. This section will show you how to support the automatic building process for a given C language code using the LEX,YACC and symbol tables learned in the previous 1-6 sections and the syntax trees learned in section 7-8.

Motivation

In the calculator supported by the 5th section variable, the syntax parsing for expr is like the following YACC code:

Lines:lines expr EOL {printf ("%g\n", $);   }            |   Lines EOL |               Lines COMMENT |    ; expr:expr PLUS Expr {$$= $+ $; }|expr minus expr{$$= $- $; }|expr Times expr{$$= $* $; }|expr over expr{$$= $/ $; }| LP Expr rp{$$= $; }|'-'Expr%precuminus{$$= - $; }| Number {$$= $;}//$$=$1 can ignored| ID {$$= Sym_table.getvalue ( $);}//get value from Sym_table| ID ASSIGN Expr {sym_table.setvalue ( $, $);$$= $; }//modify The value

It is already known that each of the above lines is the corresponding grammar matching rule (for example, expr PLUS expr) and the action to be performed when the rule matches (in {}, For example $ $ = $1 + $3;). If you modify the action above to create the corresponding syntax expression node, will it be possible to automatically build the syntax tree? For the above expr PLUS expr, the corresponding action can be modified to

$$ = expr.NewRoot(EXPR_NODEOP_EXPRNodeAttr(PLUSInteger$1$3);

Similarly, the execution of other grammar rules can be modified accordingly, so that when an expression syntax parsing is complete, the corresponding expression syntax tree is built.

General overview
    1. Lex and yacc the corresponding lexical, syntactic parsing, and construct the corresponding syntax tree
    2. Tree.h and Tree.cpp are used to support the construction process of the syntax tree, providing a corresponding creation function, which is YACC used
    3. Symtable.h and Symtable.cpp are used to support the building process of the symbol table, providing the creation, access, modification, and other operations of the symbol table, to support variables and possible function extensions.
    4. Only the most basic C language is supported, that is, the 8th section has already been tested. The following: variables, variable assignments, arithmetic logic operations, if statements, while statements, input and OUTPUT statements, expression statements, compound statements (variable declarations are not supported, variable names appear the first time they are added to the symbol table, the default value is 0, using the assignment operator to modify the value of the variable).

The following code can be supported:
It is an iterative method to solve a two-order equation group, and the three parameters of the equation are a,b,c.

void Interation () {//Solve X1, select the initial point at the axis of the curve doubleIndex=(-1.0*b)/(2*a), temp;if(b!=0)//b Not equal to0When iterating {temp=Index;Index=-1.0*(A*temp*temp+C)/b; while((Absolute (Index-temp)) (>accuracy) {temp=Index;Index=-1.0*(A*temp*temp+C)/b; } x1=Index; X2= (-1.0*b)/a-x1; }Elseb=0When ax^2+c=0Direct Solution {x1=sqrt(-1.0*c/a);         x2=-x1; }  }
Not to be continued

Small White said the compiler principle of the simplest minus-c language compiler

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.