gpt4 book ai didi

c - 使用 Lex 替换文本文件中句号后的第一个字符

转载 作者:行者123 更新时间:2023-11-30 16:57:06 27 4
gpt4 key购买 nike

我正在尝试实现可以​​检测以下场景的 Flex 程序:

输入.txt:

Hi my name is kamalakar. so I'm happy.

在阅读上面的 Input.txt 文件时,我的程序应该检测到句号后存在简单的语法错误,并尝试纠正它,如下所示:

Hi my name is kamalakar. So I'm happy.

我已经检测到句号,需要有关如何替换它的进一步建议。

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

最佳答案

最终我找到了答案..

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:~$
-----------------------------------------------*/

关于c - 使用 Lex 替换文本文件中句号后的第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39683650/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com