Computer Graphics

C program to simulate lexical analyzer for validating operators

C program to simulate lexical analyzer for validating operators

This program is designed to read an operator input from the user and determine what type of operator it is. It uses a switch statement to analyze the input and identify single-character and double-character operators, such as:

  • Greater than (>) and Greater than or equal to (>=)
  • Less than (<) and Less than or equal to (<=)
  • Assignment (=) and Equality (==)
  • Logical AND (&&) and Bitwise AND (&)
  • Logical OR (||) and Bitwise OR (|)
  • Arithmetic operators like Addition (+), Subtraction (-), Multiplication (*), Division (/), and Modulus (%)
  • Also includes the NOT operator (!) and Bitwise NOT.
#include <stdio.h>
#include <conio.h>

void main()
{
char s[5]; // Array to store the operator input
clrscr(); // Clear the screen (Note: clrscr() is not standard in modern C)

printf("\n Enter any operator:");
gets(s); // Read the operator input from the user (Note: gets() is unsafe, use fgets() instead)

switch(s[0]) // Switch on the first character of the input
{
case '>':
if(s[1] == '=') // Check if the second character is '='
printf("\n Greater than or equal");
else
printf("\n Greater than");
break;
case '<':
if(s[1] == '=')
printf("\n Less than or equal");
else
printf("\n Less than");
break;
case '=':
if(s[1] == '=')
printf("\n Equal to");
else
printf("\n Assignment");
break;
case '!':
if(s[1] == '=')
printf("\n Not Equal");
else
printf("\n Bit Not");
break;
case '&':
if(s[1] == '&')
printf("\n Logical AND");
else
printf("\n Bitwise AND");
break;
case '|':
if(s[1] == '|')
printf("\n Logical OR");
else
printf("\n Bitwise OR");
break;
case '+':
printf("\n Addition");
break;
case '-':
printf("\n Subtraction");
break;
case '*':
printf("\n Multiplication");
break;
case '/':
printf("\n Division");
break;
case '%':
printf("\n Modulus");
break;
default:
printf("\n Not an operator");
}

getch(); // Wait for a key press before closing the console
}
  • Header Files:

    • #include <stdio.h>: This library is included for standard input and output functions like printf and gets.
    • #include <conio.h>: This library is used for console input and output, such as clrscr() and getch(). Note that conio.h is not part of the C standard library and may not be supported in some compilers.
  • Main Function:

    • The program defines a main() function where the execution begins.
    • It declares a character array s of size 5 to store the operator input.
  • Clear Screen:

    • clrscr() is called to clear the console screen (this function may not work in all compilers).
  • User Input:

    • The program prompts the user to enter an operator using printf.
    • gets(s) is used to read the user input into the array s. However, gets() is unsafe as it can lead to buffer overflow. It is better to use fgets() in modern C programming.
  • Switch Statement:

    • The program uses a switch statement to evaluate the first character of the input (s[0]).
    • Each case represents a different operator. If the operator requires a second character (like >=, <=, etc.), it checks the second character (s[1]).
  • Operator Identification:

    • For each case:
      • Single Character Operators: Operators like +, -, *, /, and % are directly matched.
      • Double Character Operators: Operators like >=, <=, ==, !=, &&, and || are checked by verifying s[1].
  • Default Case:

    • If the input doesn’t match any known operators, the program prints “Not an operator.”
  • Ending the Program:

    • getch() is called to pause the program until a key is pressed before it terminates.

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *