Lex Program to Find if a Character Apart from Alphabet Occurs in a String
Last Updated :
23 Mar, 2023
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 : 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
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++
%{
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
flag = 0
while True :
input_str = input ()
if not input_str:
break
elif re.search(r '[^a-zA-Z]' , input_str):
flag = 1
else :
flag = 0
if flag = = 0 :
print ( "Only alphabets present" )
else :
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:
Share your thoughts in the comments
Please Login to comment...