Skip to content
Related Articles

Related Articles

Improve Article

Lex program to check whether the input is digit or not

  • Last Updated : 30 Apr, 2019

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.

Prerequisite: Flex (Fast lexical Analyzer Generator).

Given an input, the task is to check if the input is digit or not.

Examples:

Input:  28
Output: digit

Input: a
Output: not a digit. 

Input: 21ab
Output: not a digit. 

Below is the implementation:






/* Lex program to check whether input is digit or not. */
%{
#include<stdio.h>
#include<stdlib.h>
%}
/* Rule Section */
%%
^[0-9]*  printf("digit");
^[^0-9]|[0-9]*[a-zA-Z]  printf("not a digit");
. ;
%%
int main()
{
        // The function that starts the analysis
    yylex();
        return 0;
}

Output:

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

 

My Personal Notes arrow_drop_up
Recommended Articles
Page :