Modern compiling principle--0th chapter (including code)

Source: Internet
Author: User

"Modern compiling principle", commonly known as, Tiger Book. Because this book on the practice of higher requirements, so chose this book as a compilation of the principles of learning books, want to step-by-step recording down, and finally complete a complete compiler. However, a person always feels very lonely reading. Today read the first chapter of the topic, have not know what to do after reading. Helpless to find a Chinese version of the translation is not as good as the understanding, it is better to see English. Finally went to the night to find someone else to write the first chapter of the job run, only know what to achieve the function. Then you start writing your own bare hands, and there is no logical bug is finished. Oh. Suddenly feel that the online information is too little, so write this series of articles also want to put together like-minded people together to discuss the Tiger book. I graduated half a year, have written wrong also hope everyone correct.

The first chapter of the book, like other foreign books, first discusses the structure of the book, how it needs to be based, and what tools are used. It then describes the various aspects of the compiler. Personally, like this summary and summary of the chapter I generally like to look at the full book and then look back, at that time will have a more profound understanding of the whole book. But the first chapter of the book gives a small exercise, summed up by using the data structure and grammar rules described above to create a function to calculate the number of print and a straight-line program language translator. The procedure is simple, but as an introductory exercise, the individual is really good.

First, this assignment has begun to focus on the programming language itself. Previously, the program language was used as the whole block to understand the logical meaning and function of its expression. But when it comes to writing this program, it makes me pay attention to every word of the language. First of all, the most basic elements of language can be broadly divided into two categories, statements (statement) and expressions (expression). The statement is that there is no numeric value generated after execution, such as a print statement, a transfer statement, and an expression that has a numeric value, arithmetic operations, and the 1+2 generates 3.

Second, I can see how the programming language changes from text to tree structure, and how to manipulate the tree structure to get the final result. Has a perceptual understanding of the compiler.

Here is the code:

Calculate the number of print

#ifndef Maxargs_h_ #define Maxargs_h_"slp.h"int  Maxargs (a_exp); int Maxargs (a_explist); int Maxargs (a_stm a_stm); #endif
#include"Maxargs.h"#include"Slp.h"intMaxargs (a_stm a_stm) {if(A_stm = =NULL) {        return 0 ; }    Switch(a_stm->kind) {     Casea_stm_::a_assignstm:returnMaxargs (a_stm->u.assign.exp);  Casea_stm_::a_printstm:return 1+ Maxargs (a_stm->u.print.exps);  Casea_stm_::a_compoundstm:returnMaxargs (A_STM->U.COMPOUND.STM1) + Maxargs (a_stm->u.compound.stm2); }    return 0 ;}intMaxargs (A_exp a_exp) {if(A_exp = =NULL) {        return 0 ; }    Switch(a_exp->kind) {     CaseA_exp_::a_opexp:returnMaxargs (A_exp->u.op.left) + Maxargs (a_exp->u.op.right);  CaseA_exp_::a_eseqexp:returnMaxargs (a_exp->u.eseq.stm) + Maxargs (a_exp->u.eseq.exp); }    return 0 ;}intMaxargs (a_explist a_explist) {if(A_explist = =NULL) {        return 0 ; }    Switch(a_explist->kind) {     Casea_explist_::a_pairexplist:returnMaxargs (A_explist->u.pair.head) +maxargs (a_explist->u.pair.tail);  Casea_explist_::a_lastexplist:returnMaxargs (a_explist->u.last); }    return 0 ;}

Interpret the language and calculate the result

