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

📄 global.cpp

📁 编译原理实验
💻 CPP
字号:
/**** global.h ******************************/

#include <stdio.h>
#include <stdlib.h>
#include "string.h"

#include <ctype.h>
#define BSIZE 128
#define NONE -1
#define EOS '\0'

#define NUM 256 
#define DIV 257
#define MOD 258
#define ID 259
#define DONE 260

int tokenval = NONE;
int lineno=0;
struct entry { /* from of symbol table entry */
	char *lexptr;
	int token;
};

struct entry symtable[];



/************* error.c **********************************************/

//#include "global.h"
void error(char *m) 
//char *m;
{
	fprintf(stderr,"line %d:%s\n",lineno,m);
	exit(1);
}
/******* parser.c ***********************************************************/

//#include "global.h"
int lexan();
void expr();
void match(int t);
void term();
void emit(int t,int tval);
void factor();
int lookup(char s[]);
int insert(char s[],int tok);
int lookahead;



//int lookahead;
void parse()
{
	lookahead=lexan();
	while (lookahead !=DONE)
	{
		expr();match(';');
	}
}
void expr()
{
	int t ;
	term();
	while(1)
		switch (lookahead){
case '+':
case '-':
			t=lookahead;
			match(lookahead);term()
				;emit(t,NONE);
			continue;
default:
	return;
	}
}
void term()
{
	int t;
	factor();
	while(1)
		switch (lookahead){
case '*':
case '/':
case DIV:
case MOD:
			t=lookahead;
			match(lookahead);
			factor();
			emit(t,NONE);
			//continue;
default:
	return;
	}
}
void factor()
{
	switch (lookahead){
	case'(':
			match('(');expr();match(')');break;
	case NUM:
			emit(NUM,tokenval);match(NUM);break;
	case ID:
			emit(ID,tokenval);match(ID);break;
	default:
		error("syntax error");
	}
}


void match(int t)
      
{
	if (lookahead==t)
		lookahead=lexan();
	else error("sytax error");
}

/**** lexer.c *****************************************************/

//#include "global.h"
char lexbuf [BSIZE];
//int lineno =1;
//int tokenval = NONE;

int lexan()
{ 
	int t;
    while(1){
		t=getchar();
		if(t==' '|| t=='\t')
			;
		else if(t=='\n')
			lineno=lineno+1;
		else if (isdigit(t))
		{
			ungetc(t,stdin);
			scanf("%d",&tokenval);
			return NUM;
		}
		else if (isalpha(t))
		{
			int p,b=0;
			while (isalnum(t)){
				lexbuf[b]=t;
				t=getchar();
				b=b+1;
				if (b>=BSIZE)
					error("compiler error");
			}
			lexbuf[b]= EOS;
			if (t!= EOF)
				ungetc(t,stdin);
			p=lookup(lexbuf);
			if (p==0)
				p=insert(lexbuf,ID);
			tokenval=p;
			return symtable[p].token;
		}
		else if (t==EOF)
			return DONE;
		else {
			tokenval= NONE;
			return t;
		}
	}
}



				
/***** emitter.c*************************************************************/

//#include "global.h"
//int t,tval;
void emit(int t,int tval)
         
{
	switch(t){
	   case'+':case '-':case'*':case'/':
			printf("%c\n",t);break;
	   case DIV:
		    printf("DIV\n");break;
       case MOD:
		    printf("MOD\n");break;
       case NUM:
		    printf("%d\n",tval);break;
       case ID:
		   printf("%s\n",symtable[tval].lexptr);break;
	   default:
		   printf("token %d,tokenval %d\n",t,tval);
	}
}
/******** symbol.c**************************************************/

//#include "global.h"
#define STRMAX 999
#define SYMMAX 100
char lexemes[STRMAX];
int lastchar = -1;
struct entry symtable[SYMMAX];
int lastentry=0;

int lookup(char s[])
    
{
	int p;
	for(p=lastentry;p>0;p=p-1)
		if (strcmp(symtable[p].lexptr,s)==0)
			return p;
		return 0;
}
int insert(char s[],int tok)

{
	int len;
	len= strlen(s);
	if(lastentry + 1>=SYMMAX)
		error("lexemes array full");
	lastentry = lastentry +1;
	symtable[lastentry].token=tok;
	symtable[lastentry].lexptr=&lexemes[lastchar + 1];
    lastchar = lastchar + len + 1;
	strcpy(symtable[lastentry].lexptr,s);
	return lastentry;
}

/****************************init.c******************************************/

//#include "global.h"

struct entry keywords[] ={
	"div",DIV,
    "mod",MOD,
	0,0
};
void init()
{
	struct entry *p;
	for(p=keywords;p->token;p++)
	{insert(p->lexptr,p->token);
	}
}

/**************** main.c************************************************/

//#include "global.h"
void main()
{
	init();

   parse();
	exit(0);
}
/************************************************************************/

















































































































































⌨️ 快捷键说明

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