大三的注意了,谁要词法分析程序源代码!

a_cer 2002-06-19 03:35:19
其实我也只是想抛砖引玉,谁有语法分析程序,最好是LR1文法?
嘿嘿......
对了,需要词法分析程序的,留下E-MAIL,留者必复!!
绝对是自己写的哦...
...全文
1132 120 打赏 收藏 转发到动态 举报
写回复
用AI写文章
120 条回复
切换为时间正序
请发表友善的回复…
发表回复
PHshepeihao2007 2010-06-12
  • 打赏
  • 举报
回复
大哥,给一个,实验不会做啊
414871643@qq.com
thx
a_cer 2002-07-04
  • 打赏
  • 举报
回复
大家在这拷吧,我没想到大家这么热情,谢谢.....#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<dos.h>
char * ident[]={"break","case","char","class","const","continue",
"default","delete","do","double","else","enum",
"extern","float","for","friend","goto","if","int",
"long","new","operator","private","protected",
"public","return","short","sizeof","static",
"struct","switch","typedef","void","while","\0"};
char dpt[]="(){}[]\"',;:\0";
int dptNum=11;
char opnd[]="+-*/&|%!<>=^\0";
int opndNum=12;
int identNum=34;
int maxn=50;

int inopnd(char ch){
int i=0;
for(int j=0;j<opndNum;j++){
i=int(ch)-int(opnd[j]); /////////////////////
if(i==0) return 1;
}
return 0;
}

int indpt(char ch){
//return 0;
int i=0;
for(int j=0;j<dptNum;j++){
i=int(ch)-int(dpt[j]); //////////////////
if(i==0) return 1;
}
return 0;
}

int judge(char * strng){
// return 2;/////////////
int i=0;
if((strng[0]<='9')&&(strng[0]>='0')) return 3;
if(indpt(strng[0])) return 4;
if(inopnd(strng[0])) return 5;
// if((strng[0]==';')||(strng[0]==',')||(strng[0]=='(')||(strng[0]==')')) return 4;
// if((strng[0]=='=')||(strng[0]=='+')||(strng[0]=='-')||(strng[0]=='*')||(strng[0]=='/')||(strng[0]=='^')||(strng[0]=='>')||(strng[0]=='<')) return 5;
for (int j=0;j<identNum;j++){
i=strcmp(strng,ident[j]);
if(i==0) return 1;
}
return 2;
}

int main(){
clrscr();
//interrt
//textcolor(WHITE);
// textbackground(BLUE);
textcolor(GREEN);
cprintf("(R) Word Grammar Translator <WGT 0.9>");
cout<<endl;
cprintf("(C) copyright wanjia 2002");
cout<<endl;
textcolor(6);
char ** str1=new char*[maxn];
for(int ii=0;ii<maxn;ii++){str1[ii]=new char[maxn];
for(int mm=0;mm<maxn;mm++)str1[ii][mm]='\0';
}

//char * str;
char ch;
//-----------
char * errstr=new char[maxn];
//errstr='\0';
int e=0;
FILE *fsc;
char tmp;
if ((fsc = fopen("d:\\source.wj", "w"))== NULL){
fprintf(stderr, "Cannot creat file.\n");
return 1;
}
cout<<"You can input file under my announce:<end with ";
textcolor(RED);
cprintf("."); textcolor(WHITE);
cout<<" >\n";
cout<<"\>";
cin.get(ch);fputc(ch,fsc);
if((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')) str1[0][0]=ch;
else { cout<<"The source file must start with Identical string!(as \"int\" \"char\" and so on)";
return 0;}
//cout<<ch;
int i=1,j=0,k;
while(ch!='.'){
cin.get(ch);fputc(ch,fsc);
if(j==0){
i=1;
while(((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')||(ch<='9'&&ch>='0'))&&i<=10) {
str1[j][i++]=ch;
cin.get(ch);fputc(ch,fsc);
}
cin.putback(ch);fseek(fsc,-1,1);
str1[j][i]='\0';
j++;
}
else if((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')) {
i=0;
while(((ch<='z'&&ch>='a')||(ch<='Z'&&ch>='A')||(ch<='9'&&ch>='0'))&&i<=10) {
str1[j][i++]=ch;
cin.get(ch);fputc(ch,fsc);
}
cin.putback(ch);fseek(fsc,-1,1);
str1[j][i]='\0';
j++;
}
else if(ch<='9'&&ch>='0'){
i=0;//str1[j][i]=ch;cin.get(ch);
while(ch<='9'&&ch>='0'){
str1[j][i]=ch;
cin.get(ch);fputc(ch,fsc);
i++;
}
cin.putback(ch); fseek(fsc,-1,1);
str1[j][i]='\0';
j++;
}
else if(indpt(ch)) {
str1[j][0]=ch;
//cin.get(ch);
str1[j][1]='\0';
j++;
}
else if(inopnd(ch)){
i=0;
tmp=ch;
str1[j][i]=ch;
i++;
cin.get(ch);fputc(ch,fsc);
if((ch=='=')&&((tmp=='<')||(tmp=='>'))) str1[j][i++]=ch;
else{ cin.putback(ch);fseek(fsc,-1,1);}
str1[j][i]='\0';
j++;
}
else {
if(ch!=' '&&ch!=10&&ch!=13&&ch!='.')
errstr[e++]=ch;
}
}
errstr[e]='\0';
cout<<"Number of words:"<<j<<endl;
fclose(fsc);
//delete fsc;

FILE *fp;
if ((fp = fopen("d:\\word.wj", "w"))== NULL){
fprintf(stderr, "Cannot creat file.\n");
return 1;
}
int tp;
fprintf(fp," Result:(Source file in source.wj)\n");
fprintf(fp,"type value (type:1=indent;2=var;3=digit;4=opnd;5=depart_table)\n");

for( k=0;k<j;k++){
tp=judge(str1[k]);
cout<<judge(str1[k])<<" ";

fprintf(fp," %d\t",tp);
fwrite(str1[k],sizeof(char),strlen(str1[k]),fp);
fprintf(fp,"\n");
cout<<str1[k]<<endl;
}
i=0;
fclose(fp);
if(errstr[0]!='\0') cout<<"wrong chars of your input:"<<errstr<<endl;
for(k=0;k<j;k++) delete str1[k];
delete fp;
textcolor(GREEN);
cout<<"Please view the result in ";
cprintf(" d:\\word.wj");
cout<<"!\n";
cprintf("Thank you for use Wanjia's WGT.\n");
return 1;
}
再次谢谢!!!
feiyun222 2002-06-29
  • 打赏
  • 举报
回复
feiyun@zuiwan.net
thanks
xiaoping222 2002-06-28
  • 打赏
  • 举报
回复
t.dp@263.net谢了!!!!!!!
azure711 2002-06-28
  • 打赏
  • 举报
回复
azure711@21cn.com
thanks
Hughling 2002-06-28
  • 打赏
  • 举报
回复
2019801@sina.com

我急需要,谁能现在发给我啊?谢谢了~~~
angel_mine 2002-06-28
  • 打赏
  • 举报
回复
哈,这么多人要呀!词法分析是小case,语法分析还真是有点难度的。
zhengguoshan 2002-06-28
  • 打赏
  • 举报
回复
我也要。。谢谢。。
zhengguoshan@21cn.com
gsmy 2002-06-27
  • 打赏
  • 举报
回复
llllf@371.net
谢谢
tobebj 2002-06-27
  • 打赏
  • 举报
回复
啊,这么多人,不知道我排队排得上不,哈哈。
tobebj@elong.com
I_love_programming 2002-06-27
  • 打赏
  • 举报
回复
woailinrui@163.com
jyek 2002-06-27
  • 打赏
  • 举报
回复
3X!!!!
jyek@etang.com
kesoft 2002-06-27
  • 打赏
  • 举报
回复
ok,my e-mail:
dingsoft@sina.com
lovepty 2002-06-27
  • 打赏
  • 举报
回复
谢谢各位大哥,别忘了我!
lemon_32@sina.com
sunspot_lee 2002-06-27
  • 打赏
  • 举报
回复
这种东西只要把语法文件写出来,可以用flex和bison自动生成,不用自己编,我曾经试着编了一个C的编译器前端,很方便的。
sunspot_lee 2002-06-27
  • 打赏
  • 举报
回复
这种东西只要把语法文件写出来,可以用flex和bison自动生成的,用得着自己编吗?我编了一个C语言的编译器前端,根本不费什么劲啊
mxlinux 2002-06-27
  • 打赏
  • 举报
回复
我也要 em70@163.net

thank you very much!
nicolas 2002-06-27
  • 打赏
  • 举报
回复
我也要:cage_yang@163.com
xiaoluoli 2002-06-27
  • 打赏
  • 举报
回复
luoli00320212@sina.com
lucaslee 2002-06-27
  • 打赏
  • 举报
回复
有的人上传一下,帖个地址出来,ok?
谢谢
加载更多回复(100)

69,373

社区成员

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

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