使用Lex替换文本文件中的完全停止后的第一个字符

时间:2016-09-25 05:17:58

标签: c arrays flex-lexer lex

我正在尝试实施可以检测以下情况的Flex程序:

Input.txt:

  

您好,我叫kamalakar。所以我很高兴。

在阅读上面的Input.txt文件时,我的程序应该检测到完全停止后出现简单的语法错误,并尝试纠正它:

  

您好,我叫kamalakar。所以我很高兴。

我已经检测到完全停止,需要有关如何更换它的进一步建议。

----------- Lex文件--------

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. 
}

1 个答案:

答案 0 :(得分:0)

最终我找到答案..

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------------------
kvt@ubuntu:~$ flex sample.l
kvt@ubuntu:~$ gcc lex.yy.c -ll
sample.l: In function ‘replace’:
sample.l:94:2: warning: function returns address of local variable [-Wreturn-local-addr]
 }
  ^
kvt@ubuntu:~$ ./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

kvt@ubuntu:~$ 
-----------------------------------------------*/