求编译原理-词法分析程序源程序,想了好久了,无法解决

Matthew 2003-12-07 07:46:19

题目如下:
<标识符>::=<字母>|<标识符><字母>|<标识符><字母><数字>
<无符号数>::=<数字>|<无符号数><数字>
<字母>::=A|B|…….|Z|a|b|……… y|z
<数字>::=0|1|…..|9
<关系运算符>::= =|<|>|<=|>=|<>
<算术运算符>::=+|-|*|/|**|(|)
<赋值好>::= :=
<语句隔符>::= ;
空格和回车符可作为单词分隔符
关键字:"Begin","char","else","do","end","if","int","for","procedure",
"real","type","var","while"
阅读源程序(文件file1.txt),识别单词,若有合法单词则生成二元组(种别码,值)
输出(至数组或文件)。若有非法单词则在单词位置处给出错误标记

例:if x2<>y3 then A1:=x*4 else A2:=x**5;
都是 合法单词,生成二个表格:名字表和常数表。输出二元数组如下:
常数表
0 4
1 5
2 :
3 :

名字表
0 X1
1 Y3
2 A1
3 A2
: :

输出代码
0 $if
1 $id
2 $then
3 $else
: :
若有错误发生,则输出源程序下面对应位置上作标记:
If x{2}<>y_3 then a=x*4 ;
^ ^ ^ ^
望解答,谢谢
...全文
72 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gridcomputing 2003-12-07
  • 打赏
  • 举报
回复
up
柒数信息 2003-12-07
  • 打赏
  • 举报
回复
charlist.txt的内容 可以随便加你要的符号
(
)
*
+
-
/
=
wordlist.txt 关键字表 也可以随便写 我写的是
else
end
if
read
repeat
then
until
write
以上都是一个词一行
还有readfile.txt 是被分析文件 自己写吧
柒数信息 2003-12-07
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#define FALSE 0
#define TRUE 1
#define BUFSIZE 2048
#define CHAR 1
#define NUMBER 0

//函数声明
bool casein(char *);
void wordfind(char *);
void makelist(char *);
int switchgroup(char * );
void print(int,char *,int);
void menu();
void menu2();
int startwith(char *);

//全局变量定义
char Word[]="wordlist.txt";
char Char[]="charlist.txt";
char readfile[]="readfile.txt";
char Words[]="wordslist.txt";
柒数信息 2003-12-07
  • 打赏
  • 举报
回复
#include "word.h"

void main(){
menu();
}

///列表生成工具
void makelist(char * text){
char ch[20][20];
int i=0;
int j,k;
while(1){
cin>>ch[i];
if(strcmp(ch[i],"enterend")==0)
break;
i++;
}
char xch[20];
for(k=0;k<i;k++)
{
for(j=0;j<i-1;j++)
{
if(strcmp(ch[j],ch[j+1])>0)
{strcpy(xch,ch[j]);strcpy(ch[j],ch[j+1]);strcpy(ch[j+1],xch);}
}
}
ofstream out(text);
for(k=0;k<i;k++)
{
out<<ch[k]<<endl;
}

}




//单词分离
void wordfind(char * text){
cout<<"请输入要分析的文件名:"<<endl;
cin>>text;
char buf;
int i=0;
int len=0;
char buff[2048];
ifstream fin(text);
//源文件的规则化
while(!fin.eof()){
buf=fin.get();
if(buf=='\n'||buf==';')
buf=' ';
buff[len]=buf;
len++;
}
char * buffer=new char[len];
strncpy(buffer,buff,len);
//单词提取
ofstream out(Words);
for(i=0;i<len-1;i++)
{
if((buffer[i]>='a'&&buffer[i]<='z')||(buffer[i]>='A'&&buffer[i]<='Z')||(buffer[i]>='0'&&buffer[i]<='9'))
{
out<<buffer[i];
}
else
{
if(buffer[i]!=' ')
{
if(buffer[i-1]!=' ')
out<<endl;
out<<buffer[i]<<endl;
}
else
{
if(buffer[i-1]!=' ')
out<<endl;
}

}
}
}



//单词判断
bool casein(char * text,char * words){
char word[10];
int k=0;
ifstream fin(text);
while(!fin.eof()){
fin>>word;
if(k=strcmp(word,words)==0)
return TRUE;
else
if(k>0)
return FALSE;
}
return FALSE;

}

//单词分组
int switchgroup(char * word){
if(casein(Word,word))
return 1;
if(casein(Char,word))
return 2;
if(word[0]==':')
return 3;
else
return 4;
}


//使用集 各参数的使用
void fanal(){
int kind;
int lastkind=0;
char word[10];
ifstream fin(Words);
while(!fin.eof()){
fin>>word;
kind=switchgroup(word);
if(kind==3)
{
fin>>word;
kind=switchgroup(word);
if(word[0]=='=')
{
print(kind,":=",lastkind);
}
}
else
{
print(kind,word,lastkind);
}
lastkind=kind;

}
}


//单词类型分析
int startwith(char * word){
if(word[0]>='0'&&word[0]<='9')
return NUMBER;
else
return CHAR;
}


//显示打印
void print(int k,char * word,int l){
if(k==1)
cout<<"类型是: 关键字 名字是: "<<word<<" 值是:"<<word<<endl;
if(k==2)
cout<<"类型是: 特殊符号 名字是: "<<word<<" 值是:"<<word<<endl;
if(k==4)
if(l==1)
cout<<"类型是: 变量 名字是: "<<word<<" 值是:"<<word<<endl;
if((l==2)&&startwith(word))
cout<<"类型是: 变量 名字是: "<<word<<" 值是:"<<word<<endl;
if((l==2)&&!startwith(word))
cout<<"类型是: 常量 名字是: "<<word<<" 值是:"<<word<<endl;
}








//主菜单
void menu(){
cout<<" *****1: 文件分析 *****"<<endl
<<" *****2: 高级模式 *****"<<endl
<<" *****3: 退出系统 *****"<<endl;
char ch;
cin>>ch;
if(ch=='1')
{ wordfind(readfile);fanal();menu();}
if(ch=='2')
menu2();
if(ch=='3')
exit(0);
}

//高级菜单
void menu2(){
cout<<" *****1: 关键字表生成 *****"<<endl
<<" *****2: 符号集输入 *****"<<endl
<<" *****3: 返回主系统 *****"<<endl
<<" ***** 标记结束符 enterend *****"<<endl;
char ch;
cin>>ch;
if(ch=='1')
{
makelist(Word);
menu2();
}
if(ch=='2')
{
makelist(Char);
menu2();
}
if(ch=='3')
menu();
else
menu2();
}

word.h在下面
Matthew 2003-12-07
  • 打赏
  • 举报
回复
先谢谢了,不过你给我的太复杂了,看不懂,好像我这个没这么繁吧!
smalltalk 2003-12-07
  • 打赏
  • 举报
回复
偶自己的产品的一部分核心技术,现在给你工享,帮人一忙,胜造七级浮屠。

http://yaksa.3322.org/leadbbs/Announce/Announce.asp?BoardID=306&ID=632

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