📜  lex程序查找一个字符除了字母的字符串时发生

📅  最后修改于: 2021-05-28 05:02:36             🧑  作者: Mango

莱克斯 是生成词法分析器的计算机程序。 Lex读取指定词法分析器的输入流,并输出以C编程语言实现词法分析器的源代码。

用于执行lex程序的命令为:

lex abc.l (abc is the file name)
cc lex.yy.c 
./a.out

问题:写lex程序找到,如果从字母表除了字符上的一个给定的字符串中出现。

例子 :

Input : GeeksforGeeks is best
Output : other characters are also present
Explanation: Because ' ' space is also a character

Input : geeksforgeeks
Output : only alphabets present
Explanation: Only english alphabets are present in the string

方法 :

  1. 使用该标志检查是否还存在其他字符;
  2. 如果遇到“ \ n” ,我们将假定用户已经完全给出了字符串,现在他希望显示结果
  3. 因此,根据标志的状态,我们将显示输出。
  4. 否则,如果遇到除(az,AZ)以外的其他任何字符我们将使标志= 1,我们将通过正则表达式对其进行检查

下面是实现:

C++
/* lex code to check for characters other that 
albhabets in a given string */
  
%{
int flag = 0;
%}
  
%%
[\n] {
    flag==0?printf("Only alphabets present\n"):
    printf("Other characters are also present\n");
    flag = 0;
}
  
[^a-zA-Z] {flag = 1;}
  
. {}
%%
  
int yywrap(void) {}
  
int main(){
    yylex();
    return 0;
}


输出:

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。