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

📄 passone.c

📁 System Programming 里面关于一个编译器的第一部分设计
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>	
#include <ctype.h>
#include <math.h>

#define SYMMAX	100

int locctr;	
int progleng;	
struct oneline {	
	char loc[10];	
	char label[10];
	char opcode[10];
	char operand[10];
};
struct operators {
	char name[10];
	char code[9];
};

struct operators instruction[26] = 
{
	{"ADD", "18"},  {"AND", "40"}, {"COMP", "28"}, {"DIV", "24"},
	{"J", "3C"},    {"JEQ", "30"}, {"JGT", "34"},  {"JLT", "38"},
	{"JSUB", "48"}, {"LDA", "00"}, {"LDCH", "50"}, {"LDL", "08"},
	{"LDX", "04"},  {"MUL", "20"}, {"OR", "44"},   {"RD", "D8"},
	{"RSUB", "4C"}, {"STA", "0C"}, {"STCH", "54"}, {"STL", "14"}, 
	{"STSW", "E8"}, {"STX", "10"}, {"SUB", "1C"},  {"TD", "E0"},
	{"TIX", "2C"},  {"WD", "DC"}
};

struct entry 
{
	char name[10];
	int value;	
	int errorflag;	
};
struct entry symtable[SYMMAX];	

int lastentry = 0;

void passone();
struct oneline readline(FILE *src, int pass);
int xtoi(char *hex);
int lookup(char *s);
void insert(char *s, int locctr);
int srchop(char *s);

void main()
{
	passone();
}

void passone()
{
	int startaddress;
	FILE *src, *middle, *symbol;
	struct oneline lbuf;
	int found = 0;	
	int i, cnt = 0; 
					
	if ((src = fopen("source.txt", "r")) == NULL)
		printf("The file \"source.txt\" is not exist\n");
	else printf("The file \"source.txt\" was opened\n");

	if ((middle = fopen("intermediate.txt", "wt")) == NULL)
		printf("The file \"intermediate.txt\" was not create\n");
	else printf("The file \"intermediate.txt\" was created\n");
	
	
	lbuf = readline(src, 1);			
	if (!strcmp(lbuf.opcode, "START"))	
	{
		startaddress = xtoi(lbuf.operand);	
		locctr = startaddress;				
		lbuf = readline(src, 1);		
	}
	else locctr = 0;

	while(strcmp(lbuf.opcode, "END"))	
	{
		fprintf(middle, "%3x\t%s\t%s\t%s\n", locctr, lbuf.label, lbuf.opcode, lbuf.operand); //write line to intermediate file 
		if (lbuf.label[0] != '\0')	
		{
			
			if (found = lookup(lbuf.label))		
				symtable[found].errorflag = 1;
			else insert(lbuf.label, locctr);
		}
		found = srchop(lbuf.opcode);
		if (found)
			locctr += 3;			
		else if (!strcmp(lbuf.opcode, "WORD"))
			locctr += 3;
		else if (!strcmp(lbuf.opcode, "RESW"))
			locctr = locctr + (3 * atoi(lbuf.operand));
		else if (!strcmp(lbuf.opcode, "RESB"))
			locctr = locctr + atoi(lbuf.operand);
		else if (!strcmp(lbuf.opcode, "BYTE")) {
			if (lbuf.operand[0] == 'C') {
				for (i = 2; lbuf.operand[i] != '\''; i++)
					cnt++;
				locctr += cnt;			
				cnt = 0;
			}
			if (lbuf.operand[0] == 'X') {	
				for (i = 2; lbuf.operand[i] != '\''; i++)
					cnt++;
				locctr += cnt / 2;		
				cnt = 0;
			}
		}
		else fprintf(middle, "%3d\tset error flag (invalid operation code)\n", locctr); 
		lbuf = readline(src, 1);
	
	}
	progleng = locctr - startaddress;
	fprintf(middle, "%3x\t%s\t%s\t%s\n", locctr, lbuf.label, lbuf.opcode, lbuf.operand);

	symbol = fopen("symtab.txt", "wt");
	for (i = 0; i < lastentry; i++)
		fprintf(symbol, "%3s\t%x\t%d\n", symtable[i].name, symtable[i].value, symtable[i].errorflag);
	fcloseall();
}

