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

📄 lrparser.txt

📁 语法分析,简单的按一规则编写的语法分析,大三实验.
💻 TXT
字号:
// YUFA.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <fstream.h>

char prog[80],token[8],ch;
char *rwtab[6]={"begin","if","then","while","do","end"};
int syn,p,m,n,sum;
int kk;

void scaner();
void lrparser();
void yucu();
void expression();
void term();
void factor();
void statement();

void main()
{
	p=kk=0;
	/*
	ifstream in;
	in.open("a.txt");
	for(;!in.eof();p++)
	{
		in.get(ch);
		prog[p]=ch;
	}
	in.close();
   */
	cout << "输入字符串,以#键结束:" << endl;
	do
	{ 
		ch=getchar();
		prog[p++]=ch;
	}while(ch!='#');
	p=0;
	do
	{
	    scaner();
		switch(syn)
		{
		case 11:
			cout << "(11," << sum << ")" ;
			break;
		case -1:
			cout<<"error";
                break;
		default:
			cout<<"("<<syn<<",";
				for( m = 0; m < 8 && token[m] != '\0'; m++)
					cout << token[m];
				cout<<")";
		}
		cout << endl;
	}while(syn != 0);
	p=0;
	scaner();
	lrparser();
}

void scaner()
{  
	sum=0;
    for(m=0;m<8;m++)
		token[m++]=NULL;
    m=0;
    ch=prog[p++];
    while(ch==' '||ch=='\n')
		ch=prog[p++];
    if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
	{ 
		while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
		{
			token[m++]=ch;
			ch=prog[p++];
		}
		p--;
		syn=10;
		token[m++]='\0';
		for(n=0;n<6;n++)
			if(strcmp(token,rwtab[n])==0)
			{ 
				syn=n+1;
				break;
			}
	}
	else 
		if((ch>='0')&&(ch<='9'))
		{ 
			while((ch>='0')&&(ch<='9'))
			{ 
				sum=sum*10+ch-'0';
				ch=prog[p++];
			}
			p--;
			syn=11;
		}
		else 
			switch(ch)
			{ 
				case '<':m=0;token[m++]=ch;
					ch=prog[p++];
					if(ch=='>')
					{
						syn=21;
						token[m++]=ch;
					}
					else 
						if(ch=='=') 
						{
							syn=22;
							token[m++]=ch;
						}
						else
						{  
							syn=20;
							p--;
						}
						break;
				case '>':m=0;token[m++]=ch;
					ch=prog[p++];
					if(ch=='=')
					{
						syn=24;
						token[m++]=ch;
					}
					else
					{ 
						syn=23;
						p--;
					}
					break;
				case ':':m=0;token[m++]=ch;
					ch=prog[p++];
					if(ch=='=')
					{
						syn=18;
						token[m++]=ch;
					}
					else
					{ 
						syn=17;
						p--;
					}
					break;
				case '+': syn=13;token[0]=ch;break;
				case '-': syn=14;token[0]=ch;break;
				case '*': syn=15;token[0]=ch;break;
				case '/': syn=16;token[0]=ch;break;
				case '(': syn=27;token[0]=ch;break;
				case ')': syn=28;token[0]=ch;break;
				case '=': syn=25;token[0]=ch;break;
				case ';': syn=26;token[0]=ch;break;
				case '#': syn=0;token[0]=ch;break;
				default: syn=-1;break;
		}
}

void factor()
{ 
	if((syn==10)||(syn==11)) 
		scaner();
  
	else 
		if(syn==27)
		{ 
			scaner();
			expression();
			if(syn==28) 
				scaner();
			else 
			{ 
				cout << "缺少)!" << endl;
				kk=1;
			}
		}
		else
		{ 
			cout << "表达式错误!" << endl;
			kk=1;
		}
		return;
}

void term()
{ 
	factor();
	while((syn==15)||(syn==16))
    { 
		scaner();
		factor();
    }
	return;
}

void expression()
{ 
	term();
	while((syn==13)||(syn==14))
    { 
		scaner();
		term();
    }
	return;
}

void yucu()
{ 
	statement();
	while(syn==26)
	{ 
		scaner();
		if(syn!=6) 
			statement();
	}
	return;
}

void statement()
{ 
	if(syn==10)
	{ 
		scaner();
		if(syn==18)
		{ 
			scaner();
			expression();
		}
		else 
		{
			cout <<"赋值号错误!" << endl;
			kk=1;
		}
	}
	else 
	{ 
		cout << "语句错误" << endl;
		kk=1;
	}
	return;
}

void lrparser()
{
	if(syn==1)
	{ 
		scaner();
		yucu();
		if (syn==6)
		{ 
			scaner();
			if ((syn==0)&&(kk==0)) 
				cout << "表达式正确!" << endl;
		}
		else 
		{ 
			if(kk!=1) 
				cout << "语句缺少'end'!" << endl;
			kk=1;
		}
	}
	else 
	{ 
		cout << "语句没有'begin'!" << endl;
		kk=1;
	}
	return;
}

⌨️ 快捷键说明

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