208 lines
4.7 KiB
Java
208 lines
4.7 KiB
Java
/* **********************************************
|
|
* Duale Hochschule Baden-Württemberg Karlsruhe
|
|
* Prof. Dr. Jörn Eisenbiegler
|
|
*
|
|
* Vorlesung Übersetzerbau
|
|
* Praxis X Abstiegsparser
|
|
* - Abstiegsparser
|
|
*
|
|
* **********************************************
|
|
*/
|
|
|
|
package de.dhbw.compiler.xparser;
|
|
|
|
public class XParser {
|
|
private final TokenReader in;
|
|
|
|
public XParser(TokenReader in) {
|
|
this.in = in;
|
|
}
|
|
|
|
public Tree parseProgram() {
|
|
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));
|
|
Tree intConst;
|
|
|
|
if ((intConst = parseNumExpr2()) != null) {
|
|
tree.addLastChild(intConst);
|
|
return tree;
|
|
}
|
|
|
|
in.setPosition(oldPosition);
|
|
return null;
|
|
}
|
|
|
|
private Tree parseNumExpr2() {
|
|
int oldPosition = in.getPosition();
|
|
Tree tree = new Tree(new Token(Token.EXPR2));
|
|
Tree intConst;
|
|
|
|
if ((intConst = parseNumExpr3()) != null) {
|
|
tree.addLastChild(intConst);
|
|
return tree;
|
|
}
|
|
|
|
in.setPosition(oldPosition);
|
|
return null;
|
|
}
|
|
|
|
private Tree parseNumExpr3() {
|
|
int oldPosition = in.getPosition();
|
|
Tree tree = new Tree(new Token(Token.EXPR3));
|
|
Tree intConst;
|
|
|
|
if ((intConst = parseToken(Token.INTCONST)) != null) {
|
|
tree.addLastChild(intConst);
|
|
return tree;
|
|
}
|
|
|
|
in.setPosition(oldPosition);
|
|
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;
|
|
}
|
|
}
|
|
|
|
}
|