Lex program to recognize valid arithmetic expression and identify the identifiers and operators
Problem: Write a Lex program to recognize valid arithmetic expression and identify the identifiers and operators.
Explanation:
Flex (Fast lexical Analyzer Generator) is a tool/computer program for generating lexical analyzers (scanners or lexers) written by Vern Paxson in C around 1987. Lex reads an input stream specifying the lexical analyzer and outputs source code implementing the lexer in the C programming language. The function yylex() is the main flex function which runs the Rule Section.
Examples:
Input: a+b*c
Output: valid expression
the operators are :
+
*
the identifiers are :
a
b
c
Input: a+b-
Output: invalid expression
Input: (a*b)
Output: valid expression
the operators are :
*
the identifiers are :
a
b
Input: (a+b-
Output: invalid expression
Implementation:
/* Lex program to recognize valid arithmetic expression and identify the identifiers and operators */% { /* Definition section */#include <stdio.h> #include <string.h> int noprt = 0, nopnd = 0, valid = 1, top = -1, l = 0, j = 0; char opnd[10][10], oprt[10][10], a[100]; % } /* Rule Section */% % "("{ top++; a[top] = '('; } "{"{ top++; a[top] = '{'; } "["{ top++; a[top] = '['; } ")"{ if (a[top] != '(') { valid = 0; return; } else top--; } "}"{ if (a[top] != '{') { valid = 0; return; } else top--; } "]"{ if (a[top] != '[') { valid = 0; return; } else top--; } "+" | "-" | "*" | "/"{ noprt++; strcpy(oprt[l], yytext); l++; } [0 - 9] + | [a - zA - Z][a - zA - Z0 - 9_]* { nopnd++; strcpy(opnd[j], yytext); j++; } % % // driver code main() { int k; printf("Enter the arithmetic expression: "); yylex(); if (valid == 1 && top == -1 && (nopnd - noprt) == 1) { printf("valid expression\n"); printf("the operators are :\n"); for (k = 0; k < l; k++) printf("%s \n", oprt[k]); printf("the identifiers are :\n"); for (k = 0; k < j; k++) printf("%s \n", opnd[k]); } else printf("invalid expression\n"); } int yywrap() { return 1; } |
Output:

Recommended Posts:
- YACC program to implement a Calculator and recognize a valid Arithmetic expression
- Yacc Program to evaluate a given arithmetic expression
- YACC program to recognize strings of { anb | n≥5 }
- YACC program to recognize string with grammar { anbn | n≥0 }
- Lex program to identify the identifier
- Lex Program to Identify and Count Positive and Negative Numbers
- Lex Program For checking a valid URL
- Lex Program to check valid email
- Lex program to check if a Date is valid or not
- Lex Program to accept a valid integer and float value
- Lex program to check valid Mobile Number
- YACC program for Conversion of Infix to Postfix expression
- Compiler Design | Transition diagram for Identifiers
- Regular Expression Vs Context Free Grammar
- Lex Program to remove comments from C program
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.



