C_Minus.c
资源名称:C语言编译器.rar [点击查看]
上传用户:wqdrylgs
上传日期:2007-02-09
资源大小:65k
文件大小:2k
源码类别:
汇编语言
开发平台:
WINDOWS
- #include "globals.h"
- #include "util.h"
- #include "scan.h"
- #include "parse.h"
- #include "symtab.h"
- #include "analyze.h"
- #include "CodeGen.h"
- #define NO_ANALYZE FALSE
- /* allocate global variables */
- int lineno = 0;
- FILE * source;
- FILE * listing;
- FILE * inter_code;
- /* allocate and set tracing flags */
- int TraceScan = TRUE;
- int TraceParse = TRUE;
- int TraceAnalyze = TRUE;
- int Error = FALSE;
- int main(int argc, char * argv[])
- {
- TreeNode * syntaxTree;
- char cfile[20], codefile[20]; /* sourse code file name */
- if(argc != 2)
- {
- fprintf(stderr,"Usage: %s <filename>n",argv[0]);
- exit(1);
- }
- strcpy(codefile, argv[1]);
- if (strchr(codefile, '.') != NULL) {
- int len = strcspn(codefile, ".");
- strncpy(cfile, codefile, len);
- cfile[len] = ' ';
- }
- else {
- strcpy(cfile, codefile);
- strcat(codefile, ".c");
- }
- source = fopen(codefile,"r");
- if (source==NULL)
- {
- printf("File %s not found!n", codefile);
- exit(1);
- }
- strcpy(codefile, cfile);
- strcat(codefile, ".lst");
- listing = fopen(codefile,"w");
- if (listing == NULL)
- {
- printf("Unable to open %sn", codefile);
- exit(1);
- }
- /* compilation begins here */
- syntaxTree = parse();
- if (TraceParse) {
- fprintf(listing,"nSyntax tree:n");
- printTree(syntaxTree);
- }
- #if !NO_ANALYZE
- if (!Error)
- {
- fprintf(listing,"nBuilding Symbol Table...n");
- buildSymtab(syntaxTree);
- fprintf(listing,"nChecking Types...n");
- typeCheck(syntaxTree);
- fprintf(listing,"nType Checking Finishedn");
- }
- #if !NO_CODE
- if (!Error)
- {
- strcpy(codefile, cfile);
- strcat(codefile, ".asm");
- inter_code = fopen(codefile,"w");
- if (inter_code == NULL)
- {
- printf("Unable to open %sn", codefile);
- exit(1);
- }
- pTable = GlobalTable;
- StackSegmentBegin();
- StackSegmentEnd();
- CodeSegmentBegin();
- write();
- CodeGen_TreeNode(syntaxTree);
- CodeSegmentEnd();
- fclose(inter_code);
- }
- #endif
- #endif
- fclose(source);
- fclose(listing);
- return 0;
- }