📄 a1.cpp
字号:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <conio.h>
#include <iostream>
using namespace std;
//#define NULL 0
/*
struct buf
{
char kind;
char value;
}
buf bb[100];
*/
string Box[200];
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);
}
char letterprocess (char ch)//字母处理函数
{
if (isalpha(ch)!=0)
{
int i=-1;
char letter[20];
while (isalnum(ch)!=0)
{
letter[++i]=ch;
ch=getchar();
}
letter[i+1]='\0';
if (search(letter,1))
{
cout<<"("<<letter<<",~~ )"<<endl;
}
else
{
cout<<"( indentifier,"<<letter<<" )"<<endl;
}
}
/* return(ch);
}
char numberprocess(char ch)//数字处理程序
{*/
/*int*/
else
{
if (isdigit(ch)!=0)
{
int i=-1;
char num[20];
while (isdigit(ch)!=0)
{
num[++i]=ch;
ch=getchar();
}
if(isalpha(ch)!=0)
{
while(isspace(ch)==0)
{
num[++i]=ch;
ch=getchar();
}
num[i+1]='\0';
cout<<"错误!非法标识符: " <<num<<endl;
goto A;
}
num[i+1]='\0';
cout<<" ( digit,"<<num<<" )"<<endl;
A: cout<< " "<<endl;
}
/* return(ch);
}
char otherprocess(char ch)
{*/
/*int*/
else
{
int i=-1;
char other[20];
if (isspace(ch)!=0)
{
ch=getchar();
goto B;
}
while ((isspace(ch)==0)&&(isalnum(ch)==0))
{
other[++i]=ch;
ch=getchar();
}
other[i+1]='\0';
if (search(other,2))
cout<<"("<<other<<",~~ ) "<<endl;
else
if (search(other,3))
cout<<"("<<other<<" , ~~ )"<<endl;
else
if (search(other,4))
cout<<"("<<other<<" , ~~ )"<<endl;
else
//printf("错误!非法字符:%s\n",other);
cout<<" 错误_____非法字符: "<<other<<endl;
B: return (ch);
}
}
}
void main()
{
S:
cout<<"***************************词法分析器*********************************"<<endl<<"****** 输入源程序:"<<endl;
char str;
str=getchar();
while( str!='#')
{
/*if (isalpha(str)!=0)
str=letterprocess(str);
else
{
if (isdigit(str)!=0)
str=numberprocess(str);
else
str=otherprocess(str);
}*/
letterprocess(str);
}
cout<<"是否继续下一个词法分析[ y/ ~~]:";
char ch;
cin>>ch;
if(ch=='y')
goto S;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -