开发者

Where should I initialize variables for an OO Recursive Descent Parse Tree?

I'd like to prefa开发者_Go百科ce this by stating that this is for a class, so please don't solve this for me.

One of my labs for my cse class is creating an interpreter for a BNF that was provided. I understand most of the concepts, but I'm trying to build up my tree and I'm unsure where to initialize values. I've tried in both the constructor, and in the methods but Eclipse's debugger still only shows the left branch, even though it runs through completely.

Here is my main procedure so you can get an idea of how I'm calling the methods.

public class Parser {

public static void main(String[] args) throws IOException {

    FileTokenizer instance = FileTokenizer.Instance();
    FileTokenizer.main(args);

    Prog prog = new Prog();

    prog.ParseProg();

    prog.PrintProg();

    prog.ExecProg();
}

Now here is My Prog class:

public class Prog {
private DeclSeq ds;
private StmtSeq ss;

Prog() {
    ds = new DeclSeq();
    ss = new StmtSeq();
}

public void ParseProg() {
    FileTokenizer instance = FileTokenizer.Instance();
    instance.skipToken(); //Skips program (1)
//  ds = new DeclSeq();
    ds.ParseDS();
    instance.skipToken(); //Skips begin (2)
//  ss = new StmtSeq();
    ss.ParseSS();
    instance.skipToken();
}

I've tried having

Prog() {
    ds = null;
    ss = null;
}

public void ParseProg() {
    FileTokenizer instance = FileTokenizer.Instance();
    instance.skipToken(); //Skips program (1)
    ds = new DeclSeq();
    ds.ParseDS();
    ...

But it gave me the same error. I need the parse tree built up so I can do a pretty print and an execute command, but like I said, I only get the left branch.

Any help would be appreciated. Explanations why are even more so appreciated.

Thank you, Vasto


Turns out that my issue was in DeclSeq and StmtSeq.

I was declaring variables inside a while loop, thereby losing them after the loop exited. DOH

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