In this project, you will implement a simple lexical analyzer to scan a small se
ID: 3800546 • Letter: I
Question
In this project, you will implement a simple lexical analyzer to scan a small set of expressions. This set of expressions has the following tokens: identifier, integer constant, left parenthesis, right parenthesis, addition operator, and multiplication operator. Specifically, assume we have the following lexical definition. LETTER rightarrow a | b | ... |z|A| ... | Z DIGIT rightarrow 0 | 1 | ... | 9 We define a set of tokens as follows. ID rightarrow LETTER {LETTER | DIGIT} INT rightarrow DIGIT {DIGIT} LEFT_PARENTHESIS rightarrow (RIGHT_PARENTHESIS rightarrow) ADD_OP rightarrow ' + '| '-' MUL_OP rightarrow '+' | '/' Note that blank is a character that should be skipped by the program. Given an expression as input, your program should accomplish the following requirements: If the expression has error, for example, undefined token, unequal numbers of left parenthesis and right parenthesis, the program should output an error message. Print out all identifiers and all integer constants; Counting how many left parenthesis, right parenthesis, addition, and multiplication operators. For example, for the input expression (number1 + number2) * (number3 + 1000) your program should output ID: number1, number2, number3 INT: 1000 The numbers of LEFT PARENTHESIS, RIGHT PARENTHESIS, ADD_OP and MUL_OP; 2, 2, 2, and 1Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char operators[4]={'+','-','*','/'}; //Operators arrray
char left_parenthesis='(';
char right_parenthesis=')';
char identifier[10][10]; //to store all identifiers
char digit[10][10]; //to store all digits
char ch;
int count_Lparenthesis=0,count_Rparenthesis=0,count_add=0,count_mul=0;
int iden_i=0,iden_j=0;
int dig_i=0,dig_j=0;
int Is_identifier(char ch) //To check identifier or not
{
if(isalpha(ch)||isdigit(ch))
return 1;
else
return 0;
}
int Is_operator(char ch) //to check operator or not
{
int i;
for(i=0;i<4;i++)
{
if(ch==operators[i])
return 1;
else
return 0;
}
}
void lexical(char *str)
{
int i=0;
while(str[i]!='')
{
if(Is_identifier(str[i])) //For Identifier
{
while(Is_identifier(str[i]))
{
identifier[iden_i][iden_j++]=str[i++]; //it will store all identifers one by one
}
identifier[iden_i][iden_j]='';
iden_i++;
iden_j=0;
}
else if(isdigit(str[i])) //For Constant integer values
{
while(isdigit(str[i]))
{
digit[dig_i][dig_j++]=str[i++]; //it will store all digits one by one
}
digit[dig_i][dig_j]='';
dig_i++;
dig_j=0;
}
else if(Is_operator(str[i]))
{
if(str[i]==operators[0]||str[i]==operators[1])
count_add++; //counts the no.of '+' and '-'
else
count_mul++; //counts the no.of '*' and '/'
}
else if(str[i]==left_parenthesis)
count_Lparenthesis++; //counts the no.of Left parenthesis
else if(str[i]==right_parenthesis)
count_Rparenthesis++; //counts the no.of right parenthesis
}
}
int main()
{
int i;
char str[50]; //to store input string
printf("Enter String: ");
scanf("%[^ ]c",str);
lexical(str); //calling function
if(count_Lparenthesis!=count_Rparenthesis)
printf("No of left and right parenthesis are not equal ");
printf("Identifiers: ");
for(i=0;i<iden_i;i++)
{
puts(identifier[i]);
printf(" ");
}
printf("Digits: ");
for(i=0;i<dig_i;i++)
{
puts(digit[i]);
printf(" ");
}
printf("Count of Add_op=%d Count of mul_op=%d Count of Left parenthesis=%d Count of right parenthesis=%d",count_add,count_mul,count_Lparenthesis,count_Rparenthesis);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.