[06 - Top Down Parser] Fix 3 tests

- program00BeginEnd
- program01BeginEndMore
- program02BeginEnd2
This commit is contained in:
2020-05-13 10:34:49 +02:00
parent 18b2c44318
commit 0b2696efea
29 changed files with 317 additions and 996 deletions

View File

@@ -12,15 +12,163 @@
package de.dhbw.compiler.xparser;
public class XParser {
private final TokenReader in;
public XParser(TokenReader in) {
//TODO Initialization
this.in = in;
}
public Tree parseProgram() {
//TODO Parser
int oldPosition = in.getPosition();
Tree program, id, semicolon, block, dot, eof;
if ((program = parseToken(Token.PROGRAM)) != null
&& ((id = parseToken(Token.ID)) != null)
&& ((semicolon = parseToken(Token.SEMICOLON)) != null)
&& ((block = parseBlock()) != null)
&& ((dot = parseToken(Token.DOT)) != null)
&& ((eof = parseToken(Token.EOF)) != null)) {
Tree tree = new Tree(new Token(Token.APROGRAM));
tree.addLastChild(program);
tree.addLastChild(id);
tree.addLastChild(semicolon);
tree.addLastChild(block);
tree.addLastChild(dot);
tree.addLastChild(eof);
return tree;
}
System.out.println("> Skipping Tree");
in.setPosition(oldPosition);
return null;
}
private Tree parseBlock() {
int oldPosition = in.getPosition();
Tree begin, statList, end;
if ((begin = parseToken(Token.BEGIN)) != null
&& (statList = parseStatList()) != null
&& (end = parseToken(Token.END)) != null) {
Tree tree = new Tree(new Token(Token.BLOCK));
tree.addFirstChild(begin);
tree.addLastChild(statList);
tree.addLastChild(end);
return tree;
}
System.out.println("parseBlock> Skipping Tree");
in.setPosition(oldPosition);
return null;
}
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;
}
private Tree parseStatWithSemicolon() {
int oldPosition = in.getPosition();
Tree tree = new Tree(new Token(Token.STATWITHSEMI));
Tree state, semicolon;
if (((state = parseStat()) != null)
&& ((semicolon = parseToken(Token.SEMICOLON)) != null)) {
tree.addLastChild(state);
tree.addLastChild(semicolon);
return tree;
}
in.setPosition(oldPosition);
return null;
}
private Tree parseStat() {
int oldPosition = in.getPosition();
Tree tree = new Tree(new Token(Token.STAT));
Tree numAssign, condStat, block;
if ((numAssign = parseNumAssign()) != null) {
tree.addLastChild(numAssign);
return tree;
}
in.setPosition(oldPosition);
if ((condStat = parseCondStat()) != null) {
tree.addLastChild(condStat);
return tree;
}
in.setPosition(oldPosition);
if ((block = parseBlock()) != null) {
tree.addLastChild(block);
return tree;
}
in.setPosition(oldPosition);
return null;
}
private Tree parseCondStat() {
return null;
}
private Tree parseNumAssign() {
int oldPosition = in.getPosition();
Tree tree = new Tree(new Token(Token.ASSIGNSTAT));
Tree id, assign, numExpr;
if ((id = parseToken(Token.ID)) != null
&& (assign = parseToken(Token.ASSIGN)) != null
&& (numExpr = parseNumExpr()) != null) {
tree.addLastChild(id);
tree.addLastChild(assign);
tree.addLastChild(numExpr);
return tree;
}
in.setPosition(oldPosition);
return null;
}
private Tree parseNumExpr() {
int oldPosition = in.getPosition();
Tree tree = new Tree(new Token(Token.EXPR));
return null;
}
private Tree parseToken(int tokenType) {
int oldPosition = in.getPosition();
Token nextToken = in.nextToken();
System.out.println("parseToken> " + nextToken);
if (nextToken.getType() == tokenType) {
return new Tree(nextToken);
} else {
// Reset to last position as it needs to be re-read again
in.setPosition(oldPosition);
return null;
}
}
}

View File

@@ -12,7 +12,7 @@ package de.dhbw.compiler.xparser;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Scanner;
public class XParserMain {
@@ -75,7 +75,12 @@ public class XParserMain {
//TODO Initialize scanner and parser
//TODO Call parser
//TODO Output: Tree, count of tokens and comparisons
StringReader reader = new StringReader(BEISPIELFOLIEN);
JFlexXScanner scanner = new JFlexXScanner(reader);
TokenReader tokenReader = new TokenReader(scanner);
XParser parser = new XParser(tokenReader);
parser.parseProgram();
}
}