// aaa.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
char GetChar(char *input,int *index,int length);
int ClearBlank(char *input,int *index,int length);
int reserve(char *s);
void lrparser(char *input,int inputLength,int *index);
void yucu(char *input,int inputLength,int *index);
void factor(char *input,int inputLength,int *index);
void statement(char *input,int inputLength,int *index);
void expression(char *input,int inputLength,int *index);
void term(char *input,int inputLength,int *index);
char *retab[6]={"begin","if","then","while","do","end"};//关键字
int syn=0;
int myIsAlpha(char ch)
{
if(islower(ch)==2 || isupper(ch)==1)
{
return 1;
}
else
{
return 0;
}
}
void scaner(char *input,int inputLength,int *index)
{
char s[256]=""; //保存当前的字符
char ch=GetChar(input,index,inputLength);
int nowPosition=0;
int j=0;
if(myIsAlpha(ch)==1) //如果是字母
{
while(((ch>='0' &&ch<='9') || (myIsAlpha(ch)==1 ) )&& *index <=inputLength)
{
s[nowPosition]=ch; //添加到当前字符串中
nowPosition++;
ch=GetChar(input,index,inputLength);
}
if((ch<'0' || ch>'9') &&(myIsAlpha(ch)==0 ) )//进行回退操作,并输出结果
{
s[nowPosition]='\0';//添加结束标志
j=reserve(s);
if(j==0)
{
syn=10;
}
else
{
syn=j;
}
(*index)--;
return;
}
else //超过范围
{
s[nowPosition++]=ch;
s[nowPosition]='\0';//添加结束标志
j=reserve(s);
if(j==0)
{
syn=10;
}
else
{
syn=j;
}
getchar();
exit(0);
return;
}
}
else
if(ch>='0' &&ch<='9') //如果是数字
{
while(ch>='0' &&ch<='9'&& *index <=inputLength)
{
s[nowPosition]=ch; //添加到当前字符串中
nowPosition++;
ch=GetChar(input,index,inputLength);
}
if(ch<'0' || ch>'9')//进行回退操作
{
(*index)--;
syn=11;
return;
}
else //超过范围时
{
s[nowPosition]=ch;
syn=11;
return;
}
}
else
{
switch(ch)
{
case '+':
{
syn=13;
return;
}
case '-':
{
syn=14;
return ;
}
case '*':
{
syn=15;
return ;
}
case '/':
{
syn=16;
return ;
}
case '<':
{
ch=GetChar(input,index,inputLength);
if(ch=='=')
{
syn=22;
return ;
}
else
if(ch=='>')
{
syn=21;
return ;
}
else
{
syn=20;
if(*index>inputLength)
{
return;
}
else
{
(*index)--;
return ;
}
}
}
case '>':
{
ch=GetChar(input,index,inputLength);
if(ch=='=')
{
syn=24;
return ;
}
else
{
syn=23;
if(*index>inputLength)
{
return;
}
else
{
(*index)--;
return ;
}
}
}
case ':':
{
ch=GetChar(input,index,inputLength);
if(ch=='=')
{
syn=18;
return ;
}
else
{
if(*index>inputLength)
{
return;
}
else
{
(*index)--;
return ;
}
}
}
case '=':
{
syn=25;
return ;
}
case ';':
{
syn=26;
return ;
}
case '(':
{
syn=27;
return ;
}
case ')':
{
syn=28;
return ;
}
case '#':
{
syn=0;
return ;
}
case ' ':
{
syn=-1;
return ;
}
default:
{
printf("(非法符号)");
}
}
}
}
//与关键字进行比较
int reserve(char *s)
{
if(strcmp(s,retab[0])==0)
{
return 1;
}
else
if(strcmp(s,retab[1])==0)
{
return 2;
}
else
if(strcmp(s,retab[2])==0)
{
return 3;
}
else
if(strcmp(s,retab[3])==0)
{
return 4;
}
else
if(strcmp(s,retab[4])==0)
{
return 5;
}
else
if(strcmp(s,retab[5])==0)
{
return 6;
}
else
{
return 0;
}
}
//获取index的char
char GetChar(char *input,int *index,int length)
{
if(*index <= length)
{
(*index)++;
return input[(*index)-1];
}
else
return 0;
}
int ClearBlank(char *input,int *index,int length)
{
while( (*index) != length)
{
if(input[(*index)] ==32 && (*index) != length)
{
((*index))++;
}
else
if( input[(*index)] ==32 && (*index) == length)
{
printf("\n谢谢使用!\n");
getchar();
exit(0);
}
else
{
return 1;
}
}
return 0;
}
//语法分析
void lrparser(char *input,int inputLength,int *index)
{
if (syn==1) //表示begin
{
scaner(input,inputLength, index);
while(syn==-1) //若为空格串读
{
scaner(input,inputLength, index);
}
yucu(input,inputLength, index); //
if(syn == 6) //end
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
if(syn==0 ) //#
{
printf("success\n");
getchar();
return;
}
}
}
else
{
printf("error!");
return;
}
}
void yucu(char *input,int inputLength,int *index)
{
statement(input,inputLength, index); //语句段分析
while (syn==26) //;
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
statement(input,inputLength, index); //语句段分析
}
return ;
}
// 语句段分析
void statement(char *input,int inputLength,int *index)
{
if(syn ==10)//为字符串
{
scaner(input,inputLength, index);
while(syn==-1) //空格
{
scaner(input,inputLength, index);
}
if(syn==18) //赋值符号
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
expression(input,inputLength, index); //表达式分析
}
else
{
printf("输出赋值号错误!\n");
getchar();
exit(0);
}
}
else
{
printf("输出语句错误!%d\n",syn);
getchar();
exit(0);
}
return ;
}
//表达式处理
void expression(char *input,int inputLength,int *index)
{
term(input,inputLength, index);
while(syn == 13|| syn ==14)//+-
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
term(input,inputLength, index );//* /
}
return ;
}
void term(char *input,int inputLength,int *index)
{
//括号分析
factor(input,inputLength, index );
while(syn == 15|| syn ==16) //*/
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
//括号分析
factor(input,inputLength, index );
}
return ;
}
//括号分析
void factor(char *input,int inputLength,int *index)
{
if(syn==10 || syn==11) //如果为标识符或者位数字
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
}
else
if(syn ==27) //左括号
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
expression(input,inputLength, index ); //表达式分析
if(syn==28)//)括号
{
scaner(input,inputLength, index);
while(syn==-1)
{
scaner(input,inputLength, index);
}
}
else
{
printf("输出)错误");
getchar();
exit(0);
}
}
else
{
printf("输出表达式错误");
getchar();
exit(0);
}
return ;
}
int main(void)
{
char inputString[80];
int i =0;
int inputLength=0;
char ch;
printf("请输入字符串",islower('a'));
printf("请输入字符串:");
while((ch =getchar()) != 10)
{
inputString[inputLength++]=ch;
}
inputLength--;
printf("\n输出序列:");
scaner(inputString,inputLength, &i);
while(syn==-1)//去除前面的所有空格
{
scaner(inputString,inputLength, &i);
}
lrparser(inputString, inputLength,&i);
printf("\n谢谢使用,按任何键退出!\n");
getchar();
}
语法分析
最新推荐文章于 2022-05-20 00:56:13 发布