Skip to article frontmatterSkip to article content

Parsing

Cornell University
// E → T + E | T
void parseE() throws SyntaxError {
    parseT();
    if (peek("+")) {
        advance();
        parseE();
    }
}

Similarly, the method parseT looks for “×” to decide which production to use:

// T → F × T | F
void parseT() throws SyntaxError {
    parseF();
    if (peek("×")) {
        advance();
        parseT();
    }
}

And parseF() can decide using the first symbol it sees, assuming we have an appropriate method isNumber():

// F → n | ( E )
void parseF() throws SyntaxError {
    if (isNumber(peek())) {
        advance();
    } else {
        consume("(");
        parseE();
        consume(")");
    }
}