create a recursive decent parse that read ( 1 + 2 ) / 3 from a txt file import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.StringTokenizer;
create a recursive decent parse that read ( 1 + 2 ) / 3 from a txt file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Lexical {
public static void main(String args[]) throws FileNotFoundException{
//reading file (Create one and put the expression (2+5/t) or (1-9)*0)
Scanner input = new Scanner(new File("Expression.txt"));
while(input.hasNextLine()) {
//reading next line
String line = input.nextLine();
//reading tokens by space
StringTokenizer strings = new StringTokenizer(line," ");
//reading more strings
while (strings.hasMoreElements()) {
//converting object to string
String token = strings.nextElement().toString();
switch(token) //switch case
{
case "(" :
System.out.println("Next token is: 25 Next lexeme is (");
break;
case ")" :
System.out.println("Next token is: 26 Next lexeme is )");
break;
case "=" :
System.out.println("Next token is: 20 Next lexeme is =");
break;
case "+" :
System.out.println("Next token is: 21 Next lexeme is +");
break;
case "-" :
System.out.println("Next token is: 22 Next lexeme is -");
break;
case "*" :
System.out.println("Next token is: 23 Next lexeme is *");
break;
case "/" :
System.out.println("Next token is: 24 Next lexeme is /");
break;
default :
System.out.println("Next token is: 11 Next lexeme is "+token);
}
}
//finally end of the String
System.out.println("Next token is: -1 Next lexeme is EOF");
}
}
}
Step by step
Solved in 2 steps