📌  相关文章
📜  用于检查字符串是否出现字母以外的字符的Lex 程序

📅  最后修改于: 2022-05-13 01:57:14.073000             🧑  作者: Mango

用于检查字符串是否出现字母以外的字符的Lex 程序

问题:-编写一个 Lex 程序来查找除字母以外的字符是否出现在字符串

说明:- Flex(快速词法分析器生成器)是一种工具或计算机程序,它生成由 Vern Paxson 于 1987 年左右在 C 中编写的词法分析器(扫描器或词法分析器)。Lex 读取指定词法分析器的输入流并输出实现词法分析器的源代码在 C 编程语言中。

函数yylex() 是运行规则部分的主要 flex函数。

例子。

Input: abcd
Output: No character other than alphabets
Expnation: Since we have alphabets ranging from a to z that's why the output is "no character other than alphabets"
 
Input: abcd54
Output: character other than alphabets present
Explanation: since 5 and 4 are not alphabets
 
Input: abc@bdg
Output: character other than alphabets present
Explanation: since @ is not a alphabet

下面是实施方案:

C
%{ 
  int len=0; 
%} 
  
// Rules to identify if a character apart from alphabets
// occurs in a string
  
%% 
[a-zA-Z]+ {printf("No character other than alphabets");}
  
/* here . will match any other character than alphabets
 because alphabets are already matched above
 * will matches 0 or more characters in front of it.
*/
  
.* {printf("character other than alphabets present"); }
%% 
   
// code section 
int yywrap() { } 
    
int main() 
 {  
  yylex(); 
  return 0; 
 }


输出: