Open In App

Lex program to check if characters other than alphabets occur in a string

Improve
Improve
Like Article
Like
Save
Share
Report

Problem  :- Write a Lex program to find if a character apart from alphabets occurs in a string or not

Explanation:-  Flex (Fast Lexical Analyzer Generator ) is a tool or computer program which generates lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987.Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.

The function yylex() is the main flex function which runs the Rule Section.

Examples.

Input: abcd
Output: No character other than alphabets
Explanation: 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

Below is the implementation program:

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


Output:


Last Updated : 02 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads