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

📄 myprogram.cpp

📁 java 词法分析器 c语言实现. java 词法分析器 c语言实现.
💻 CPP
字号:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <fstream>
#include "const.h"

using namespace std;

char ch;             //当前扫描到的字符
int type;            //单词的类型
ifstream inFile;     //输入文件
ofstream outFile;    //输出文件
char rbuf[RBUFSIZE]; //读文件缓冲区
int rp;              //读文件缓冲区指针
char sbuf[SBUFSIZE]; //单词字符串缓冲区
int sp;              //单词字符串缓冲区指针
//***********************从读文件缓冲区得到下一个字符**********************
void get_ch()
{
    ch=rbuf[rp];
	rp++;
}
//***********************向字符缓冲区追加一个字符**************************
void put_ch(char ch)
{
    sbuf[sp]=ch;
	sp++;
}
//*****************************清空读文件缓冲区****************************
void clear_rbuf()
{
	int i;
	for(i=0;i<RBUFSIZE;i++)
		rbuf[i]='\0';
	rp=0;
}
//******************************清空单词字符缓冲区*************************
void clear_sbuf()
{
	int i;
	for(i=0;i<SBUFSIZE;i++)
		sbuf[i]='\0';
	sp=0;
}
//**************************得到单词类型*********************************
void get_type(char * msg)
{ 
	int i;
    for(i=0;i<TABLE_LENGTH;i++)
    {
        if (!strcmp(msg, ATTR_MAP[i].keyword))
        {
			type=ATTR_MAP[i].type;
            return;
        }
    }
    return;
}
//******************判断字符是否属于base进制并转换**********************
int digit(int base)
{
    char c=ch;
    int result;
    if (c>='0'&&c<='7'){
        result = (int)(c - '0');
    }
    else if(c>='8'&&c<='9'){
        if (base > 8){
            result=(int)(c-'0');
        }
        else{
            result = -1;
        }
    }
    else if(c>='a'&&c<= 'f'){
        if (base>10){
            result=(int)(c-'a'+10);
        }
        else{
            result=-1;
        }
    }
    else if (c>='A'&&c<='F'){
        if (base>10){
            result=(int)(c-'A'+10);
        }
        else{
            result=-1;
        }
    }
    else{
        result=-1;
    }
    return result;
}
//******************************扫描指数********************************
void scan_fraction()
{
    while(digit(10)>=0){
        put_ch(ch);
        get_ch();
    }
    if(ch=='e'||ch=='E'){
        put_ch(ch);
        get_ch();
        if(ch=='+'||ch=='-'){
            put_ch(ch);
            get_ch();
        }
		while(digit(10)>=0)
		{
			put_ch(ch);
			get_ch();
		}
		return;
	}
	return;
}
//**************************扫描浮点数后缀*******************************
void scan_suffix() {
    scan_fraction();
    if(ch=='f'||ch=='F'||ch=='d'||ch=='D'){
		put_ch(ch);
	    get_ch();
	}
	type=T_FLOAT;
	return;
}
//**************************判断字符是否是特殊字符**********************
bool is_spectial(char &ch)
{
	if(ch=='!'||ch=='%'||ch=='&'||ch=='*'||ch=='?'||ch=='+'||ch=='-'||
		ch==':'||ch=='<'||ch=='='||ch=='>'||ch=='^'||ch=='|'||ch=='~'){
		return true;
	   }
	else{
		return false;
	}
}
//***************************扫描运算符*********************************
void scan_operator()
{
    while (is_spectial(ch))
    {
        put_ch(ch);
        get_ch();
    }
	get_type(sbuf);
	if(type==0){
		type=T_ERROR;
	}
	return;
}
//***********************扫描8、10、16进制数值****************************
void scan_number(int radix)
{
    while(digit(radix)>=0){
        put_ch(ch);
        get_ch();
    }
    if(radix!=10&&ch=='.'){
        put_ch(ch);
        get_ch();
        type=T_ERROR;
    }
	else if(radix==10&&ch=='.'){
		put_ch('.');
		get_ch();
		if(digit(10)>=0)
		{
			scan_suffix();
		}
	}
	else if(radix==10&&(ch=='e'||ch=='E'||ch=='f'||ch=='F'||ch=='d'||ch=='D')){
		scan_suffix();
	}
	else if(ch == 'l' || ch == 'L'){
		put_ch(ch);
		get_ch();
        type=T_INT;
	}
	else type=T_INT;
	return;
}
////**************************跳过注释内容*********************************
void skip_comment()
{
    while(ch!='\0'){
        switch(ch){
        case '*':
            get_ch();
            if (ch=='/'){
				get_ch();
				return;
			}
            break;
        default:
            get_ch();
            break;
        }
    }
}
//*********************判断字符是否标识符首字符*************************
bool is_idchar(char &ch){
	return
		((ch>='0'&&ch<='9')||
	    (ch>='A'&&ch<='Z')||
	    (ch>='a'&&ch<='z')||
	    ch=='$'||ch=='_');
}
//***********************搜索关键字、标识符******************************
void scan_ident()
{
	bool id_or_key = true;
	bool tem=true;//是否仍是标识符或关键字
    while(ch!=C_TAB&&ch!=C_FF&&ch!=C_CR&&ch!=C_LF&&ch!='\0'){
		if(is_idchar(ch)){
			put_ch(ch);
			get_ch();
			if(is_idchar(ch))
				continue;
			else{
				get_type(sbuf);
			}
			if(type!=0)
				return;
			else{
				type=T_IDENTIFIER;
				return;
			}
		}
    }
}
//***********************转义字符搜索字符*******************************
void scan_char(){
    int oct = 0;
    int hex = 0;
	if(ch=='\\'){
		get_ch();
		if(ch=='\\'){put_ch('\\');get_ch();}
		if(ch=='\''){put_ch('\'');get_ch();}
		if(ch=='\"'){put_ch('\"');get_ch();}
		if(ch=='b') {put_ch('\b');get_ch();}
		if(ch=='t') {put_ch('\t');get_ch();}
		if(ch=='n') {put_ch('\n');get_ch();}
		if(ch=='f') {put_ch('\f');get_ch();}
		if(ch=='r') {put_ch('\r');get_ch();}
		if('0'<=ch&&ch<='7'){
			oct=digit(8);
			get_ch();
			if('0'<=ch&&ch<='7'){
				oct=oct*8+digit(8);
				get_ch();
				if('0'<=ch&&ch<='7'){
					oct=oct*8+digit(8);
					get_ch();
				}
			}
			put_ch((char)oct);
		}
		if(ch=='u'){
			get_ch();
			if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='f')||('A'<=ch&&ch<='F')){
				hex=hex*16+digit(16);
				get_ch();
				if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='f')||('A'<=ch&&ch<='F')){
					hex=hex*16+digit(16);
					get_ch();
					if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='f')||('A'<=ch&&ch<='F')){
						hex=hex*16+digit(16);
						get_ch();
						if(('0'<=ch&&ch<='9')||('a'<=ch&&ch<='f')||('A'<=ch&&ch<='F')){
							hex=hex*16+digit(16);
							get_ch();
						}
					}
				}
			}
			put_ch((char)hex);
		}
	}
	else{
		put_ch(ch);
		get_ch();
	}
}
//*********************获取下一个单词及属性*****************************
void get_word()
{
    clear_sbuf();
	type=0;
    while (ch!='\0')
    {
//***********************关键字、标识符*********************************
		if((ch>='A'&&ch<='Z')||(ch>='a'&&ch<='z')||ch=='$'||ch=='_'){
            scan_ident();
            return;
		}
//***************************字符***************************************
		else if(ch=='\''){
			get_ch();
			if(ch=='\''){
				type=T_ERROR;
				strcpy(sbuf,"''");
				get_ch();
			}
			else{
				scan_char();
				if(ch=='\''){
					type=T_CHAR;
					get_ch();
				}
				else type=T_ERROR;
			}
			return;
		}	
//**************************字符串**************************************
		else if(ch=='\"'){
			get_ch();
			if(ch=='\"'){
				type=T_ERROR;
				strcpy(sbuf,"\"\"");
				get_ch();
			}
			else{
				do{
					scan_char();
				}while(ch!='\"'&&ch!=C_TAB&&ch!=C_FF&&ch!=C_CR&&ch!=C_LF);
				if(ch=='\"'){
					type=T_STRING;
					get_ch();
				}
				else type=T_ERROR;
			}
			return;
		}
//************************.开头数字*************************************
		else if(ch=='.'){
			put_ch(ch);
		    get_ch();
            if(digit(10)>=0){
                scan_suffix();
            }
            else type=T_BOUND;
            return;
		}
//***************************0开头数字**********************************
        else if(ch=='0'){
			put_ch('0');
            get_ch();
            if(ch=='x'||ch=='X'){
				put_ch(ch);
                get_ch();
                if(digit(16)>=0&&ch!='0'){
					scan_number(16);
                }
            }
			else if(digit(8)>=0&&ch!='0'){
				scan_number(8);
			}
            else if(ch=='.'){
				put_ch('.');
				get_ch();
                if(digit(10)>=0){
                    scan_suffix();
				}
            }
			else if(ch==' '){
				get_ch();
				type=T_INT;
			}
			else type=T_ERROR;
			return;
		}
//***********************1-9开头数字***********************************
		else if('1'<=ch&&ch<='9'){
            scan_number(10);
            return;
		}
//***********************9个界限符中的8个************************
		else if((ch=='(')||(ch==')')||(ch=='[')||(ch==']')){
			put_ch(ch);
            get_ch();
            type = T_BOUND;
            return;
		}
		else if(ch==','){
			put_ch(ch);
            get_ch();
            type = T_COMMA;
            return;
		}
		else if((ch=='{')||(ch=='}')){
			put_ch(ch);
            get_ch();
            type = T_BRACKET;
            return;
		}
		else if(ch==';'){
			put_ch(ch);
            get_ch();
            type = T_SEMICOLON;
            return;
		}
//**********************注释、'/'运算符、 '/='运算符**************
        else if(ch=='/'){
            get_ch();
            if(ch=='/'){
				while(ch!=C_CR&&ch!=C_LF&&ch!='\0'){
                    get_ch();
                }
                break;
            }
            else if(ch=='*'){
                get_ch();
                skip_comment();
            }
            else if(ch=='='){
                strcpy(sbuf, "/=");
                type=T_ASSIGN;
                get_ch();
            }
            else{
                strcpy(sbuf, "/");
                type=T_MULDIV;
            }
            return;
		}
//***********************特殊字符**********************************
		else if(is_spectial(ch))
		{
			scan_operator();
			return;
		}
//*************************间隔符************************************
		else get_ch();
	}
}
//***********************将源文件读入缓冲区**************************
void readfile(char * fn_in)
{
	rp = 0;
	inFile.open(fn_in);
    if (!inFile.is_open())
    {
        return;
    }
    while(inFile.get(rbuf[rp]))
    {
        rp++;
    }
    inFile.close();
	rp = 0;
}
//*************************向输出文件写字符**************************
void writefile()
{
	sp = 0;
    outFile << "(0x" << hex << type << ") ";
    outFile << "[";
	while(sbuf[sp]!='\0'){
		outFile << sbuf[sp];
		sp++;
	}
    outFile << "]";
    outFile << endl;
	sp = 0;
}
//*************************main函数*********************************
int main(int argc, char * argv[])
{
    char fn_in[NAMESIZE];   //输入文件名
    char fn_out[NAMESIZE];  //输出文件名
	cout << "Input source file name: ";
	cin >> fn_in;
	readfile(fn_in);
	cout << "source file name: " << fn_in << endl;
	cout << "Input dest file name: ";
	cin >> fn_out;
	cout << "dest file name: "  << fn_out<< endl;
	outFile.open(fn_out);
	get_ch();
	while(ch!='\0'){
		get_word();
		if(strlen(sbuf)!=0){
			writefile();
		}
	}
	outFile.close();
 	return 0;
}

⌨️ 快捷键说明

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