2016-09-25 2 views
0

Ich versuche Flex-Programm zu implementieren, die unten Szenario erkennen können:erste Zeichen nach dem vollständigen Stopp in Textdatei Lex mit

Eingabe.txt:

Hallo, mein Name ist Kamalakar. also bin ich glücklich.

Während oben Eingabe.txt mein Programm-Datei lesen, sollten erkennen, nach dem vollständigen Stillstand gibt es einfache grammatische Fehler und es versuchen, es zu korrigieren mögen:

Hallo, mein Name Kamalakar ist. Also bin ich glücklich.

Ich erkenne bereits den Punkt, brauche weitere Vorschläge, wie man es ersetzt.

----------- Lex Datei --------

digit [0-9] 
letter [A-Za-z] 
%option noyywrap 
%{ 
    #include<stdio.h> 
    #include<unistd.h> 
    int count = 0; 
    void replace(char*); 
%} 

%% 
{letter}({letter}|{digit})* count++; 
({letter}|[\w])*"."[" "]+({letter}|[\w])* { 
printf("Full Stop found !! and your line is, %s",yytext); 
replace(yytext); 
} 


%% 

int main(int argc, char *argv[]) 
{ 
    yyin = fopen(argv[1], "r"); 
    yylex(); 
    //yywrap(); 

    printf("Count : %d",count); 
    fclose(yyin); 
    return 0; 
} 

void replace(char* data) 
{ 
    // suggest some ways to replace it. 
} 
+0

ich bereits zum vollständigen Stillstand erkennen ! brauche weitere Vorschläge, wie man es ersetzt !! – Kamalakar

Antwort

0

Schließlich finde ich die Antwort ..

digit [0-9] 
letter [A-Za-z] 
%option noyywrap 
%{ 
    #include<stdio.h> 
    #include<unistd.h> 
    char *yytext2 ; 
    char* replace(char*); 
    FILE *ff,*fw; 

%} 

%% 

({letter}|[\w])*"."[" "]+({letter}|[\w])* { 
yytext2 = (char *) malloc(sizeof(yytext)); 
yytext2 = replace(yytext); 
fprintf(fw,yytext2,"%s"); 
} 


{letter}+ {fprintf(fw,yytext,"%s");} 
[" "] {fprintf(fw,yytext,"%s");} 
["\n"] {fprintf(fw,yytext,"%s");} 

%% 

int main(int argc, char *argv[]) 
{ 
    ff=fopen(argv[1],"r"); 
    fw=fopen("rep.txt","w"); 
    char ch = fgetc(ff); 
    if(ch>96){ch = ch - 32;} 
    printf(" First Character should be : %c",ch); 
     yyin=ff; 
    yylex(); 
    fclose(yyin); 
    fclose(fw); 
    return 0; 
} 

char* replace(char* data) 
{ 
    printf("\n Original String :\t %s",data); 
    int j,flag = 0,i; 
    char lower, upper; 
    char dup[50]; 
    for(j=0;data[j]!='\0';j++) 
    {  
     printf("\n Iteration : %d",j);  
     dup[j] = data[j]; 
     //printf("%d",dup[j]); 
     if(data[j]=='.') 
     {   

      dup[j]=data[j]; 
      printf("\ndup %d = %c",j,dup[j]); 
      //printf("%d",dup[j]); 

      dup[j+1]=data[j+1]; 
      printf("\ndup %d = %c",j+1,dup[j+1]); 
      lower = data[j+2]; 
      upper = toupper(lower); 
      dup[j+2]=upper; 
      printf("\ndup %d = %c",j+2,upper); 
      printf("\nInside"); 
      for(i=j+3;data[i]!='\0';i++) 
      { 
       dup[i]=data[i]; 
       printf("\ndup %d = %c",i,dup[i]); 
      } 
      dup[i]='\0'; 
      if(lower >= 'a' && lower <= 'z') 
      { 
       upper = ('A' + lower - 'a'); 
       dup[j+2]=upper; 
       flag = 1; 
       printf("\n Capital character is : %c ",dup[j+2]); 
       printf("\n\nReplaced String is :%s\n\n",dup); 
      } 
      else 
      { 
      upper = lower; 
      dup[j+2]=upper; 
      printf("\n Capital character is : %c ",dup[j+2]); 
      printf("\n\nReplaced String is :%s\n\n",dup); 
      flag = 1; 
      } 
}  

     if(flag==1){break;} 
    } 
    return dup; 
} 

/*---------------output------------------ 
[email protected]:~$ flex sample.l 
[email protected]:~$ gcc lex.yy.c -ll 
sample.l: In function ‘replace’: 
sample.l:94:2: warning: function returns address of local variable [-Wreturn-local-addr] 
} 
^
[email protected]:~$ ./a.out hi.txt 
First Character should be : T 
Original String : fullstop. i 
Iteration : 0 
Iteration : 1 
Iteration : 2 
Iteration : 3 
Iteration : 4 
Iteration : 5 
Iteration : 6 
Iteration : 7 
Iteration : 8 
dup 8 = . 
dup 9 = 
dup 10 = I 
Inside 
Capital character is : I 

Replaced String is :fullstop. I 


Original String : wow. it 
Iteration : 0 
Iteration : 1 
Iteration : 2 
Iteration : 3 
dup 3 = . 
dup 4 = 
dup 5 = I 
Inside 
dup 6 = t 
Capital character is : I 

Replaced String is :wow. It 

[email protected]:~$ 
-----------------------------------------------*/ 
Verwandte Themen