rqwuza
Last Updated: February 25, 2016
·
254
· xsoameix

Use function pointer to make parser more clear.

When every parse function want to recursively call another parse function, buildTree() will be call first.
It makes parse function more clear and more readable.

Example:

void parse_PS() {
        if(lookahead == NT) {
                buildTree(&parse_P, "P");
                buildTree(&parse_PS, "PS");
        } else if(lookahead == EOF) {
        } else {
                syntaxError();
        }
}

void buildTree(void (*parseFunc)(), char *id) {
        addChild(id);

        // Simulate a tree
        enterTree();
        (*parseFunc)();
        leaveTree();
}

Source: https://github.com/xsoameix/LL-1-table-builder/blob/develop/app/Parser.c