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

📄 简单程序设计语言的词法分析器.txt

📁 将源程序预处理、状态图转换等结合
💻 TXT
字号:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>

const short WORDLEN=100;//字符长度
struct code_val//结构体
{
	char code;
    char val[WORDLEN];
};
void pro_process(char *buf)
	{
		ifstream cinf(".\\text.c",ios::in);
	    int i=0;    /*计数器*/
	    int j=0;
	    int count=0;
	    char old_c='\0',cur_c;       /*前一个字符,当前字符*/
	    bool in_comment=false;      /*false表示当前字符未处于注释中*/
	    while(cinf.read(&cur_c,sizeof(char)))    /*从文件读一个字符*/
		{
		    if(in_comment==false)
			{
				if(old_c=='/'&&cur_c=='*')       /*进入注释*/
				   {
					   i--;              /*去除已存入扫描缓冲区的字符*/
				       in_comment=true;
				   }
				   else
				   {
					   if(old_c=='\\'&&cur_c=='\n')    /*发现续行*/  
					   i--;              /*去除已存入扫描缓冲区的字符*/
					   else
					   {
						   if(cur_c>='A'&&cur_c<='Z')   /*大写变小写*/
						   cur_c+=32;
					      if(cur_c>='\t'&&cur_c<='\n')     /*空格取代tab换行*/
						   cur_c+=' ';
						   buf[i++]=cur_c;
						   count++;
					   }
				   }
			}
			else
			{
				if(old_c=='*'&&cur_c=='/')      /*离开注释*/
				    in_comment=false;
			}
		  old_c=cur_c;                      /*保留前一个字符*/
		}
		buf[i++]='#';                          /*在源程序词尾加字符#*/
		for(j=0;j<count;j++)
		{
			printf("%c",buf[j]);
		}
	}
void Concat(char token[],char c)//连接字符函数
{
	for(int i=0;token[i];i++);
    token[i]=c;
    token[++i]='\0';
}

char reserve(char token[])//查基本字表函数
{
	const char *table[]={"begin","end","integer","real"};
    const char code[]={"{}ac"};
    for(int i=0;i<(int)strlen(code);i++)
    if(strcmp(token,table[i])==0) return code[i];
    return 'i';
}

struct code_val scanner(char *buf)//扫描函数
{
	static int i=0;
    struct code_val t={'\0',"NUL"};
    char token[WORDLEN]="";
    while(buf[i]==' ')		i++;//去除前导空格
    if(buf[i]>='a' && buf[i]<='z')	//识别单词、标识符或基本字
    {
		while(buf[i]>='a' && buf[i]<='z'||buf[i]>='0' && buf[i]<='9')
	    Concat(token,buf[i++]);
        t.code=reserve(token);
        if(t.code=='i')		strcpy(t.val,token);
        return t;
    }
    if(buf[i]>='0' && buf[i]<='9')	//判断是整(实)常数
    {
		while(buf[i]>='0' && buf[i]<='9')
			Concat(token,buf[i++]);
        if(buf[i]=='.')
        {
			Concat(token,buf[i++]);
	        while(buf[i]>='0' && buf[i]<='9')//123.4
				Concat(token,buf[i++]);
            t.code='y';
		}
        else
			t.code='x';
        strcpy(t.val,token);
        return t;
    }
    if(buf[i]=='.')//实常数
    {
		Concat(token,buf[i++]);
        if(buf[i]>='0' && buf[i]<='9')
        {
			while(buf[i]>='0' && buf[i]<='9')
	        Concat(token,buf[i++]);
	        t.code='y';
	        strcpy(t.val,token);
	        return t;
		}
        else
		{		//单个.错误词形
			cout<<"Error word>"<<token<<endl;
			exit(0);
		}
     }
	 switch(buf[i])		//其余单词
     {
		case ',':
			t.code=',';
			break;
		case ';':
			t.code=';';
			break;
		case '(':
			t.code='(';
			break;
		case ')':
			t.code=')';
			break;
	    case '=':
			t.code='=';
			break;
		case '+':
			if(buf[++i]=='+')	
				t.code='$';
			else
			{
				t.code='+';
				i--;
		    }
			break;
		case '*':
			t.code='*';
			break;
		case '#':
			t.code='#';
			break;
		default:		//错误字符
			cout<<"Error char>"<<buf[i]<<endl;
			exit(0);
     }
	 i++;
	 return t;//返回当前单词的二元式
}
void main()     		
{
	char buf[4048]={'\0'};
	pro_process(buf);
	cout<<buf<<endl;
	ofstream coutf("Lex_r.txt",ios::out);
	code_val t;
	do
	{
		t=scanner(buf);
		coutf<<t.code<<'\t'<<t.val<<endl;
	}while(t.code!='#');
	cout<<"end of lexical analysis"<<endl;
}

⌨️ 快捷键说明

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