Program DFA Sederhana dengan bahasa C

/* 
Contoh Program DFA sederhana , untuk parsing  
 buat file testdfa misal isinya spt ini:
   a  b
>0 1  0
 1 2  0
*2 0  3

*/

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

char  transfer(char state, char huruf, char state_x[],char state_y[]);

main()
{
FILE *fp;
char baris[20], kar1[5], kar2[5], kar3[5],stringmasukan[10],state_a[5],state_b[5],state_cur[5];
char matrik[20][3];
int  i, s, f ,j;

fp=fopen("testdfa","r");
if (fp==NULL) {
    printf("file tdk bisa dibuka\n");
    exit(1);
}
/*
if(argc!=3) {
    printf("You forgot to enter a filename.\n");
    exit(1);
  }

  if((fp=fopen(argv[1], "r"))==NULL) {
    printf("Cannot open source file.\n");
    exit(1);
  }

*/

//printf(" %s\t %s\t %s\n ", argv[1], argv[2],argv[3])

printf("\n");

printf("==========================\n");

printf("data tabel transfer DFA : \n");

printf("==========================\n");

i= 0;
while (fgets(baris,sizeof baris,fp)!=NULL) {    // baca tiap baris  sampai fp ga ada char/null

//printf("data baris= %d = %s",i,baris);

sscanf(baris,"%s %s %s",kar1,kar2,kar3);   // salin string tiap baris ( dipisah spasi)ke variable
printf("%s",baris);

      if (strstr(kar1,">")!=NULL)  s=i;
      if (strstr(kar1,"*")!=NULL)  f=i;

state_cur[i]=kar1[0];
state_a[i]=kar2[0];
state_b[i]=kar3[0];
/*
printf("state_cur[i] = %c\t",state_cur[i]);
printf("state_a[i] = %c\t",state_a[i]);
printf("state_b[i] = %c\n",state_b[i]);
*/
i++;
}

//printf("index s f = %d  %d\n", s,f);
//printf("Start State Is = %c | Final State Is = %c\n",state_cur[s], state_cur[f]);
printf("start state = %c \n",state_cur[s]);
printf("Final state = %c\n", state_cur[f]);

char start_state = state_cur[s];
printf("\n");

printf("==========================================\n");
printf("masukan string:(max 10 huruf kecil a,b ) : ");
fgets(stringmasukan,sizeof stringmasukan,stdin);
//printf("data yg anda masukan = %s jml huruf= %d\n ",stringmasukan,strlen(stringmasukan)-1);
printf("==========================================\n");

int k = strlen(stringmasukan)-1;
int m=0;
char last_state = start_state;

for(m=0;m<k;m++)
{
 char cur_huruf = stringmasukan[m];
last_state = transfer(last_state,cur_huruf,state_a,state_b);
printf("input : %c\t",cur_huruf);
printf("menuju state => : %c\n",last_state);
}
printf("==========================================\n");

printf("state akhir : %c\n\n\n",last_state);

if(last_state== state_cur[f]) {
printf("###################\n");
printf("# string diterima #\n");
printf("###################\n\n\n"); }
else{
printf("###################\n");
printf("# string di TOLAK #\n");
printf("###################\n\n\n"); }

return(0);
}

char transfer(char state, char huruf ,char state_x[],char state_y[])
{
char current_state;
//extern state_a,state_b;
int indek;

 switch (state)
{
        case '0' : indek=0 ; break;
        case '1' : indek=1 ; break;
        case '2' : indek=2 ; break;
        case '3' : indek=3 ; break;
}

if(huruf== 'a')
{ current_state = state_x[indek]; }

if(huruf== 'b')
{
current_state = state_y[indek];
}

return (current_state);

}
Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis untuk pemula.

Posted on 06/07/2011, in Contoh-contoh Aplikasi. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: