⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 1.小型编译器 2。支持整数
💻 C
字号:
/****************************************************/
/* File: main.c                                     */
/* Main program for TINY compiler                   */
/* Compiler Construction: Principles and Practice   */
/* Kenneth C. Louden                                */
/****************************************************/

#include "Globals.h"
#include "SymTab.h"

/* set NO_PARSE to TRUE to get a scanner-only compiler */
#define NO_PARSE FALSE
/* set NO_ANALYZE to TRUE to get a parser-only compiler */
#define NO_ANALYZE FALSE

/* set NO_CODE to TRUE to get a compiler that does not
 * generate code
 */
#define NO_CODE FALSE

#include "Util.h"
#if NO_PARSE
#include "Scan.h"
#else
#include "Parse.h"
#if !NO_ANALYZE
#include "Analyze.h"
#if !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 = FALSE;
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[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);

	//char	c;
	//int	i;
	//double d;
	//printf("size:%d %d %d\n",sizeof(c),sizeof(i),sizeof(d));
	
#if NO_PARSE
  	while (getToken()!=ENDFILE);
#else
	extern FILE *yyin;
  	yyin = fopen(pgm,"r");  
	//printf("parse begin\n");
  	syntaxTree = parse();
  	if (TraceParse) {
		fprintf(listing,"\nSyntax tree:\n");
    	printTree(syntaxTree);
  	}
	//printf("parse end\n");

#if !NO_ANALYZE
  	if (! Error)
  	{ 
  		if (TraceAnalyze) 
			fprintf(listing,"\nBuilding Symbol Table...\n");
    	buildSymtab(syntaxTree);
		//printTree(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);
    	}
		pTable = GlobalTable;
    	codeGen(syntaxTree,codefile);
    	fclose(code);

		doRun();
  	}
#endif
#endif
#endif
  	fclose(source);
  	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -