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:

filter_none

edit
close

play_arrow

link
brightness_4
code

/* 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;
}

chevron_right


Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.