#ifndef Interpretes_h_#defineInterpretes_h_#include"util.h"#include"Slp.h"typedefstructTable *Table_;structtable{stringID; intvalue; Table_ tail;}; typedefstructIntandtable_ *intandtable;structintandtable_{inti; Table_ table;}; Table_ interstm (a_stm stm, Table_ t); intandtable interexp (a_exp exp, Table_ t); intandtable interexplist (A_expList ExpL ist, Table_ t); Table_ Update (Table_,string,int ) ;intLookup (Table_,string) ;#endif
#include"Interprets.h"#include"util.h"#include"Slp.h"#include<stdio.h>#include<stdlib.h>#include<string.h>Table_ Table (stringIdintValue, Table_ *tail) {Table_ T= Table_ (malloc(sizeof(*T))) ; T->id =ID; T->value =value; T->tail =T; returnT;} Table_ interstm (a_stm stm, Table_ t) {if(STM = =NULL) {        returnNULL;    } Table_ Table;    Intandtable itable;   A_explist tmpexplist; Switch(stm->kind) {    Casea_stm_::a_compoundstm:table= Interstm (stm->U.COMPOUND.STM1, T); returnInterstm (stm->u.compound.stm2, table);  Casea_stm_::a_printstm:tmpexplist= stm->U.print.exps;  while(1)       {           if(Tmpexplist->kind = =a_explist_::a_lastexplist) {itable= Interexp (tmpexplist->u.last, T); printf ("%d", itable->i);  Break ; }           Else{itable= Interexp (tmpexplist->U.pair.head, T); printf ("%d", itable->i); Tmpexplist= tmpexplist->U.pair.tail; }} printf ("\ n" ) ; returnItable->table;  Casea_stm_::a_assignstm:itable= Interexp (stm->u.assign.exp, T); Table= Update (itable->table, Stm->u.assign.id, itable->i); returntable; }   returnNULL;} Intandtable interexplist (a_explist explist, Table_ t) {if(Explist = =NULL) {        returnNULL;    } intandtable tmp; Switch(explist->kind) {     Casea_explist_::a_lastexplist:tmp= Interexp (explist->u.last, T); returntmp;  Casea_explist_::a_pairexplist:tmp= Interexp (explist->U.pair.head, T); TMP= Interexplist (Explist->u.pair.tail, tmp->table); returntmp; }    returnNULL;} Intandtable Interexp (a_exp exp, Table_ t) {if(exp = =NULL) {        returnNULL; } intandtable T1= Intandtable (malloc(sizeof(*T))) ; inttmp; Switch(exp->kind) {    Casea_exp_::a_idexp:t1->i = lookup (t, exp->u.id); T1->table =T; returnT1;  Casea_exp_::a_numexp:t1->i = exp->U.num; T1->table =T; returnT1;  Casea_exp_::a_opexp:t1= Interexp (exp->U.op.left, T); TMP= t1->i; T1= Interexp (Exp->u.op.right, t1->table); Switch(exp->u.op.oper) { Casea_plus:tmp+ = T1->i;  Break ;  Casea_minus:tmp-= t1->i;  Break ;  Casea_times:tmp*= t1->i;  Break ;  Casea_div:tmp/= t1->i;  Break ; } T1->i =tmp; returnT1;  CaseA_exp_::a_eseqexp:t= Interstm (exp->u.eseq.stm, T); T1= Interexp (exp->u.eseq.exp, T); returnT1; }   returnNULL;} Table_ Update (Table_ T,stringS,intv) {Table_ T1= Table_ (malloc(sizeof(*T1)))     ; T1->id =s; T1->tail =T; T1->value =v; returnT1;}intLookup (Table_ T,stringc) {Table_ tmp=T;  while(TMP) {if(Tmp->id = =c) {returnTmp->value; } tmp= tmp->tail; }    return-1 ;}

This is the main function

/*This file is intentionally empty. You should fill it on with yoursolution to the programming exercise. */#include"Prog1.h"#include"Slp.h"#include"util.h"#include"Interprets.h"#include"Maxargs.h"voidMain () {a_stm p; P=prog (); intA =Maxargs (P); printf ("The print number is %d\n", a); Table_ T=NULL; T=interstm (P, t);}

The end result is 2 8 7 80

Modern compiling principle--0th chapter (including code)

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.