?? lrparser.txt
字號(hào):
// 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 << "輸入字符串,以#鍵結(jié)束:" << 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 << "表達(dá)式錯(cuò)誤!" << 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 <<"賦值號(hào)錯(cuò)誤!" << endl;
kk=1;
}
}
else
{
cout << "語(yǔ)句錯(cuò)誤" << endl;
kk=1;
}
return;
}
void lrparser()
{
if(syn==1)
{
scaner();
yucu();
if (syn==6)
{
scaner();
if ((syn==0)&&(kk==0))
cout << "表達(dá)式正確!" << endl;
}
else
{
if(kk!=1)
cout << "語(yǔ)句缺少'end'!" << endl;
kk=1;
}
}
else
{
cout << "語(yǔ)句沒有'begin'!" << endl;
kk=1;
}
return;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -