Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like 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).

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.


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


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. */
/* 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
        return 0;


My Personal Notes arrow_drop_up
Recommended Articles
Page :