struct oneline readline(FILE *src, int pass)
{
	struct oneline linebuf;		
	char t;						
	int i, j;					
	int bp, cnt = 0, check = 0;	
	char lexbuf[3][10];				


	for (i = 0; i < 3; i++)
		for (j = 0; j < 10; j++)
			lexbuf[i][j] = '\0';

	t = getc(src);
	while(1) 
	{
		if (t == ' ' || t == '\t') t = getc(src);
		else if (t == '.') 
		{	
			while (t != '\n')
				t = getc(src);
			t = getc(src);		
		}
		else if (((cnt != 0) && (t == '\n')) || (t == EOF))
		{
			if (cnt == 1) 
			{
				strcpy(linebuf.opcode, lexbuf[0]);	
				strcpy(linebuf.label, lexbuf[1]);	
				strcpy(linebuf.operand, lexbuf[2]);	
			}
			if (cnt == 2) 
			{
				strcpy(linebuf.opcode, lexbuf[0]);	
				strcpy(linebuf.operand, lexbuf[1]);		
				strcpy(linebuf.label, lexbuf[2]);		
			}
			if (cnt == 3) 
			{
				strcpy(linebuf.label, lexbuf[0]);		
				strcpy(linebuf.opcode, lexbuf[1]);	
				strcpy(linebuf.operand, lexbuf[2]);		
			}			
			return linebuf;
		}
		else 
		{
			bp = 0;

			if (pass == 2 && check == 0) 
			{
				i = 0;
				while (isalnum(t)) 
				{
					linebuf.loc[i] = t;
					t = getc(src);
					i++;
				}
				linebuf.loc[i] = '\0';
				check = 1;
				t = getc(src);
			}
			else 
			{
				while (isalnum(t) || t == '\'' || t == ',') 
				{	
					lexbuf[cnt][bp] = t;
					t = getc(src);
					bp++;
				}
				cnt++;
				continue;	
			}
		}
	}
}

int xtoi(char *hex)		
{
	 int i, j, cnt;
	 double sum = 0;
	 int num[10], integer = 0;

	for (cnt = 0; *(hex+cnt) != '\0'; cnt++);

	for (i = 0; i < cnt ; i++)
	{
		if (*(hex+i) == 'A' || *(hex+i) == 'a')	{
			sum = 10 * pow(16, cnt-i-1);
		}
		else if (*(hex+i) == 'B' || *(hex+i) == 'b') {
			sum = 11 * pow(16, cnt-i-1);
		}			
		else if (*(hex+i) == 'C' || *(hex+i) == 'c') {
			sum = 12 * pow(16, cnt-i-1);
		}
		else if (*(hex+i) == 'D' || *(hex+i) == 'd') {
			sum = 13 * pow(16, cnt-i-1);
		}
		else if (*(hex+i) == 'E' || *(hex+i) == 'e') {
			sum = 14 * pow(16, cnt-i-1);
		}
		else if (*(hex+i) == 'F' || *(hex+i) == 'f') {
			sum = 15 * pow(16, cnt-i-1);
		}
		else if (*(hex+i) >= '0' && *(hex+i) <= '9') {
			sum = (*(hex+i) - 48) * pow(16, cnt-i-1);
		}
		else perror("Error: Is not Number");

		num[i] = (int)sum;
	}

	for (j = 0; j < 4; j++)
		integer += num[j];
	return integer;
}

int lookup(char *s)
{
	int p;
	
	for (p = 0; p < lastentry; p++)	
		if (strcmp(symtable[p].name, s) == 0)
			return p;
		return 0;		
}

void insert(char *s, int locctr)
{
	if (lastentry >= SYMMAX)
		perror("Symbol table full");
	
	strcpy(symtable[lastentry].name, s);	
	symtable[lastentry].value = locctr;		
	lastentry += 1;
}

int srchop(char *s)	
{
	int p;
	
	for (p = 0; p < 26; p++)
		if (strcmp(instruction[p].name, s) == 0)
			return (p + 1);
		return 0;			
}

⌨️ 快捷键说明

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