#include #include "stdafx.h" char letterprocess (char ch)//字母处理函数 char numberprocess(char ch)//数字处理程序 ////////////////////////////////////////////////////////////////////////////////////////////// char otherprocess(char ch)词法分析器
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <ctype.h>
#include <conio.h>
#define NULL 0
FILE *fp;
char ch;
char *keyword[8]={"do","begin","else","end","if","then","var","while"};
char *operatornum[4]={"+","-","*","/"};
char *comparison[6]={"<","<=","=",">",">=","<>"};
char *interpunction[6]={",",";",":=",".","(",")"};
//////////////////////////////////////////////////////////////////////////////////////////
bool search(char searchstr[],int wordtype)
{
int i;
switch (wordtype)
{
case 1:for(i=0;i<=7;i++)
{
if(strcmp(keyword[i],searchstr)==0)
return(true);
}
case 2:{
for(i=0;i<=3;i++)
{
if(strcmp(operatornum[i],searchstr)==0)
return(true);
}
break;
}
case 3: for(i=0;i<=5;i++)
{
if(strcmp(comparison[i],searchstr)==0)
return(true);
}
case 4: for(i=0;i<=5;i++)
{
if(strcmp(interpunction[i],searchstr)==0)
return(true);
}
}
return(false);
}
///////////////////////////////////////////////////////////////////////////////////////////
{
int i=-1;
char letter[20];
while (isalnum(ch)!=0)
{
letter[++i]=ch;
ch=fgetc(fp);
};
letter[i+1]='\0';
if (search(letter,1))
{
printf("<%s,->\n",letter);
//strcat(letter,"\n");
//fputs('<' letter '>\n',outp);
}
else
{
printf("<indentifier,%s>\n",letter);
//strcat(letter,"\n");
//fputs(letter,outp);
}
return(ch);
}
///////////////////////////////////////////////////////////////////////////////////////////
{
int i=-1;
char num[20];
while (isdigit(ch)!=0)
{
num[++i]=ch;
ch=fgetc(fp);
}
if(isalpha(ch)!=0)
{
while(isspace(ch)==0)
{
num[++i]=ch;
ch=fgetc(fp);
}
num[i+1]='\0';
printf("错误!非法标识符:%s\n",num);
goto u;
}
num[i+1]='\0';
printf("<num,%s>\n",num);
//strcat(num,"\n");
//fputs(num,outp);
u: return(ch);
}
{
int i=-1;
char other[20];
if (isspace(ch)!=0)
{
ch=fgetc(fp);
goto u;
}
while ((isspace(ch)==0)&&(isalnum(ch)==0))
{
other[++i]=ch;
ch=fgetc(fp);
}
other[i+1]='\0';
if (search(other,2))
printf("<relop,%s>\n",other);
else
if (search(other,3))
printf("<%s,->\n",other);
else
if (search(other,4))
printf("<%s,->\n",other);
else
printf("错误!非法字符:%s\n",other);
u: return (ch);
}
/////////////////////////////////////////////////////////////////////////////////////////////
void main ()
{
char str,c;
printf("**********************************词法分析器************************************\n");
//outp=fopen("二元式表.txt","w");
if ((fp=fopen("源程序.txt","r"))==NULL)
printf("源程序无法打开!\n");
else
{
str =fgetc(fp);
while (str!=EOF)
{
if (isalpha(str)!=0)
str=letterprocess(str);
else
{
if (isdigit(str)!=0)
str=numberprocess(str);
else
str=otherprocess(str);
}
};
printf("词法分析结束,谢谢使用!\n");
printf("点任意键退出!\n");
}
c=getch();
}
Word教程网
Excel教程网
Dreamweaver教程网
Fireworks教程网
PPT教程网
FLASH教程网
PS教程网
HTML教程网
DIV CSS教程网
FLASH AS教程网
ACCESS教程网
SQL SERVER教程网
C语言教程网
JAVASCRIPT教程网
ASP教程网
ASP.NET教程网
CorelDraw教程网
关于我们 | 教程购买 | 广告刊登 | 网站地图 |湖北继续教育网 |QQ:2693987339(点击联系)购买教程光盘
地址:湖北省武汉市曹家湾32号501室 电话:027-86646545 15972130058--教程购买问题汇总
21视频教程网专业的网站开发视频教程学习网站
ICP备案号:鄂ICP备14009716号-13
公安备案号:42011102002974