Lex program to check whether the input is digit or not
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; } |
chevron_right
filter_none
Output:
Recommended Posts:
- Lex program to check whether input number is odd or even
- Lex program to take input from file and remove multiple spaces, lines and tabs
- Lex Program to print the total characters, white spaces, tabs in the given input file
- Lex program to check whether a given number is even or odd
- Lex program to check whether given string is Palindrome or Not
- Lex program to check if a Date is valid or not
- C program to check syntax of 'for' loop
- Lex program to check perfect numbers
- Lex Program to check whether a number is Prime or Not
- Lex Program to check valid email
- Lex program to check valid Mobile Number
- YACC program to check whether given string is Palindrome or not
- Lex program to check whether an year is a leap year or not
- Lex program to check whether given number is armstrong number or not
- Input Buffering in Compiler Design
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.