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