[06 - Top Down Parser] Fix "program*" in TestXminTopDownParser.class

This commit is contained in:
2020-05-13 12:14:24 +02:00
parent ecd5c8c915
commit bc72602f89
2 changed files with 53 additions and 11 deletions

View File

@@ -65,23 +65,12 @@ public class XParser {
}
private Tree parseStatList() {
// int oldPosition = in.getPosition();
Tree tree = new Tree(new Token(Token.STATLIST));
Tree stat;
while ((stat = parseStatWithSemicolon()) != null) {
tree.addLastChild(stat);
}
// if ((block = parseBlock()) != null) {
// tree.addFirstChild(new Tree(new Token(Token.STATWITHSEMI)));
// tree.addFirstChild(block);
// tree.addLastChild(new Tree(new Token(Token.SEMICOLON)));
//
// return tree;
// }
// in.setPosition(oldPosition);
return tree;
}
@@ -128,6 +117,59 @@ public class XParser {
}
private Tree parseCondStat() {
int oldPosition = in.getPosition();
Tree tree = new Tree(new Token(Token.CONDSTAT));
Tree ifKey, cond, then, stat1, elseKey, stat2;
if ((ifKey = parseToken(Token.IF)) != null
&& (cond = parseCond()) != null
&& (then = parseToken(Token.THEN)) != null
&& (stat1 = parseStat()) != null
&& (elseKey = parseToken(Token.ELSE)) != null
&& (stat2 = parseStat()) != null) {
tree.addLastChild(ifKey);
tree.addLastChild(cond);
tree.addLastChild(then);
tree.addLastChild(stat1);
tree.addLastChild(elseKey);
tree.addLastChild(stat2);
return tree;
}
in.setPosition(oldPosition);
if (((ifKey = parseToken(Token.IF)) != null)
&& (cond = parseCond()) != null
&& (then = parseToken(Token.THEN)) != null
&& (stat1 = parseStat()) != null) {
tree.addLastChild(ifKey);
tree.addLastChild(cond);
tree.addLastChild(then);
tree.addLastChild(stat1);
return tree;
}
in.setPosition(oldPosition);
return null;
}
private Tree parseCond() {
int oldPosition = in.getPosition();
Tree tree = new Tree(new Token(Token.COND));
Tree a, operator, b;
if ((a = parseNumExpr()) != null
&& ((operator = parseToken(Token.EQUALS)) != null
|| (operator = parseToken(Token.LESS)) != null
|| (operator = parseToken(Token.MORE)) != null)
&& (b = parseNumExpr()) != null) {
tree.addLastChild(a);
tree.addLastChild(operator);
tree.addLastChild(b);
return tree;
}
in.setPosition(oldPosition);
return null;
}