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

📄 lex.cpp

📁 该程序是一个MiniPascal语言的编译器
💻 CPP
字号:
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include<iostream.h>

#include "global.h"
#include "keyword.h"

//*********************************************************************************************//

//									词法分析部分

//*********************************************************************************************//


void getch();//把下一个输入字符读到ch中,把读入源程序字符的向前指针前移一个字节位置
void getbc();//检查ch中的字符是否为空白、tab或换行。若是,则调用getch()直至ch进入一个非空白也非
			//标记字符为止。
void concatenation();//把ch中的字符连接到token中字符串的后面
void retract();//把读入源程序字符的向前指针回调一个字节位置,并把ch中的字符置为空
int reserve();//对token中的字符串查找保留字表,若是则送回它的编码,否则送回非保留字信息
void error(int,int);
int nexttoken(){//取下一个单词,词法分析部分
	int xh;
	for(xh=0;xh<SIZE;xh++)token[xh]=0;//初始化
	getch();getbc();//得到有效字符
	if(isalpha(ch)){
		while(isalpha(ch)||isdigit(ch)){
			concatenation();getch();
		}
		retract();
		xh=reserve();
        if(xh==0){
		    return(id);
		}
		return(xh);                                                                       
	}
	if(isdigit(ch)){
		while(isdigit(ch)){
			concatenation();getch();
		}
		retract();
		return(number);
	}
	switch(ch){
	case EOF:return EOF;
	case '(':return(lpar);
	case ')':return(rpar);
	case '+':return(plus_op);
	case '-':return(minus_op);
	case '=':return(relop_EQ);
	case '<':getch();
		if(ch=='='){return(relop_LE);}
		else if(ch=='>'){return(relop_NE);}
		retract();return(relop_LT);
	case '>':getch();
		if(ch=='='){return(relop_GE);}
		retract();return(relop_GT);
	case ':':getch();
		if(ch=='='){return(assign_op);}
		retract();return(colon);
	case ';':return(semicolon);
	case '*':return(multi_op);
	case '|':return(or_op);
	case ',':return(comma);
	case '[':return(lbracket);
	case ']':return(rbracket);
	case '.':getch();
		if(ch=='.')return dotdot;
		retract();return dot;
	default:
		error(19,LINE);
		return nexttoken();
//		return -1;
	}//and ".." need to be added!
}
void getch(){
	ch=fgetc(testfile);
	if(ch=='\n')LINE++;
	if(ch=='{'){
		while(ch!='}')
		{
			getch();
			if(ch==EOF)
			{
				error(33,LINE);
				cout<<progname<<".obj- "<<errorcount<<" error(s)"<<endl;
				exit(0);
			}

		}
		getch();
	}
}
void getbc(){
	while(ch==' '||ch=='\n'||ch==9)getch();
//	if(ch==EOF)cout<<"over..."<<endl;
}
void retract(){
	if(ch=='\n')LINE--;
	fseek(testfile,-1L,1);
	ch=' ';
}
void concatenation()
{
	char tmp=tolower(ch);
	strncat(token,&tmp,1);
}
int reserve(){
	int i;
	for(i=0;token[i]!=0;i++);
	if(!strcmp(token,"true"))return True;
	if(!strcmp(token,"false"))return False;
	if(!strcmp(token,"program"))return Program;
    if(!strcmp(token,"procedure"))return Procedure;
	if(!strcmp(token,"const"))return Const;
	if(!strcmp(token,"type")) return Type;
	if(!strcmp(token,"array"))return Array;
	if(!strcmp(token,"of"))   return Of;
	if(!strcmp(token,"record"))return Record;
	if(!strcmp(token,"begin")) return Begin;
	if(!strcmp(token,"end"))   return End;
	if(!strcmp(token,"var"))   return Var;
	if(!strcmp(token,"procedure"))return Procedure;
	if(!strcmp(token,"if"))   return If;
	if(!strcmp(token,"then")) return Then;
	if(!strcmp(token,"else")) return Else;
	if(!strcmp(token,"while"))return While;
	if(!strcmp(token,"do"))   return Do;
//	if(!strcmp(token,"empty"))return Empty;
	if(!strcmp(token,"or")) return Or;
	if(!strcmp(token,"and"))return And;
	if(!strcmp(token,"div"))return Div;
	if(!strcmp(token,"mod"))return Mod;
	if(!strcmp(token,"not"))return Not;
	if(!strcmp(token,"boolean"))return Boolean;
	if(!strcmp(token,"integer"))return Integer;
	if(!strcmp(token,"read"))   return Read;
	if(!strcmp(token,"write"))  return Write;
    return 0;
}

⌨️ 快捷键说明

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