Pretty syntax analysis errors

This commit is contained in:
Gašper Dobrovoljc
2025-03-21 16:16:45 +01:00
parent a94802b571
commit b8545a143b
6 changed files with 61 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
/**
* Implementacija programskega jezika PINS'25.
*
*
* @author bostjan.slivnik@fri.uni-lj.si
*/
module pins25 {

View File

@@ -130,7 +130,7 @@ public class LexAn implements AutoCloseable {
Report.Location start = currentLocation();
switch (buffChar) {
case -1: // EOF
buffToken = new Token(new Report.Location(0, 0), Token.Symbol.EOF, null);
buffToken = new Token(start, Token.Symbol.EOF, null);
return;
case '\'':

View File

@@ -91,7 +91,7 @@ public class SynAn implements AutoCloseable {
break;
default:
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected FUN or VAR.");
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected definition.");
}
}
@@ -198,25 +198,37 @@ public class SynAn implements AutoCloseable {
check(Token.Symbol.END);
break;
default:
case IDENTIFIER:
case LPAREN:
case ADD:
case SUB:
case NOT:
case PTR:
case INTCONST:
case CHARCONST:
case STRINGCONST:
System.out.println("statement -> expression exprassign");
parseExpression();
parseExpressionAssign();
break;
default:
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected statement.");
}
}
private void parseRestStatementDefinitions() {
switch (lexAn.peekToken().symbol()) {
case IN:
System.out.println("reststmtdefs -> ε");
break;
case FUN:
case VAR:
System.out.println("reststmtdefs -> definition reststmtdefs");
parseDefinition();
parseRestStatementDefinitions();
break;
case IN:
System.out.println("reststmtdefs -> ε");
break;
}
}
@@ -256,7 +268,7 @@ public class SynAn implements AutoCloseable {
break;
default:
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected integer, character or string constant.");
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected constant.");
}
}
@@ -302,12 +314,32 @@ public class SynAn implements AutoCloseable {
break;
default:
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected integer, character or string constant.");
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected constant.");
}
}
private void checkExpression() {
Token token = lexAn.peekToken();
switch (token.symbol()) {
case IDENTIFIER:
case LPAREN:
case ADD:
case SUB:
case NOT:
case PTR:
case INTCONST:
case CHARCONST:
case STRINGCONST:
return;
default:
throw new Report.Error(token, "Unexpected symbol '" + token.lexeme() + "', expected expression.");
}
}
private void parseExpression() {
checkExpression();
System.out.println("expression -> conjexpr restdisj");
parseConjunctionExpression();
parseRestDisjunctions();
@@ -323,6 +355,8 @@ public class SynAn implements AutoCloseable {
}
private void parseConjunctionExpression() {
checkExpression();
System.out.println("conjexpr -> cmpexpr restconj");
parseComparisonExpression();
parseRestConjunctionExpressions();
@@ -340,6 +374,8 @@ public class SynAn implements AutoCloseable {
}
private void parseComparisonExpression() {
checkExpression();
System.out.println("cmpexpr -> addexpr restcmp");
parseAdditionExpression();
parseRestComparisons();
@@ -366,6 +402,8 @@ public class SynAn implements AutoCloseable {
}
private void parseAdditionExpression() {
checkExpression();
System.out.println("addexpr -> multexpr restadd");
parseMultiplicationExpression();
parseRestAdditions();
@@ -389,6 +427,8 @@ public class SynAn implements AutoCloseable {
}
private void parseMultiplicationExpression() {
checkExpression();
System.out.println("multexpr -> prefixexpr restmult");
parsePrefixExpression();
parseRestMultiplicationExpressions();
@@ -413,6 +453,8 @@ public class SynAn implements AutoCloseable {
}
private void parsePrefixExpression() {
checkExpression();
Token token = lexAn.peekToken();
switch (token.symbol()) {
case ADD:
@@ -432,6 +474,8 @@ public class SynAn implements AutoCloseable {
}
private void parsePostfixExpression() {
checkExpression();
System.out.println("postfixexpr -> primary postfixop");
parsePrimary();
parsePostfixOperator();