📄 复件 zhushizifu.cpp
字号:
//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <ctype.h>
#include <conio.h>
#include <iostream> /*.....*/
using namespace std; /*.....*/
#define NULL 0
FILE *fp;
char ch;
char *key1[33]={"and","array","begin","bool","call","case","char","constant","do","else",
"end","false","for","if","input","integer","not","of","or","output",
"procedure","program","read","real","repeat","set","then","to","true",
"until","var","while","write"};
char *key2[12]={"我","(",")","*","我","+",",","-",".","..","我","我"};
char *key3[11]={":",":=",";","<","<=","<>","=",">",">=","[","]"};
//查找表
bool search(char searchstr[],int wordtype)
{
int i;
switch (wordtype)
{
case 1:for(i=0;i<=32;i++)
{
if(strcmp(key1[i],searchstr)==0)
{ printf("<%d,%s>\n",i+1,key1[i]);
return(true);
}
}
case 2:{
for(i=0;i<=12;i++)
{
if(strcmp(key2[i],searchstr)==0)
{ printf("<%d,-->\n",i+38);
return(true);
}
}
break;
}
case 3: for(i=0;i<=10;i++)
{
if(strcmp(key3[i],searchstr)==0)
{ //printf("<%d,%s>\n",i+50,key3[i]);
printf("<%d,-->\n",i+50);
return(true);
}
}
//default:cout<<"error!"; /*......*/
}
return(false);
}
//字母处理
char letterpro (char ch)//字母处理函数
{
int i=-1;
char letter[20];
while (isalnum(ch)!=0)
{
letter[++i]=ch;
ch=fgetc(fp);
};
letter[i+1]='\0';
if (search(letter,1))//key1
;
else
printf("<biaoshi,%s>\n",letter);
return(ch);
}
char numberpro(char ch)//数字处理程序
{
int i=-1;
char num[20];
while (isdigit(ch)!=0)
{
num[++i]=ch;
ch=fgetc(fp);
}
if(isspace(ch)!=0)//整常数
{
num[++i]='\0';
printf("<35,%s>\n",num);
}
if(ch=='.')//实符常数
{ num[++i]='.';
ch=fgetc(fp);
while (isdigit(ch)!=0)
{
num[++i]=ch;
ch=fgetc(fp);
}
num[++i]='\0';
printf("<36,%s>\n",num);
}
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';
u: return(ch);
}
//注释处理
char zhushipro(char ch)
{ char zhushi[20];
int i=-1;
ch=fgetc(fp);
//if(ch!='*') {printf("<48,-->\n");goto u;}
if(ch=='*')
{ printf("<49,-->\n");
zhushi[++i]=ch;
ch=fgetc(fp);
m:while(ch!='*') {zhushi[++i]=ch;ch=fgetc(fp);}
if(ch=='*')
{ zhushi[++i]=ch;
ch=fgetc(fp);
if(ch=='/')
{ printf("<42,-->\n");ch=fgetc(fp);goto u;}
if(ch!='/')
{ zhushi[++i]=ch;ch=fgetc(fp);goto m;}
}
//goto u;
}
if(ch!='*') {printf("<48,-->\n");goto u;}
u: return (ch);
}
char zifupro(char ch)
{ int i=-1;
char zifu[10];
printf("<38,-->\n");
ch=fgetc(fp);
while(ch!='\'')
{ zifu[++i]=ch;
ch=fgetc(fp);
//if(i>20)//字符常数长度〉50,出错
//{ cout<<"too long zifuchuan\n";
// goto u;
//}
}
if(ch=='\'')
{ zifu[++i]='\0';
printf("<37,%s>\n",zifu);
printf("<38,-->\n");
ch=fgetc(fp);
}
return (ch);
}
char otherprocess(char 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))
;
else
if (search(other,3))
;
else
printf("错误!非法字符:%s\n",other);
u: return (ch);
}
void main ()
{
char str,c;
printf("词法分析器\n");
if ((fp=fopen("源程序.txt","r"))==NULL)
printf("源程序无法打开!\n");
else
{
str =fgetc(fp);
while (str!=EOF)
{
if (isalpha(str)!=0)
str=letterpro(str);
else if(isdigit(str))
//(str<='9'&&str>='0')
str=numberpro(str);
else if(str=='/')
str=zhushipro(str);
else if(str=='\'')
str=zifupro(str);
else
str=otherprocess(str);
};
printf("词法分析结束!\n");
printf("点任意键退出!\n");
}
c=getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -