Lex program to check if characters other than alphabets occur in a string
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 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
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:



