#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()
}
词法分析时单词的识别根据构词规则识别单词。
1、词法分析的任务
词法分析是编译的第一个阶段,其任务是:从左至右逐个字符地对源程序(用高级语言编写的)进行扫描,产生一个个单词符号,把字符串形式的源程序改造成为单词符号串形式的中间程序。
2、词法分析器
执行词法分析的程序称为词法分析程序,也称为词法分析器或扫描器。词法分析器的功能是输入源程序,输出单词符号。
3、词法分析的两种处理结构
(1)把词法分析程序作为主程序。即,把词法分析与语法分析明显分开,由词法分析程序将字符串形式的源程序改造成单词符号串形式的中间程序,以这个中间程序作为语法分析程序的输入。在这种处理结构中,词法分析和语法分析实际上是分别实现的。
(2)把词法分析程序作为语法分析程序调用的子程序。在进行语法分析时,每当语法分析程序需要一个单词时,便调用词法分析程序,词法分析程序每一次调用便从字符串源程序中识别出一个单词交给语法分析程序。词法分析和语法分析实际上是交替进行的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)