In this project, you will implement a simple lexical analyzer to scan a small se
ID: 3799871 • 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
import java.util.*;
//Class Expression definition
public class Expression
{
//Main method
public static void main(String ss[])
{
//Scanner class object created to accept data
Scanner sc = new Scanner(System.in);
System.out.println("Enter a expression");
//Accept an expression
String exp = sc.nextLine();
//To store identifiers
String strID = "ID:";
//To store integers
String strINT = "INT:";
//To store frequency of left parenthesis, right parenthesis, addition and multiplication symbol
int lp, rp, add, mul;
//Initially all are zero
lp = rp = add = mul = 0;
//Loop till end of the expression
for(int c = 0; c < exp.length(); c++)
{
//Extracts the character
char ch = exp.charAt(c);
//If the character is a digit
if(Character.isDigit(ch))
{
//Extract all the characters till right parenthesis or space
do
{
//Concatenate the current character with strINT
strINT += exp.charAt(c);
//Increase the counter
c++;
//Checks for the space if true break
if(exp.charAt(c) == ' ')
break;
//Checks for the right parenthesis if true break
else if(exp.charAt(c) == ')')
break;
}while(true);
//Adds a comma at the end
strINT += ", ";
c--;
}//End of if
//If the character is a alphabet
else if(Character.isLetter(ch))
{
//Extract all the characters till right parenthesis or space
do
{
//Concatenate the current character with strID
strID += exp.charAt(c);
//Increase the counter
c++;
//Checks for the space if true break
if(exp.charAt(c) == ' ')
break;
//Checks for the right parenthesis if true break
else if(exp.charAt(c) == ')')
break;
}while(true);
//Adds a comma at the end
strID += ", ";
c--;
}//end of else if
//If the character is a left parenthesis
else if(ch == '(')
//Increase left parenthesis counter
lp++;
//If the character is a right parenthesis
else if(ch == ')')
//Increase right parenthesis counter
rp++;
//If the character is a addition operator
else if(ch == '+')
//Increase addition operator counter
add++;
//If the character is a multiplication operator
else if(ch == '*')
//Increase multiplication operator counter
mul++;
}//End of for loop
//Displays information
System.out.println(strID);
System.out.println(strINT);
System.out.println("The numbers of LEFT PARENTHESIS, RIGHT PARENTHESIS, ADD_OP and MUL_OP");
System.out.println(lp + ", " + rp + ", " + add + ", " + mul);
}//End of main method
}//End of class
Sample Run 1:
Enter a expression
(number1 + number2) * (22 + 10)
ID:number1, number2,
INT:22, 10,
The numbers of LEFT PARENTHESIS, RIGHT PARENTHESIS, ADD_OP and MUL_OP
2, 0, 2, 1
Sample Run 2:
Enter a expression
res = ((number1 + number2) * (val1 * 10) + 30 * val3)
ID:res, number1, number2, val1, val3,
INT:10, 30,
The numbers of LEFT PARENTHESIS, RIGHT PARENTHESIS, ADD_OP and MUL_OP
3, 3, 2, 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.