Question
Edit the following code: This is a complete parser that implements recursive decent parsing, that executes with a scanner. In Java, please add to the
Edit the following code: This is a complete parser that implements recursive decent parsing, that executes with a scanner. In Java, please add to the main method " Test.java" a function to instead take a file as input in order to parse the contents of that file as opposed to taking in user input from the command line. The text file will be a .lua file. A sample of the output is provided as well as all of the classes. Please provide your output as proof of the working program.
Parser.java
import java.io.*;
/* This program illustrates recursive descent parsing using a
pure procedural approach.
The grammar:
statement = { expression ";" } "."
expression = term { ( "+" | "-" ) term }
term = factor { ( "*" | "/" ) factor }
factor = number | "(" expression ")"
*/
public class Parser {
private Scanner scanner;
public Parser(Scanner scanner) {
this.scanner = scanner;
} // Parser
public void run ( ) {
scanner.getToken( );
statement( );
} // run
private void statement ( ) {
// statement = { expression ";" } "."
while(scanner.token != Token.period) {
int value = expression( );
System.out.println("=> " + value);
scanner.getToken( ); // flush ";"
} // while
} // statement
private int expression ( ) {
// expression = term { ( "+" | "-" ) term }
int left = term( );
while (scanner.token == Token.plusop ||
scanner.token == Token.minusop) {
int saveToken = scanner.token;
scanner.getToken( );
switch (saveToken) {
case Token.plusop:
left += term( );
break;
case Token.minusop:
left -= term( );
break;
} // switch
} // while
return left;
} // expression
private int term ( ) {
// term = factor { ( "*" | "/" ) factor }
int left = factor( );
while (scanner.token == Token.timesop ||
scanner.token == Token.divideop) {
int saveToken = scanner.token;
scanner.getToken( );
switch (saveToken) {
case Token.timesop:
left *= factor( );
break;
case Token.divideop:
left /= factor( );
break;
} // switch
} // while
return left;
} // term
private int factor ( ) {
// factor = number | "(" expression ")"
int value = 0;
switch (scanner.token) {
case Token.number:
value = scanner.number( );
scanner.getToken( ); // flush number
break;
case Token.lparen:
scanner.getToken( );
value = expression( );
if (scanner.token != Token.rparen)
scanner.error("Missing ')'");
scanner.getToken( ); // flush ")"
break;
default:
scanner.error("Expecting number or (");
break;
} // switch
return value;
} // factor
} // class Parser
Scanner.java
import java.io.*;
public class Scanner {
private char ch = ' ';
private char ident = ' ';
private int intValue = 0;
private Buffer buffer;
public int token;
public Scanner (DataInputStream in) {
buffer = new Buffer(in);
token = Token.semicolon;
} // Scanner
public int getToken ( ) {
while (Character.isWhitespace(ch))
ch = buffer.get( );
if (Character.isLetter(ch)) {
ident = Character.toLowerCase(ch);
ch = buffer.get( );
token = Token.letter;
}
else if (Character.isDigit(ch)) {
intValue = getNumber( );
token = Token.number;
}
else {
switch (ch) {
case ';' : ch = buffer.get( );
token = Token.semicolon;
break;
case '.' : ch = buffer.get( );
token = Token.period;
break;
case '+' : ch = buffer.get( );
token = Token.plusop;
break;
case '-' : ch = buffer.get( );
token = Token.minusop;
break;
case '*' : ch = buffer.get( );
token = Token.timesop;
break;
case '/' : ch = buffer.get( );
token = Token.divideop;
break;
case '=' : ch = buffer.get( );
token = Token.assignop;
break;
case '(' : ch = buffer.get( );
token = Token.lparen;
break;
case ')' : ch = buffer.get( );
token = Token.rparen;
break;
default : error ("Illegal character " + ch );
break;
} // switch
} // if
return token;
} // getToken
public int number ( ) {
return intValue;
} // number
public char letter ( ) {
return ident;
} // letter
public void match (int which) {
token = getToken( );
if (token != which) {
error("Invalid token " + Token.toString(token) +
"-- expecting " + Token.toString(which));
System.exit(1);
} // if
} // match
public void error (String msg) {
System.err.println(msg);
System.exit(1);
} // error
private int getNumber ( ) {
int rslt = 0;
do {
rslt = rslt * 10 + Character.digit(ch, 10);
ch = buffer.get( );
} while (Character.isDigit(ch));
return rslt;
} // getNumber
} // Scanner
class Buffer {
private String line = "";
private int column = 0;
private int lineNo = 0;
private DataInputStream in;
public Buffer (DataInputStream in) {
this.in = in;
} // Buffer
public char get ( ) {
column++;
if (column >= line.length()) {
try {
line = in.readLine( );
} catch (Exception e) {
System.err.println("Invalid read operation");
System.exit(1);
} // try
if (line == null)
System.exit(0);
column = 0;
lineNo++;
System.out.println(line);
line = line + " ";
} // if column
return line.charAt(column);
} // get
Token.java
class Token {
public static final int semicolon = 0;
public static final int period = 1;
public static final int plusop = 2;
public static final int minusop = 3;
public static final int timesop = 4;
public static final int divideop = 5;
public static final int assignop = 6;
public static final int lparen = 7;
public static final int rparen = 8;
public static final int letter = 9;
public static final int number = 10;
private static String[] spelling = {
";", ".", "+", "-", "*", "/", "=", "(", ")",
"letter", "number"};
public static String toString (int i) {
if (i < 0 || i > number)
return "";
return spelling[i];
} // toString
} // Token
Test.java
import java.io.*;
public class Test {
public static void main(String args[]) {
// command args ignored
Parser parser = new Parser(new Scanner(
new DataInputStream(System.in)));
parser.run( );
System.out.println("done");
} // main
} // class Test
} // class Buffer
If input is: (sum + 47) / total Output should be similar to the following:
Next token is: 25 Next lexeme is ( EnterEnter Enter
Next token is: 11 Next lexeme is sum EnterEnter Enter
Next token is: 21 Next lexeme is + ExitExit Next token is: 10 Next lexeme is 47 Enter
EnterNext token is: 26 Next lexeme is ) Exit Exit Exit Next token is: 24 Next lexeme is / Exit Next token is: 11 Next lexeme is total Enter Next token is: -1 Next lexeme is EOF Exit Exit Exit
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started