Question: Fix a predictive parser in Java: My code is given as follow, but please help me fix it so that it can process an input
Fix a predictive parser in Java:
My code is given as follow, but please help me fix it so that it can process an input file and output in preorder form!!!
The output is always 'null* null* null*', I can't find why!! Please anyone help me!
And Incorrect Syntax is also needed to be judged!!!
//TreeNode class
package com.expressionTree;
public class TreeNode {
private String data;
private TreeNode left;
private TreeNode right;
public TreeNode(){
this(null);
}
public TreeNode(String newData){
this.data=newData;
this.left=this.right=null;
}
public void setData(String data){
this.data=data;
}
public String getData(){
return data;
}
public void setLeft(TreeNode left){
this.left=left;
}
public TreeNode getLeft(){
return left;
}
public void setRight(TreeNode right) {
this.right=right;
}
public TreeNode getRight() {
return right;
}
}
/*------------------------------------------------------------------------*/
//expressionTree class
package com.expressionTree;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class expressionParser {
private String[] exprs; // array to store expressions
private int numExprs; // number of expressions in file
final int MAX_SIZE = 100; // maximum number of expressions
private TreeNode[] exprTrees;
private int numExprTrees;// number of trees
private String[] outps; // array to store output expressions
private int numOutps; // number of output expressions
public expressionParser(){
this.exprs = new String[MAX_SIZE];
this.numExprs = 0;
this.exprTrees = new TreeNode[MAX_SIZE];
this.numExprTrees = 0;
this.outps = new String[MAX_SIZE];
this.numOutps = 0;
}
// function to read expressions from file
public void readExprs(String filename){
File f = new File(filename);
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
String line;
while((line = reader.readLine())!= null){
exprs[numExprs] = line;
numExprs++;
}
reader.close();
} catch (FileNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// function to process expressions
public void processExprs(){
for(int i = 0; i < numExprs; i++){
String[] expression = new String[100];
int numTerms = 0;
String term = "";
String expr = exprs[i];
for(int j = 0; j < expr.length(); j++){
char c = expr.charAt(j);
if(c!=' '){
if(c=='+'||c=='-'||c=='*'||c=='/'||Character.isLetter(c)){
if(!term.equals("")){
expression[numTerms] = term;
numTerms++;
term = "";
}
expression[numTerms] = String.valueOf(c);
numTerms++;
} else if(Character.isDigit(c)){
term += String.valueOf(c);
} else if (c=='+'||c=='-'||c=='*'||c=='/'&&expr.charAt(j+1)=='+'||expr.charAt(j+1)=='-'||expr.charAt(j+1)=='*'||expr.charAt(j+1)=='/'){
expression[numTerms]="Incorrect Syntax";
}
}
}
exprTrees[numExprTrees] = createExprTree(expression, 0);
numExprTrees++;
}
}
// function to create expression tree
public TreeNode createExprTree(String[] expr, int p){
TreeNode root = null;
String term = expr[p];
while(p!=expr.length - 1){
if(term.equals("+")||term.equals("-")||term.equals("*")||term.equals("/")){
root = new TreeNode(term);
root.setLeft(createExprTree(expr,p++));
root.setRight(createExprTree(expr,p++));
} else {
return new TreeNode(term);
}
}
return root;
}
// function to traverse expression tree in preorder
public String traverseExprTree(TreeNode root){
String str = null;
if(root==null){
str = "";
} else{
str += root.getData();
traverseExprTree(root.getLeft());
traverseExprTree(root.getRight());
}
return str;
}
// function to generate output
public void generateOutput(){
for(int i = 0; i < numExprTrees; i++){
TreeNode root = exprTrees[i];
outps[numOutps] = traverseExprTree(root);
numOutps++;
}
}
// function to generate output
public void outputfile(){
String outputFileName = "./output.txt";
File outf = new File(outputFileName);
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(outf));
for(int i = 0; i < numOutps; i++){
writer.write(outps[i]);
writer.newLine();
}
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
String filename="./input.txt";
expressionParser parser = new expressionParser();
parser.readExprs(filename);
parser.processExprs();
parser.generateOutput();
parser.outputfile();
}
}
Input-Output specification
Input : "input.txt" file that contains expressions - Input expressions only contain variables with a single character alphabet (like a, b, c, x, y, not like var1, length) - Input expressions may contain multiple digit number (like 150, 20, 293...) - Input expression may contains empty spaces within an expression (like 5 +10-x...) - Input expression does not contain brackets - Each expression is separated with a new line character (' ')
Output : "output.txt" file that should contain results for each expression in the input file - For a syntactically incorrect expression, print "incorrect syntax" - For a syntactically correct expression, print the syntax tree for the expression in preorder (ex. *+234)
[input.txt] x-2*y a + 35 - b 10+*5 [output.txt] -x*2y -+a35b incorrect syntax
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
