📄 main.c
字号:
/* main.c
* Main program for TINY compiler
*
* Compiler Construction: Principles and Practice
* Kenneth C. Louden
* 编译原理及实践
* (美) Kenneth C. Louden 著
* 冯博琴 冯岚 等译
* 机械工业出版社 IBSN 7-111-07703-2
* 源代码:zwf编辑并修订
* Code Modify:
*/
#include "globals.h"
/* define NO_PARSE to get a scanner-only compiler */
/*#define NO_PARSE */
/* be defined in makefile Larry */
/* define NO_ANALYZE to get a parser-only compiler */
/*#define NO_ANALYZE */
/* be defined in makefile Larry */
/* define NO_CODE to get a compiler that does not generate code */
/*#define NO_CODE */
/* be defined in makefile Larry */
/* And define CHINESE open Chinese(GB2312) support (non GBK) */
#include "util.h"
#ifdef NO_PARSE //创创建只扫描的编译器
#include "scan.h"
#else
#include "parse.h"
#ifndef NO_ANALYZE //创创建只分析和扫描的编译器
#include "analyze.h"
#ifndef NO_CODE //创建执行语义分析但不生成代码的编译器
#include "cgen.h"
#endif
#endif
#endif
/* allocate global variables */
int lineno = 0;
FILE *source;
FILE *listing;
FILE *code;
/* allocate and set tracing flags */
int EchoSource = TRUE;
int TraceScan = TRUE;
int TraceParse = TRUE;
int TraceAnalyze = TRUE;
int TraceCode = TRUE;
int Error = FALSE;
int main(int argc, char *argv[])
{
TreeNode *syntaxTree;
char pgm[256]; /* source code file name */
if (argc != 2) {
#ifdef CHINESE
fprintf(stderr, "用法: %s <文件名>\n", argv[0]);
#else
fprintf(stderr, "usage: %s <filename>\n", argv[0]);
#endif
return 1;
}
strcpy(pgm, argv[1]);
if (strchr(pgm, '.') == NULL)
strcat(pgm, ".tny");
source = fopen(pgm, "r");
if (source == NULL) {
#ifdef CHINESE
fprintf(stderr, "文件 %s 不存在\n", pgm);
#else
fprintf(stderr, "File %s not found\n", pgm);
#endif
return 1;
}
listing = stdout;
#ifdef CHINESE
fprintf(listing, "郑卫飞编辑并改写\n微型编译器: %s\n", pgm);
#else
fprintf(listing, "\nTINY COMPILATION: %s\n", pgm);
#endif
#ifdef NO_PARSE
while (getToken() != ENDFILE);
#else
syntaxTree = parse();//第1遍构造语法树的扫描程序和分析程序
if (TraceParse) {
#ifdef CHINESE
fprintf(listing, "\n语法树:\n");
#else
fprintf(listing, "\nSyntax tree:\n");
#endif
printTree(syntaxTree);
}
#ifndef NO_ANALYZE
if (!Error) {
#ifdef CHINESE
fprintf(listing, "\n建造符号表...\n");
#else
fprintf(listing, "\nBuilding Symbol Table...\n");
#endif
buildSymtab(syntaxTree);
#ifdef CHINESE
fprintf(listing, "\n检查类型...\n");
#else
fprintf(listing, "\nChecking Types...\n");
#endif
typeCheck(syntaxTree);
#ifdef CHINESE
fprintf(listing, "\n类型检查结束\n");
#else
fprintf(listing, "\nType Checking Finished\n");
#endif
}
#ifndef 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) {
#ifdef CHINESE
fprintf(listing, "无法打开文件 %s\n", codefile);
#else
fprintf(listing, "Unable to open %s\n", codefile);
#endif
return 1;
}
codeGen(syntaxTree, codefile);
fclose(code);
}
#endif /* !NO_CODE */
#endif /* !NO_ANALYZE */
#endif /* NO_PARSE */
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -