编译原理词法分析程序

编译原理词法分析程序,第1张

#include <iostream>

#include <ctype.h>

#include <fstream>

#include <string.h>

#include <malloc.h>

using namespace std

ifstream fp("source.txt",ios::in)

char cbuffer

char *key[13]={"if","else","for","while","do","return","break","continue","int","void"

,"main","const","printf"}//关键字

char *border[7]={ "," , "" , "{" , "袭喊}" , "(" , ")" ,"//"} //拍键野分界符

char *arithmetic[6]={"+" , "-" , "*" , "/" , "++" , "--"} //运算符

char *relation[7]={"<" , "<=" , "=" , ">" , ">=" , "==" ,"!="}//关系运算符

char *lableconst[80] //标识符

int constnum=40

int lableconstnum=0

int linenum=1 //统计常数和标识符数量

int search(char searchchar[],int wordtype)

{

int i=0,t=0

switch (wordtype)

{

case 1:

{ for (i=0i<=12i++) //关键字

{

if (strcmp(key[i],searchchar)==0)

return(i+1)

}

return(0)}

case 2:

{

for (i=0i<=6i++)//分界符

{

if (strcmp(border[i],searchchar)==0)

return(i+1)

}

return(0)

}

case 3:

{

for (i=0i<=5i++)//运算符

{

if (strcmp(arithmetic[i],searchchar)==0)

return(i+1)

}

return(0)

}

case 4:

{

for (i=0i<=6i++)//关系运算符

{

if (strcmp(relation[i],searchchar)==0)

return(i+1)

}

return(0)

}

case 5:

{

for (t=40t<=constnumt++) //常数

{

if (strcmp(searchchar,lableconst[t])==0)//判断该常数是否已出现过

return(t+1)

}

lableconst[t-1]=(char *)malloc(sizeof(searchchar))//为新的元素分配内存亮行空间

strcpy(lableconst[t-1],searchchar)//为数组赋值lableconst指针数组名

constnum++ //常数个数自加

return(t)

}

case 6:

{

for (i=0i<=lableconstnumi++)

{

if (strcmp(searchchar,lableconst[i])==0) //判断标识符是否已出现过

return(i+1)

}

lableconst[i-1]=(char *)malloc(sizeof(searchchar))

strcpy(lableconst[i-1],searchchar)

lableconstnum++ //标识符个数自加

return(i)

}

default:cout<<"错误!"

}

}

char alphaprocess(char buffer)//字符处理过程

{

int atype

int i=-1

char alphatp[20]

while ((isalpha(buffer))||(isdigit(buffer)))

//这两个函数分别是判字符和判数字函数位于ctype.h中

{

alphatp[++i]=buffer

fp.get(buffer)

}

alphatp[i+1]='\0'//在末尾添加字符串结束标志

if (atype=search(alphatp,1))

cout<<"linenum: "<<linenum<<" String= "<<alphatp<<"\t\t\t"<<"关键字"<<endl

else

{

atype=search(alphatp,6) //标识符

cout<<"linenum: "<<linenum<<" String= "<<alphatp<<"\t\t\t"<<"标识符"<<endl

}

return(buffer)

}

char digitprocess(char buffer) //数字处理过程

{

int i=-1

char digittp[20]

int dtype

while ((isdigit(buffer)))

{

digittp[++i]=buffer

fp.get(buffer)

}

digittp[i+1]='\0'

dtype=search(digittp,5)

cout<<"linenum: "<<linenum<<" String= "<<digittp<<"\t\t\t"<<"数据"<<endl

return(buffer)

}

char otherprocess(char buffer) //分界符、运算符、逻辑运算符、等

{

int i=-1

char othertp[20]

int otype,otypetp

othertp[0]=buffer

othertp[1]='\0'

if (otype=search(othertp,3))

{

fp.get(buffer)

othertp[1]=buffer

othertp[2]='\0'

if (otypetp=search(othertp,3)) //判断该运算符是否是

//由连续的两个字符组成的

{

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"运算符"<<endl

fp.get(buffer)

goto out

}

else //单字符逻辑运算符

{

othertp[1]='\0'

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"逻辑运算符"<<endl

goto out

}

}

if (otype=search(othertp,4)) //关系运算符

{

fp.get(buffer)

othertp[1]=buffer

othertp[2]='\0'

if (otypetp=search(othertp,4)) //判断该关系运算符是否是

//由连续的两个字符组成的

{

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"关系运算符"<<endl

fp.get(buffer)

goto out

}

else //单字符逻辑运算符

{

othertp[1]='\0'

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"逻辑运算"<<endl

goto out

}

}

if (buffer=='!') //"=="的判断

{

fp.get(buffer)

if (buffer=='=')

//cout<<"!= (2,2)\n"

fp.get(buffer)

goto out

}

else

{

if (otype=search(othertp,2)) //分界符

{

cout<<"linenum: "<<linenum<<" String= "<<othertp<<"\t\t\t"<<"分界符"<<endl

fp.get(buffer)

goto out

}

}

if ((buffer!='\n')&&(buffer!=' '))

cout<<"错误!,字符非法"<<"\t\t\t"<<buffer<<endl

fp.get(buffer)

out: return(buffer)

}

void main()

{

int i

for (i=0i<=50i++)

{

lableconst[i]=" "//用于保存标识符

}

if (!fp)

cout<<"文件打开错误!!"<<endl

else

{

fp.get (cbuffer)

while (!fp.eof())

{

if(cbuffer=='\n')

{

linenum++

fp.get(cbuffer)

}

else if (isalpha(cbuffer))

{

cbuffer=alphaprocess(cbuffer)

}

else if (isdigit(cbuffer))

{

cbuffer=digitprocess(cbuffer)

}

else

cbuffer=otherprocess(cbuffer)

}

}

cout<<"标识符个数是:"<<lableconstnum<<"分别是"<<endl

i=0

while(i<lableconstnum)

{

cout<<lableconst[i++]<<" "

}

cout<<endl

cout<<"完成\n"

getchar()

}

#include<stdio.h>#include<string.h>#define nmax 14 #define norw 8 /*关键字个数*/ #define al 10 #define getchdo if(-1==getch()) return -1 #define getsymdo if(-1==getsym()) return -1 char ch/*获取字符的缓冲区,getch使用*/ char word[norw][al]/*保留字*/ char fname[al]/*文件名*/ char id[al+1]/*存放标识符或保留字迟羡*/ char line[81]/*读取行缓冲区*/ char a[al+1]/*临时符号,多处的字节用于存放0*/ char anum[nmax+1]/*临时符号,存放number*/ char inum[nmax+1]/*存放常数*/ int cc,ll/*cc表示当前字符(ch)的位置*/ int num/*常数*/ int err//错误计数器 FILE * finFILE * foutFILE * fas/*词法分析结果文件*/ /*函数执行出错,退出程序*/ int getch()/*读取一行字符*/ int getsym() /*从文件fin中读取一行字符,保存到字符缓冲区line中*/ int getch() { if(cc==ll) { ll=0cc=0ch=' 'while(ch!=10) { if(feof(fin)) { return -1} if(EOF==fscanf(fin,"%c",&ch)) { line[ll]=0break} line[ll]=chll++} } ch=line[cc]cc++return 0} /*读取一个分词*/ int getsym() { int i,j,kwhile(ch==' '||ch==10||ch==9) //忽略空格,换行和TAB { getchdo} if(ch>='a'&&ch<='z') //判断是否为关键字或标识符败祥 { k=0do{ if(k<al){ a[k]=chk++} getchdo}while(ch>='a'&&ch<='z'||ch>='0'&&ch<='9')a[k]=0if(k>al)printf("error")strcpy(id,a)i=0j=norw-1do{ k=(i+j)/2if(strcmp(id,word[k])<=0) {j=k-1} if(strcmp(id,word[k])>=0) { i=k+1} }while(i<=j)if(i-1>j) { fprintf(fas,"Keyword %s\n",id)/*分词为关键字*/ } return 0} 这里面的代码写不开,你加我的QQ:412686686,我直接传文件给你好了,你这个问察旦搏题的代码太多了,我刚好昨天给别人写了一个。

1、在电脑任意盘新建一个文本文档→右键选择Edit with Vim打开就可以了,便可以开始进行编写语言。

2、进入gVim之后输入i才可以整成输入代码,输入i之后缓枝下框边显示输入,这个时候就可以输入你要首哪缺输入的代码了。

3、Windows+R→cmd→输入如图所示开始编译(选择编译文件所在的系统盘)。

4、编译成功后,接下来我们想象更好玩的,我们可以更改界面的北京还有字体了。我就列者辩举一下黑色背景复制下面代码。

5、右键打开使用Vim打开。

6、把调整代码粘贴到代码行列中就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/12234315.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存