Question

Write a Lexical analyzer (in C) to identify: parenthesis, exponents, multiplication,division, addition, subtraction, assignment ("==") and...

Write a Lexical analyzer (in C) to identify: parenthesis, exponents, multiplication,division, addition, subtraction, assignment ("==") and inequality ("!=").

Please make sure to use C language.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Lexical analysis is done by compiler. It is the first phase. The compiler coverts the progrsm into a sequence of tokens. We know that token is the smallest unit in a program such as variables, keywords, constants, special characters etc. Lexical analyzer removes the white space characters and seperates each tokens from the program without including commented statements. And it checks for the error and returns the row and column number of the error. Check for the following program:

#include<stdio.h>

#include<conio.h>

void main()

{

char str[2]; // is a character array which can store only two //characters.. This for reading operator from the user.

printf("\n Enter the operator:") ;

gets(str);

/*reading operator, if the operator is a single operator like +,-,* then it stores in str[0] and for double character like ==, != the second character stores in str[1] position.*/

switch(str[0]) //checking case value, str[0] retrieves the first //character as operator

{

case '(': // case checking for open bracket

printf("\n Open parathesis");//if it is true print the statement

break;

case ')': //case checking for closed bracket, if it is true, then //print the statement

printf("\n Closed parathesis");

break;

case '^': //checking for exponents

printf("\nExponents"); // if case operator true, then print the //statement

break;

case '*': //checking for *

printf("\nMultiplication");

break

case '/': //checking for division operator

printf("\nDivision");

break;

case '+': // checking for addition operator

printf("\nAddition");

break;

case '-': //checking for subtraction

printf("\n Subtraction");

break; // exiting from switch

case '=': // checking for =

if(str[1]=='=')

/* checking for ==. Here, str[0] stores the first = and str[1] stores the second =. If it is true then print ==*/

printf("\nEqual to equal to");

else. //otherwise only =

printf("\nEqual to or Assignment");

break;

case '!': //check for !=. If str[0]is eqal to ! and str[1] is equal to = //then prints not eqal to otherwise equal to.

if(str[1]=='=')

printf("Not eqal to");

else

printf("eual to");

break; // exiting from switch.

default: // if enter an operator other than the operators in case //values execute the below statemebt

printf("Invalid operator");

break;

}

getch();

}

Add a comment
Know the answer?
Add Answer to:
Write a Lexical analyzer (in C) to identify: parenthesis, exponents, multiplication,division, addition, subtraction, assignment ("==") and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT