constructParseTree function
This method builds an AST of the matched tokens, removes all unneeded tokens and constructs an intermediate representation by trying to "understand" the code it is given.
Implementation
List<dynamic> constructParseTree(Map<String, dynamic> grammar,
List<String> cosmeticTokens, List<dynamic> parsedTokens) {
List<dynamic> result = [];
List<dynamic> tokens = parsedTokens;
for (int i = 0; i < tokens.length; i++) {
List<dynamic> tokenList = tokens[i];
List<String> tokenNames = [];
List<dynamic> elementList = [];
String statementType = '';
for (int y = 0; y < tokenList.length; y++) {
List<dynamic> token = tokenList[y];
tokenNames.add(token[0]);
for (int z = 0; z < grammar.length; z++) {
String key = grammar.keys.elementAt(z);
String sentence = grammar[key];
String joinedTokens = tokenNames.join(' ');
if (joinedTokens == sentence) {
statementType = key;
} else {}
}
if (cosmeticTokens.contains(token[1]) == true) {
} else {
elementList.add(token[1]);
}
}
elementList.insert(0, statementType);
result.add(elementList);
}
return result;
}