StackOverflow error from recursion, in project 10 implementation

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

StackOverflow error from recursion, in project 10 implementation

Obij
    Hello please could someone give me some guidance or pointers on my issue. i am currently doing project 10 and have finished the first part (building the tokenizer) and I am trying to build the compilation engine module. but i am having problems visualizing how the various methods work. For example, from the lecture slides for project 10, on page 38 (titled: the parsing process), a compileExpression method started running, and printed an output to the output file, then while the compile expression method was still running, it called the compileTerm() method, which did some processing and also wrote to the output file. Next, after some processing and printing to the output file, by the still running compile expression method, it again, called the compile Term() method, which processed and wrote another output to the output file. The compile expression, then did some final writing to the output file, before finally exiting.
     i have tried to understand how to realize this behaviour codewise and i was thinking that maybe (especially since recursion was mentioned heavily in the described implementation), that the delayed behaviour of the compile expression and similar-acting methods (for the project) could be due to the method calling itself repeatedly, thus not exiting, until it does a final return at some point (at which it must have executed all of its intended behaviour.) To test this out, I did a java mini-implementation of a few of the basic methods of the compilation engine class (most of the methods had if/else statements  and recursively called themselves, but there was always an if/else branch with a return statement included in one of their if/else branch), tested it out on a two line input file that had some very simple jack code. It initially gave me a null pointer error code, I debugged and decided that it might be from trying to simultaneously read and write complex statements into the output file. So i changed the output file to an arraylist and tested again. This time, it gave me a stack over flow error, which from googling, I found that it tends to occur from deep recursions. I don't know whether someone could suggest to me, how exactly the behaviour should be implemented.
     i have also included my buggy code below, in case anyone who might help, is also conversant with java and thus if seeing  my code, would further clarify my issue to him/her. Thanks, in anticipation.

package testcompileengine;
import java.io.*;
import java.util.*;


/**
 *
 * @author OBINNA
 */
public class TestCompileEngine {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException{
        // TODO code application logic here
        TestCompileEngine compile= new TestCompileEngine("C:\\Users\\OBINNA\\Documents\\TestCompilerT2.xml");
        compile.advance();
        System.out.println(strList);
    }
   
    //declare some local variables
    private String currentToken;
    private String lineText;
    private BufferedReader br;
    private BufferedWriter output;
    private boolean equalsPassed= false;
    private static List<String> strList= new ArrayList<String>();
   
    /** constructor
     *
     */
    public TestCompileEngine(String tokenFile) throws IOException{
        File file= new File(tokenFile);
        FileReader fr= new FileReader(file);
        br= new BufferedReader(fr);
        //create html file for writing into
        //int pointIndex= tokenFile.indexOf(".");
        //String initialString= tokenFile.substring(0, pointIndex +1);
        //String fileName= initialString + "html";
        //now create a file with fileName
        //File file2= new File(fileName);
        //BufferedWriter output= new BufferedWriter(new FileWriter(file2));
    }
   
    /** Utility method that checks if an item is a term
     *
     */
    public boolean isATerm(String currentToken){
        boolean aTerm= false;
        List<String> keyWordConstants= new ArrayList<>(Arrays.asList("true", "false", "null", "this"));
       
        //extract the token itself from currentToken
        int angledBracket2Index= currentToken.indexOf(">");
        int angledBracket3Index= currentToken.indexOf("<", angledBracket2Index);
        String token= currentToken.substring(angledBracket2Index +1, angledBracket3Index);
        token= token.trim();
       
        if(currentToken.contains("integerConstant") || currentToken.contains("stringConstant") || currentToken.contains("identifier") || currentToken.contains(".") || currentToken.contains("~") || currentToken.contains("-")){
            aTerm= true;
            return aTerm;
        }
       
        for(String keyWord: keyWordConstants){
            if(token.equals(keyWord)){
                aTerm= true;
                return aTerm;
            }
        }
       
        return aTerm;
    }
   
    /** the compileTerm method
     *
     */
    public void compileTerm() throws IOException{
        if(this.isATerm(currentToken)){
            lineText= currentToken;
            strList.add("<term>");
            //output.newLine();
            strList.add(lineText);
            //output.newLi();
            strList.add("</term>");
            //.newLine();
        }
    }
   
    /** method compiles expressions
     *
     */
    public void compileExpression() throws IOException{
        if(currentToken.contains("(") || currentToken.contains("=")){
            lineText= currentToken;
            strList.add(lineText);
            strList.add("<expression>");
            this.compileExpression();
           
        }else if(this.isAnOp(currentToken)){
            lineText= currentToken;
            strList.add(lineText);
            //output.newLine();
            this.compileExpression();
           
        }else if(currentToken.contains(")") || currentToken.contains(";")){
            lineText= currentToken;
            strList.add("</expression>");
            strList.add(lineText);
            //output.newLine();
            return;
           
        }else if(currentToken.contains("identifier")){
            this.compileTerm();
            this.compileExpression();
        }
    }
   
    /** Utility method that checks if current token contains an op
     *
     */
    public boolean isAnOp(String currentToken){
        boolean anOp= false;
        List<String> opList= new ArrayList<>(Arrays.asList("+", "-", "*", "/", "&", "<", "|", ">", "="));
       
        //extract the token itself from currentToken
        int angledBracket2Index= currentToken.indexOf(">");
        int angledBracket3Index= currentToken.indexOf("<", angledBracket2Index);
        String token= currentToken.substring(angledBracket2Index +1, angledBracket3Index);
        token= token.trim();
       
        // iterate through the ArrayList and check appropriately
        for (String symbol: opList){
            if(token.equals(symbol)){
                anOp= true;
                return anOp;
            }
        }
        return anOp;
    }
   
    /** method that compiles the while block
     *
     */
    public void compileWhile() throws IOException{
        if(currentToken.contains("while")){
            //System.out.println(currentToken);
            lineText= currentToken;
            strList.add("<whileStatement>");
            //output.newLine();
            strList.add(lineText);
            this.compileWhile();
           
        }else if(currentToken.contains("identifier") && this.isATerm(currentToken)){
            //System.out.println(currentToken);
            this.compileTerm();
            this.compileWhile();
           
        }else if(currentToken.contains("(")){
            //System.out.println(currentToken.contains("("));
            //System.out.println(currentToken);
            this.compileExpression();
            this.compileWhile();
           
        }else if(currentToken.contains("{")){
            //System.out.println(currentToken);
            lineText= currentToken;
            strList.add("</whileStatement");
            //utput.newLine();
            strList.add(lineText);
            //output.newLine();
            return;
        }
    }
   
    /** method compiles the let block
     *
     */
    public void compileLet() throws IOException{
        if(currentToken.contains("let")){
            lineText= currentToken;
            strList.add("<letStatement>");
            //output.newLine();
            strList.add(lineText);
            //output.newLine();
            this.compileLet();
           
        }else if(currentToken.contains("identifier") && (equalsPassed= false)){
            lineText= currentToken;
            strList.add(lineText);
            //output.newLine();
            equalsPassed= true;
           
        }else if(currentToken.contains("identifier")){
            this.compileExpression();
            equalsPassed= false;
            return;
        }
       
    }
   
    /** method that advances the reading of the input file
     *
     */
    public void advance()throws IOException{
        while((currentToken= br.readLine()) != null){
            if(currentToken.contains("tokens")){
                continue;
            }
            System.out.println(currentToken);
            this.compileWhile();
            this.compileLet();
        }
        br.close();
        //output.close();
    }
       
   
   
}