有限自动机与构造

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
typedef int state;
typedef int condition;  

#define STATENUM 4
#define STATE1 0
#define STATE2 1
#define STATE3 2
#define STATETRAP 3  

#define CONDITIONS 2
#define CONDITION1 0
#define CONDITION2 1  

typedef void (* actiontype)(state mystate,condition mycondition);
typedef struct{
  state next;
  actiontype action;
}trasition, *ptrasition;  

void action1(state mystate,condition myconditon);
void action2(state mystate,condition myconditon);
void action3(state mystate,condition myconditon);
void actiontrap(state mystate,condition myconditon);
trasition t1={
  STATE2,action1
};
trasition t2={
  STATE3,action2
};
trasition t3={
  STATE2,action3
};
trasition tt={
  STATETRAP,actiontrap
};  

void action1(state mystate,condition myconditon){
  printf("action1 one triggered\n");
}
void action2(state mystate,condition myconditon){
  printf("action2 one triggered\n");
}
void action3(state mystate,condition myconditon){
  printf("action3 one triggered\n");
}
void actiontrap(state mystate,condition myconditon){
  printf("actiontrap one triggered\n");
}  

ptrasition transition_table[STATENUM][CONDITIONS] = {
/*      c1,  c2*/
/* s1 */&t1, &tt,
/* s2 */&tt, &t2,
/* s3 */&t3, &tt,
/* st */&tt, &tt,
};
typedef struct
{
    state current;
} StateMachine, * pStateMachine;  

state step(pStateMachine machine, condition mycondition)
{
    ptrasition t = transition_table[machine->current][mycondition];
    (*(t->action))(machine->current, mycondition);
    machine->current = t->next;
    printf("the current state is %d\n",t->next );
    return machine->current;
}
int main(int argc, char *argv[])
{
  StateMachine mymachine;
  mymachine.current=STATE1;
  int mycon;
  char ch;
  while(1){
    scanf("%d",&mycon);
    step(&mymachine,mycon);
  }
  return 0;
}  
时间: 2024-10-10 21:36:53

有限自动机与构造的相关文章

实验三有限自动机的构造与识别

#include<stdio.h> #include <ctype.h> #define ok 1 #define error 0 #define MAXREGLUARLONG 40 #define MAXSTATELONG 40 #define MAXCAHRSLONG 40 typedef int state; int iCurrentState=0; //初态以1开始 int iPreState=0; int iLastForkState=0; int iForkState=

实验三 有限自动机的构造与识别

一.实验目标 1.掌握有穷状态自动机的概念: 2.掌握有穷状态自动机的存储及表示方法: 3.掌握有穷状态自动机与正则式之间的关系. 二.实验要求 1.输入正规式: 2.构造该正规式的有穷状态自动机: 3. 以五元组形式输出. 三.算法 参见教材的转换规则. 练习: 2  (a|b)*abb 2  l(l|d)* 2  1(1010*|1(010)*1)*0 四.完成算法设计.编码和调试工作,完成实验报告. #include<string.h> #include<stdio.h> #

1118有限自动机的构造与识别

1 #include<string.h> 2 #include<stdio.h> 3 #include<stdlib.h> 4 int main() 5 { 6 char p[30][30]; 7 char q[30][30]; 8 int line=0; 9 int n; 10 int i,j; 11 int count=0; 12 int k,t=0; 13 int flag=0; 14 int l,m=0; 15 char VN[30]={'\0'}; 16 ch

1210有限自动机的构造与识别

#include<string.h>#include<stdio.h>#include<stdlib.h>int main(){char p[30][30];char q[30][30];int line=0;int n;int i,j;int count=0;int k,t=0;int flag=0;int l,m=0;char VN[30]={'\0'};char VT[30]={'\0'};printf("规则数:");scanf("

1118实验三有限自动机的构造与识别

#include<string.h>#include<stdio.h>#include<stdlib.h>int main(){ char p[30][30]; char q[30][30]; int line=0; int n; int i,j; int count=0; int k,t=0; int flag=0; int l,m=0; char VN[30]={'\0'}; char VT[30]={'\0'}; printf("请输入规则个数:&quo

有限自动机的构造和识别

#include<string.h>#include<stdio.h>#include<stdlib.h>int main(){    char p[30][30];//存放文法    char q[30][30];    int line=0;    int n;    int i,j;    int count=0;    int k,t=0;    int flag=0;    int l,m=0;    char VN[30]={'\0'};//存放非终结符号 

第三次实验--有限自动机的构造与识别

实验目的:通过正规式构造NFA(有穷自动机)方法. 所运用到的语法规则: 语句                          规则 1.R=? /R=ε            X-----ε----->Y 2.R=a                     X-----a----->Y 3.R=r1r2                A-----r1----->B-----r2----->C 4.R=r1|r2                5.R=r1*          

有限自动机的构造与识别3

#include<stdio.h> #define N 30 char str[N]={NULL}; int st[N]; int t=0,k=0,a,t1=0; struct node//定义一个队列 { char data; struct node * next; }; typedef struct node QueueNode; struct node2//定义一个链队列 { QueueNode *front; QueueNode *rear; }; typedef struct nod

1118 实验三 有限自动机的构造与识别

#include<stdio.h>char gra[80];char ch;int m=0,p=0;struct{ int A; char R[10]; int B;}NFA;main(){ char gra[10]; printf("---------------正规文法转换成有穷自动机------------------\n"); printf("请输入正规文法R:"); do{ ch=getchar(); gra[p++]=ch; }while(c

有限自动机的构造与识别

#include "iostream.h"#include "string.h" #include "fstream.h"#define NULL 0 class TransTile{public: char current; char next; char input; TransTile(char C,char I,char Ne){ current = C; next = Ne; input = I; }}; class DFA{publi