Lex Program to Find if a Character Apart from Alphabet Occurs in a String
Lex is a computer program that generates lexical analyzers. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language.
The commands for executing the lex program are:
lex abc.l (abc is the file name) cc lex.yy.c ./a.out
Problem: Write a lex program to find if a character apart from the alphabet occurs in a given string.
Example :
Input : w3wiki is best Output : other characters are also present Explanation: Because ' ' space is also a character Input : w3wiki Output : only alphabets present Explanation: Only english alphabets are present in the string
Approach :
- Using the flag to check if other characters are also present or not;
- If “\n” is encountered we will assume that the user has given the string completely and now he wants the result to be displayed
- So according to the state of flag, we will show the output.
- Else if any other character except (a-z, A-Z) is encountered we will make the flag = 1 which we are checking via Regular expression.
Below is the implementation:
C++
/* lex code to check for characters other that alphabets 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; } |
Java
import java.util.Scanner; import java.util.regex.*; public class Main { private static int flag = 0 ; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Pattern pattern = Pattern.compile( "[^a-zA-Z]" ); while (scanner.hasNextLine()) { String line = scanner.nextLine(); Matcher matcher = pattern.matcher(line); if (matcher.find()) { flag = 1 ; } if (line.matches( "[\n]" )) { if (flag == 0 ) { System.out.println( "Only alphabets present" ); } else { System.out.println( "Other characters are also present" ); } flag = 0 ; } } } } |
Javascript
let flag = 0; function yylex(input) { let match; while ((match = lexer.exec(input)) !== null ) { if (match[0] === '\n' ) { flag == 0 ? console.log( "Only alphabets present" ) : console.log( "Other characters are also present" ); flag = 0; } else if (/[^a-zA-Z]/.test(match[0])) { flag = 1; } } } const lexer = /[\n]|[^a-zA-Z]|./g; const input = "This is a sample string with $ and numbers 123" ; yylex(input); |
Python
import re # import the re module for regular expression operations flag = 0 # initialize a flag variable to track presence of non-alphabetic characters while True : # loop to take input from the user until an empty input is given input_str = input () # take input from the user if not input_str: # if input is empty, break out of the loop break elif re.search(r '[^a-zA-Z]' , input_str): # if input contains non-alphabetic characters, set flag to 1 flag = 1 else : # otherwise, set flag to 0 flag = 0 if flag = = 0 : # if flag is 0, only alphabetic characters were present in the input print ( "Only alphabets present" ) else : # otherwise, non-alphabetic characters were also present in the input print ( "Other characters are also present" ) |
C#
using System; public class Program { static int flag = 0; static void Main() { yylex(); } static int yylex() { while ( true ) { int c = Console.Read(); switch (c) { case -1: return 0; case '\n' : if (flag == 0) { Console.WriteLine( "Only alphabets present" ); } else { Console.WriteLine( "Other characters are also present" ); } flag = 0; break ; default : if (! char .IsLetter(( char )c)) { flag = 1; } break ; } } } } |
Output:
Contact Us