Created
November 11, 2017 09:35
-
-
Save 2iw31Zhv/fb004dc2f77f5821707f71a5dc31b8c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parse function for each non-terminal with error recovery. | |
* TODO: add error recovery! | |
* | |
* @param symbol the non-terminal to be parsed. | |
* @return the parsed value of `symbol` if parsing succeeded, otherwise `null`. | |
*/ | |
private SemValue parse(int symbol, Set<Integer> follow) { | |
Pair<Integer, List<Integer>> result = query(symbol, lookahead); // get production by lookahead symbol | |
follow.addAll(followSet(symbol)); | |
if (result == null){ | |
error(); | |
while (!follow.contains(lookahead) && | |
(result = query(symbol, lookahead)) == null){ | |
lookahead = lex(); | |
} | |
if (follow.contains(lookahead)){ | |
return null; | |
} | |
} | |
int actionId = result.getKey(); | |
List<Integer> right = result.getValue(); | |
int length = right.size(); | |
SemValue[] params = new SemValue[length + 1]; | |
for (int i = 0; i < length; i++) { // parse right-hand side symbols one by one | |
int term = right.get(i); | |
params[i + 1] = isNonTerminal(term) | |
? parse(term, follow) // for non terminals: recursively parse it | |
: matchToken(term) // for terminals: match token | |
; | |
} | |
for (int i = 0; i < length; i++) { | |
if(params[i + 1] == null) | |
{ | |
return null; | |
} | |
} | |
params[0] = new SemValue(); // initialize return value | |
act(actionId, params); // do user-defined action | |
return params[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment