0

I am writing a recursive descent parser for parsing a specific grammar, and I have to do the nested if/else in that grammar.

The grammar is as follows:

<statement> ::= if <cond> <statement> 

In the parseStmt we have to do the following: basically the getToken() method will remove blanks and grab the token.

// if <cond> <statement>
else if (token.equals("if")) {
    token = getToken();
    cond1 = (parseCond(token));
    cond = (cond1) && (cond);

    token = getToken();
    execute = (cond) && (execute);
    parseStmt(cond, token);
    token = getToken();

    if (token.equals("else")) {
        if (cond) {
            execute = false;
            token = getToken();
            parseStmt(cond, token);
        } else {
            execute = (cond) && (execute);
            token = getToken();
            parseStmt(cond, token);
            cond = true;
            execute = true;
            token = getToken();
        }
    } else {
        line = token + line;
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • 1
    What is the question here? – fish Apr 08 '20 at 14:29
  • The use of the globals `cond` and `execute` suggests that you are trying to immediately evaluate, rather than just parsing. But immediate evaluation isn't very scalable. You won't be able to handle loops, for example. So uou might want to rethink your design. – rici Apr 08 '20 at 15:31
  • The question is how can i write the nested if else keeping track of true and false – Harshini Aitharaju Apr 09 '20 at 17:04

0 Answers0