编译原理词法分析实现

来源:互联网 发布:手机站如何优化 编辑:程序博客网 时间:2024/06/05 02:32

通过正则表达式实现对单词的识别


项目描述:

通过状态转换图实现对单词的识别

  • 输入:符号串
  • 输出:yes/no

C++代码:

#include <iostream>#include <string.h>using namespace std;bool Judge(char str[],int n){    int i=-1;    int sta=1;    while((++i)<n)    {        switch(sta)        {        case 1:            if(str[i]=='a')                sta=2;            else return false;            break;        case 2:            if(str[i]=='b')                sta=3;            else return false;            break;        case 3:            if(str[i]=='a')                sta=4;            else return false;            break;        case 4:            if(str[i]=='a')                sta=5;            else if(str[i]=='b')                sta=4;            else return false;            break;        case 5:            if(str[i]=='a')                sta=5;            else if(str[i]=='b')                sta=4;            break;        default:             return false;        }    }    if(sta==5)        return true;    else return false;}int main(){    char str[100];    while(cin>>str)    {    if(Judge(str,strlen(str)))        cout<<"Yes"<<endl;    else        cout<<"No"<<endl;}    return 0;}

运行截图:

这里写图片描述

0 0