AST Fixes
This commit is contained in:
parent
a8f08725aa
commit
4ab003b2c6
@ -53,8 +53,7 @@ public class SynAn implements AutoCloseable {
|
||||
this.attrLoc = attrLoc;
|
||||
final AST.Nodes<AST.MainDef> defs = parseProgram();
|
||||
if (lexAn.peekToken().symbol() != Token.Symbol.EOF)
|
||||
Report.warning(lexAn.peekToken(),
|
||||
"Unexpected text '" + lexAn.peekToken().lexeme() + "...' at the end of the program.");
|
||||
Report.warning(lexAn.peekToken(), "Unexpected text '" + lexAn.peekToken().lexeme() + "...' at the end of the program.");
|
||||
return defs;
|
||||
}
|
||||
|
||||
@ -98,11 +97,11 @@ public class SynAn implements AutoCloseable {
|
||||
// definition -> VAR IDENTIFIER ASSIGN initializers
|
||||
Token var = check(Token.Symbol.VAR);
|
||||
Token id = check(Token.Symbol.IDENTIFIER);
|
||||
check(Token.Symbol.ASSIGN);
|
||||
Token assign = check(Token.Symbol.ASSIGN);
|
||||
List<AST.Init> inits = parseInitializers();
|
||||
|
||||
AST.VarDef varDef = new AST.VarDef(id.lexeme(), inits);
|
||||
attrLoc.put(varDef, new Report.Location(var, attrLoc.get(inits.getLast())));
|
||||
attrLoc.put(varDef, inits.isEmpty() ? new Report.Location(var, assign) : new Report.Location(var, attrLoc.get(inits.getLast())));
|
||||
return varDef;
|
||||
}
|
||||
|
||||
@ -118,7 +117,7 @@ public class SynAn implements AutoCloseable {
|
||||
check(Token.Symbol.ASSIGN);
|
||||
return parseStatements();
|
||||
}
|
||||
|
||||
|
||||
// funcassign -> ε
|
||||
|
||||
return new ArrayList<>();
|
||||
@ -318,10 +317,7 @@ public class SynAn implements AutoCloseable {
|
||||
attrLoc.put(init, new Report.Location(value, attrLoc.get(multConst)));
|
||||
return init;
|
||||
}
|
||||
AST.Init init = new AST.Init(
|
||||
new AST.AtomExpr(AST.AtomExpr.Type.INTCONST, "1"),
|
||||
intConst
|
||||
);
|
||||
AST.Init init = new AST.Init(new AST.AtomExpr(AST.AtomExpr.Type.INTCONST, "1"), intConst);
|
||||
attrLoc.put(init, value);
|
||||
return init;
|
||||
}
|
||||
@ -331,10 +327,7 @@ public class SynAn implements AutoCloseable {
|
||||
Token value = check(Token.Symbol.CHARCONST);
|
||||
AST.AtomExpr charConst = new AST.AtomExpr(AST.AtomExpr.Type.CHRCONST, value.lexeme());
|
||||
attrLoc.put(charConst, value);
|
||||
AST.Init init = new AST.Init(
|
||||
new AST.AtomExpr(AST.AtomExpr.Type.INTCONST, "1"),
|
||||
charConst
|
||||
);
|
||||
AST.Init init = new AST.Init(new AST.AtomExpr(AST.AtomExpr.Type.INTCONST, "1"), charConst);
|
||||
attrLoc.put(init, value);
|
||||
return init;
|
||||
}
|
||||
@ -344,10 +337,7 @@ public class SynAn implements AutoCloseable {
|
||||
Token value = check(Token.Symbol.STRINGCONST);
|
||||
AST.AtomExpr strConst = new AST.AtomExpr(AST.AtomExpr.Type.STRCONST, value.lexeme());
|
||||
attrLoc.put(strConst, value);
|
||||
AST.Init init = new AST.Init(
|
||||
new AST.AtomExpr(AST.AtomExpr.Type.INTCONST, "1"),
|
||||
strConst
|
||||
);
|
||||
AST.Init init = new AST.Init(new AST.AtomExpr(AST.AtomExpr.Type.INTCONST, "1"), strConst);
|
||||
attrLoc.put(init, value);
|
||||
return init;
|
||||
}
|
||||
@ -495,7 +485,7 @@ public class SynAn implements AutoCloseable {
|
||||
// restcmp -> NEQ addexpr
|
||||
check(Token.Symbol.NEQ);
|
||||
AST.Expr right = parseAdditionExpression();
|
||||
AST.BinExpr binExpr = new AST.BinExpr(AST.BinExpr.Oper.EQU, left, right);
|
||||
AST.BinExpr binExpr = new AST.BinExpr(AST.BinExpr.Oper.NEQ, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return binExpr;
|
||||
}
|
||||
@ -543,10 +533,10 @@ public class SynAn implements AutoCloseable {
|
||||
|
||||
// addexpr -> multexpr restadd
|
||||
AST.Expr left = parseMultiplicationExpression();
|
||||
return parseRestAdditions(left);
|
||||
return parseRestAdditionExpressions(left);
|
||||
}
|
||||
|
||||
private AST.Expr parseRestAdditions(AST.Expr left) {
|
||||
private AST.Expr parseRestAdditionExpressions(AST.Expr left) {
|
||||
Token token = lexAn.peekToken();
|
||||
switch (token.symbol()) {
|
||||
case ADD: {
|
||||
@ -555,15 +545,15 @@ public class SynAn implements AutoCloseable {
|
||||
AST.Expr right = parseMultiplicationExpression();
|
||||
AST.Expr binExpr = new AST.BinExpr(AST.BinExpr.Oper.ADD, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return parseRestAdditions(binExpr);
|
||||
return parseRestAdditionExpressions(binExpr);
|
||||
}
|
||||
case SUB: {
|
||||
// restadd -> SUN multexpr restadd
|
||||
// restadd -> SUB multexpr restadd
|
||||
check(Token.Symbol.SUB);
|
||||
AST.Expr right = parseMultiplicationExpression();
|
||||
AST.Expr binExpr = new AST.BinExpr(AST.BinExpr.Oper.SUB, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return parseRestAdditions(binExpr);
|
||||
return parseRestAdditionExpressions(binExpr);
|
||||
}
|
||||
|
||||
default:
|
||||
@ -586,7 +576,7 @@ public class SynAn implements AutoCloseable {
|
||||
case MUL: {
|
||||
// restmult -> MUL prefixexpr restmult
|
||||
check(Token.Symbol.MUL);
|
||||
AST.Expr right = parseMultiplicationExpression();
|
||||
AST.Expr right = parsePrefixExpression();
|
||||
AST.Expr binExpr = new AST.BinExpr(AST.BinExpr.Oper.MUL, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return parseRestMultiplicationExpressions(binExpr);
|
||||
@ -594,7 +584,7 @@ public class SynAn implements AutoCloseable {
|
||||
case DIV: {
|
||||
// restmult -> DIV prefixexpr restmult
|
||||
check(Token.Symbol.DIV);
|
||||
AST.Expr right = parseMultiplicationExpression();
|
||||
AST.Expr right = parsePrefixExpression();
|
||||
AST.Expr binExpr = new AST.BinExpr(AST.BinExpr.Oper.DIV, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return parseRestMultiplicationExpressions(binExpr);
|
||||
@ -602,7 +592,7 @@ public class SynAn implements AutoCloseable {
|
||||
case MOD: {
|
||||
// restmult -> MOD prefixexpr restmult
|
||||
check(Token.Symbol.MOD);
|
||||
AST.Expr right = parseMultiplicationExpression();
|
||||
AST.Expr right = parsePrefixExpression();
|
||||
AST.Expr binExpr = new AST.BinExpr(AST.BinExpr.Oper.MOD, left, right);
|
||||
attrLoc.put(binExpr, new Report.Location(attrLoc.get(left), attrLoc.get(right)));
|
||||
return parseRestMultiplicationExpressions(binExpr);
|
||||
@ -684,21 +674,27 @@ public class SynAn implements AutoCloseable {
|
||||
case IDENTIFIER:
|
||||
// primary -> IDENTIFIER exprargs
|
||||
Token id = check(Token.Symbol.IDENTIFIER);
|
||||
List<AST.Expr> args = parseExpressionArguments();
|
||||
if (args != null) {
|
||||
if (lexAn.peekToken().symbol() == Token.Symbol.LPAREN) {
|
||||
// exprargs -> LPAREN arguments RPAREN
|
||||
check(Token.Symbol.LPAREN);
|
||||
List<AST.Expr> args = parseArguments();
|
||||
Token rParen = check(Token.Symbol.RPAREN);
|
||||
|
||||
AST.CallExpr callExpr = new AST.CallExpr(id.lexeme(), args);
|
||||
attrLoc.put(callExpr, new Report.Location(id, attrLoc.get(args.getLast())));
|
||||
attrLoc.put(callExpr, new Report.Location(id, rParen));
|
||||
return callExpr;
|
||||
}
|
||||
|
||||
AST.VarExpr varExpr = new AST.VarExpr(id.lexeme());
|
||||
attrLoc.put(varExpr, id);
|
||||
return varExpr;
|
||||
|
||||
case LPAREN:
|
||||
// primary -> LPAREN expression RPAREN
|
||||
check(Token.Symbol.LPAREN);
|
||||
Token lParen = check(Token.Symbol.LPAREN);
|
||||
AST.Expr expr = parseExpression();
|
||||
check(Token.Symbol.RPAREN);
|
||||
Token rParen = check(Token.Symbol.RPAREN);
|
||||
attrLoc.put(expr, new Report.Location(lParen, rParen));
|
||||
return expr;
|
||||
|
||||
case INTCONST:
|
||||
@ -712,19 +708,6 @@ public class SynAn implements AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
private List<AST.Expr> parseExpressionArguments() {
|
||||
if (lexAn.peekToken().symbol() == Token.Symbol.LPAREN) {
|
||||
// exprargs -> LPAREN arguments RPAREN
|
||||
check(Token.Symbol.LPAREN);
|
||||
List<AST.Expr> args = parseArguments();
|
||||
check(Token.Symbol.RPAREN);
|
||||
return args;
|
||||
}
|
||||
|
||||
// exprargs -> ε
|
||||
return null;
|
||||
}
|
||||
|
||||
private List<AST.Expr> parseArguments() {
|
||||
List<AST.Expr> args = new ArrayList<>();
|
||||
|
||||
@ -761,10 +744,8 @@ public class SynAn implements AutoCloseable {
|
||||
System.out.println("This is PINS'25 compiler (syntax analysis):");
|
||||
|
||||
try {
|
||||
if (cmdLineArgs.length == 0)
|
||||
throw new Report.Error("No source file specified in the command line.");
|
||||
if (cmdLineArgs.length > 1)
|
||||
Report.warning("Unused arguments in the command line.");
|
||||
if (cmdLineArgs.length == 0) throw new Report.Error("No source file specified in the command line.");
|
||||
if (cmdLineArgs.length > 1) Report.warning("Unused arguments in the command line.");
|
||||
|
||||
try (SynAn synAn = new SynAn(cmdLineArgs[0])) {
|
||||
synAn.parse(new HashMap<>());
|
||||
|
Loading…
x
Reference in New Issue
Block a user