1 Commits

Author SHA1 Message Date
Gašper Dobrovoljc
8251a3608c Changed addition operators to right associative 2025-06-03 18:41:44 +02:00
4 changed files with 13 additions and 44 deletions

View File

@@ -39,8 +39,9 @@ restcmp -> EQU addexpr
| . | .
addexpr -> multexpr restadd . addexpr -> multexpr restadd .
restadd -> ADD multexpr restadd restadd -> ADD addexpr
| SUB multexpr restadd | . | SUB addexpr
| .
multexpr -> prefixexpr restmult . multexpr -> prefixexpr restmult .
restmult -> MUL prefixexpr restmult restmult -> MUL prefixexpr restmult
@@ -69,28 +70,4 @@ restinits -> COMMA initializer restinits | .
initializer -> INTCONST intconstmult | CHARCONST | STRINGCONST . initializer -> INTCONST intconstmult | CHARCONST | STRINGCONST .
intconstmult -> MUL const | . intconstmult -> MUL const | .
const -> INTCONST | CHARCONST | STRINGCONST . const -> INTCONST | CHARCONST | STRINGCONST .
--------------------------------------------------------------------
//LEFT ASOC.
exp0 = exp0 op other | other
|
v
exp0 = other exp0'
exp0' = op other exp0' | .
//RIGHT ASOC.
exp0 = other op exp0 | other
|
v
exp0 = other exp0'
exp0' = op exp0 | .

View File

@@ -1,5 +1,3 @@
// line1 --
line2 -----x
line3
fun main() = 0 fun main() =
3 - 2 - 1

View File

@@ -247,13 +247,7 @@ public class LexAn implements AutoCloseable {
return; return;
} }
StringBuilder builder = new StringBuilder(); while (buffChar != '\n' && buffChar != -1) {
while (buffChar != -1) {
if (buffChar == '\n' && !builder.toString().endsWith("--")) {
break;
}
builder.append((char) buffChar);
nextChar(); nextChar();
} }

View File

@@ -540,20 +540,20 @@ public class SynAn implements AutoCloseable {
Token token = lexAn.peekToken(); Token token = lexAn.peekToken();
switch (token.symbol()) { switch (token.symbol()) {
case ADD: { case ADD: {
// restadd -> ADD multexpr restadd // restadd -> ADD addexpr
check(Token.Symbol.ADD); check(Token.Symbol.ADD);
AST.Expr right = parseMultiplicationExpression(); AST.Expr right = parseAdditionExpression();
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 parseRestAdditionExpressions(binExpr); return binExpr;
} }
case SUB: { case SUB: {
// restadd -> SUB multexpr restadd // restadd -> ADD addexpr
check(Token.Symbol.SUB); check(Token.Symbol.SUB);
AST.Expr right = parseMultiplicationExpression(); AST.Expr right = parseAdditionExpression();
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 parseRestAdditionExpressions(binExpr); return binExpr;
} }
default: default: