main( int argc, char * argv[] ) { TreeNode * syntaxTree; char pgm[120]; /* source code file name */ if (argc != 2) // 检查参数个数 { fprintf(stderr,"usage: %s <filename>\n",argv[0]); exit(1); } strcpy(pgm,argv[1]) ; if (strchr (pgm, '.') == NULL) // 自动检测补全后缀 strcat(pgm,".tny"); source = fopen(pgm,"r"); if (source==NULL) { fprintf(stderr,"File %s not found\n",pgm); exit(1); } // 输出导至标准输出 listing = stdout; /* send listing to screen */ fprintf(listing,"\nTINY COMPILATION: %s\n",pgm); #if NO_PARSE while (getToken()!=ENDFILE); // 关键函数1 #else // parse()自己调用了getToken() syntaxTree = parse(); // 关键函数2 if (TraceParse) { fprintf(listing,"\nSyntax tree:\n"); printTree(syntaxTree); } #if !NO_ANALYZE if (! Error) // Error是一个全局变量,parse()会将状态写在这里 { if (TraceAnalyze) fprintf(listing,"\nBuilding Symbol Table...\n"); buildSymtab(syntaxTree); // 关键函数3 if (TraceAnalyze) fprintf(listing,"\nChecking Types...\n"); typeCheck(syntaxTree); // 关键函数4 if (TraceAnalyze) fprintf(listing,"\nType Checking Finished\n"); } // 上面这两个函数可以看成一个整体,输入是一棵语法树,输出是一颗带标记的语法树,和一张符号表 #if !NO_CODE if (! Error) // 又是一次错误检查 { char * codefile; // 组建输出文件的文件名 int fnlen = strcspn(pgm,"."); codefile = (char *) calloc(fnlen+4, sizeof(char)); strncpy(codefile,pgm,fnlen); strcat(codefile,".tm"); code = fopen(codefile,"w"); if (code == NULL) { printf("Unable to open %s\n",codefile); exit(1); } codeGen(syntaxTree,codefile); // 关键函数5 fclose(code); } #endif #endif #endif fclose(source); return 0; }
int main(int argc, char *argv[]) { char pgm[20]; if(argc != 2) { fprintf(stderr, "usage: %s <filename>\n", argv[0]); exit(1); } strcpy(pgm, argv[1]); source = fopen(pgm, "r"); if(source == NULL) { fprintf(stderr, "File %s not found\n", pgm); exit(1); } listing = stdout; /* send listing to screen */ fprintf(listing, "\nC-MINUS COMPILATION: %s\n", pgm); #if NO_PARSE while(getToken() != ENDFILE); #else syntaxTree = parse(); if (TraceParse) { fprintf(listing,"\nSyntax tree:\n"); printTree(syntaxTree); } #if !NO_ANALYZE if (! Error) { if (TraceAnalyze) fprintf(listing,"\nBuilding Symbol Table...\n"); buildSymtab(syntaxTree); if (TraceAnalyze) fprintf(listing,"\nChecking Types...\n"); typeCheck(syntaxTree); if (TraceAnalyze) fprintf(listing,"\nType Checking Finished\n"); } #if !NO_CODE if (! Error) { char * codefile; int fnlen = strcspn(pgm,"."); codefile = (char *) calloc(fnlen+4, sizeof(char)); strncpy(codefile,pgm,fnlen); strcat(codefile,".tm"); code = fopen(codefile,"w"); if (code == NULL) { printf("Unable to open %s\n",codefile); exit(1); } codeGen(syntaxTree,codefile); fclose(code); } #endif #endif #endif fclose(source); return 0; return 0; }
void buildSymtab(TreeNode* node,StTable* handle,TreeNode* paraOfFun,ValueType funType,char* funcName){ int i; int count=0; StTable* newHandle; TreeNode* paraNode; StNode* stPtr; StPara* stParaPtr; /*** * compound statement is a new scope * */ if(node->kind==StmtK && node->subkind.stmtType==ComStmt){ /* * create new scope in the symbol table * */ newHandle=stNewScope(handle); newHandle->funType=funType; strcpy(newHandle->funcName,funcName); /*** * if it is the begin of a function, * need to insert the parameters into symbol table * */ while(paraOfFun!=NULL){ paraOfFun->handle=newHandle; stPtr=stInsert(newHandle,paraOfFun->attr.name,paraOfFun->type,paraOfFun->lineno,0); if(stPtr==NULL){ printf("error:line %d:redeclaration of arguement %s\n",node->lineno,node->attr.name); Error=1; } paraOfFun=paraOfFun->sibling; } } else{ newHandle=handle; } node->handle=newHandle; if(node->kind==DeclK){ if(node->subkind.declType==SimVarDcl){ /** * simple variable declaration, insert into symbol table * **/ stPtr=stInsert(node->handle,node->attr.name,node->type,node->lineno,0); if(stPtr==NULL){ printf("error:line %d:redeclaration of variable %s\n",node->lineno,node->attr.name); Error=1; } /* * preorder travel * */ for(i=0;i<MAXCHILDREN;i++){ if(node->child[i]!=NULL){ buildSymtab(node->child[i],node->handle,NULL,funType,funcName); } } if(node->sibling!=NULL){ buildSymtab(node->sibling,handle,NULL,funType,funcName); } } else if(node->subkind.declType==ArrVarDcl){ /** * array declaration * **/ stPtr=stInsert(node->handle,node->attr.name,node->type,node->lineno,0); if(stPtr==NULL){ printf("error:line %d:redeclaration of variable %s\n",node->lineno,node->attr.name); Error=1; } /***** * preorder travel * ***/ for(i=0;i<MAXCHILDREN;i++){ if(node->child[i]!=NULL){ buildSymtab(node->child[i],node->handle,NULL,funType,funcName); } } if(node->sibling!=NULL){ buildSymtab(node->sibling,handle,NULL,funType,funcName); } } else{ /**** * function declaration * ****/ stPtr=stInsert(node->handle,node->attr.name,node->type,node->lineno,1); if(stPtr==NULL){ printf("error:line %d:redeclaration of function %s\n",node->lineno,node->attr.name); Error=1; } /**need to store the parameter with the function name**/ else{ paraNode=node->child[0]; while(paraNode!=NULL){ if(stPtr->para==NULL){ stPtr->para=(StPara*)malloc(sizeof(StPara)); stParaPtr=stPtr->para; stParaPtr->type=paraNode->type; stParaPtr->next=NULL; count++; } else{ stParaPtr->next=(StPara*)malloc(sizeof(StPara)); stParaPtr=stParaPtr->next; stParaPtr->type=paraNode->type; stParaPtr->next=NULL; count++; } paraNode=paraNode->sibling; } stPtr->count=count; /******* * the statement is in new function scope * ********/ buildSymtab(node->child[1],node->handle,node->child[0],node->type,node->attr.name); /******** * actually, no sibling * but for formalize * *******/ if(node->sibling!=NULL){ buildSymtab(node->sibling,handle,NULL,funType,funcName); } } } } else{ /******* * just preorder travel for other case * *******/ for(i=0;i<MAXCHILDREN;i++){ if(node->child[i]!=NULL){ buildSymtab(node->child[i],node->handle,NULL,funType,funcName); } } if(node->sibling!=NULL){ buildSymtab(node->sibling,handle,NULL,funType,funcName); } } }
int main( int argc, char * argv[] ) { /*TreeNode * syntaxTree = NULL;*/ char pgm[255]; /* source code file name */ #ifdef DEBUG source = fopen("J:\\pure_c\\story\\src\\bin\\Debug\\test.str", "rb"); #else if (argc != 2) { fprintf(stderr, "usage: %s <filename>\n", argv[0]); exit(1); } strcpy(pgm, argv[1]); if (strchr (pgm, '.') == NULL) strcat(pgm, ".str"); source = fopen(pgm, "rb"); #endif // DEBUG if (source == NULL) { fprintf(stderr, "File %s not found\n", pgm); exit(1); } listing = stdout; /* send listing to screen */ /*fprintf(listing, "\nSTORY COMPILATION: %s\n", pgm);*/ /*GlobalState *gs; gs.GCdebt = 0; gs.totalBytes = 0; gs.gcoList = NULL;*/ StState *st = StState_Create(ReallocateMemory, NULL); /*st.globalState = &gs; gs.stringTable = StringTable_Create(&st, 20);*/ LexState scanState; scanState.appState = st; scanState.lineNumber = 0; scanState.lookAhead.token = TK_EOS; scanState.currentChar = 0; FileInputBuffer fib; fib.inputFile = source; fib.length = 0; MemoryStream ms; Stream_CreateFileMemoryStream(&ms, &fib); scanState.stream = &ms; MemoryBuffer mbuff; MemoryBuffer* pmbuff = &mbuff; MemoryBuffer_Create(st, pmbuff); scanState.tokenBuffer = pmbuff; #if NO_PARSE Stream_Fill(scanState.stream); scanState.currentChar = *scanState.stream->currentPosition++; Lexer_ScanNextToken(&scanState); while(TRUE) { /*st_debug("token type: %d\n", scanState.currentToken.token);*/ PrintToken(&scanState.currentToken, scanState.tokenBuffer->buffer); if (scanState.currentToken.token == TK_EOS) break; Lexer_ScanNextToken(&scanState); } /*fclose(source);*/ MemoryBuffer_Free(st, pmbuff); /*while (getToken(&scanState) != EOS);*/ #else syntaxTree = parse(); if (TraceParse) { fprintf(listing, "\nSyntax tree:\n"); printTree(syntaxTree); } #if !NO_ANALYZE if (!Error) { if (TraceAnalyze) fprintf(listing,"\nBuilding Symbol Table...\n"); buildSymtab(syntaxTree); if (TraceAnalyze) fprintf(listing, "\nChecking Types...\n"); typeCheck(syntaxTree); if (TraceAnalyze) fprintf(listing, "\nType Checking Finished\n"); } #if !NO_CODE if (! Error) { char * codefile; int fnlen = strcspn(pgm, "."); codefile = (char *) calloc(fnlen+4, sizeof(char)); strncpy(codefile, pgm, fnlen); strcat(codefile, ".tm"); code = fopen(codefile,"w"); if (code == NULL) { printf("Unable to open %s\n",codefile); exit(1); } codeGen(syntaxTree,codefile); fclose(code); } #endif #endif #endif fclose(source); return 0; }
main(int argc, char * argv[]) { // char* pgm = "D:\\code\\Visual Studio\\visual studio 2015\\Projects\\TINY\\tiny\\Debug\\SAMPLE.TNY"; TreeNode * syntaxTree; char pgm[120]; /* source code file name */ if (argc != 2) { fprintf(stderr, "usage: %s <filename>\n", argv[0]); exit(1); } strcpy(pgm, argv[1]); if (strchr(pgm, '.') == NULL) strcat(pgm, ".tny"); //char* pgm = "D:\\code\\Visual Studio\\visual studio 2015\\Projects\\TINY\\tiny\\Debug\\SAMPLE.TNY"; source = fopen(pgm, "r"); if (source == NULL) { fprintf(stderr, "File %s not found\n", pgm); exit(1); } listing = stdout; /* send listing to screen */ fprintf(listing, "\nTINY COMPILATION: %s\n", pgm); #if NO_PARSE while (getToken() != ENDFILE); #else syntaxTree = parse();//first 词法语法分析,构造语法树 if (TraceParse) { fprintf(listing, "\nSyntax tree:\n"); printTree(syntaxTree); printf("语法树OK\n"); } #if !NO_ANALYZE printf("Error?\n"); if (!Error) { printf("no error\n"); if (TraceAnalyze) fprintf(listing, "\nBuilding Symbol Table...\n"); buildSymtab(syntaxTree);//语义分析,构造符号表 if (TraceAnalyze) fprintf(listing, "\nChecking Types...\n"); typeCheck(syntaxTree);//语义分析,执行类型检查 if (TraceAnalyze) fprintf(listing, "\nType Checking Finished\n"); } else printf("it had error\n"); #if !NO_CODE printf("Error?\n"); if (!Error) { printf("no error\n"); char * codefile; int fnlen = strcspn(pgm, "."); codefile = (char *)calloc(fnlen + 4, sizeof(char)); strncpy(codefile, pgm, fnlen); strcat(codefile, ".tm"); code = fopen(codefile, "w"); if (code == NULL) { printf("Unable to open %s\n", codefile); exit(1); } codeGen(syntaxTree, codefile);//完成代码生成 fclose(code); } #endif #endif #endif fclose(source); return 0; }