How to implement parser for a grammar in Java

Viewed 39

I have defined a grammar and now I'm implementing a parser for it.

The program should start with the keyword main followed by an opening curly bracket, followed in turn by a (possibly empty) sequence of statements, and terminated by a closing curly bracket. My questions is how to define the program in the parser? I have tried several different ways, including this, but it doesn't seem to be correct when I test.

public void program() {
        //Program -> MAIN LCBR Statement* RCBR
        eat("MAIN");
        eat("LCBR");
       while (lex.token().type != "RCBR") {
            statement();
       }
    }

Any suggestions would be appreciated!

0 Answers
Related