Skip to content

Commit c75f8df

Browse files
committed
lecture: fix pattern matching example (ANTLR)
1 parent e40bae0 commit c75f8df

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

lecture/02-parsing/antlr-parsing.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,16 +648,23 @@ ist *Pattern Matching* über Klassen und Records so weit ausgebaut, dass man es
648648
Alternative zum Visitor-Pattern oder zu den ANTLR-Listenern nutzen kann.
649649
:::
650650

651+
``` antlr
652+
expr : e1=expr '*' e2=expr # MULT
653+
| e1=expr '+' e2=expr # ADD
654+
| DIGIT # ZAHL
655+
;
656+
```
657+
658+
\bigskip
659+
651660
``` java
652661
public static class PatternMatching {
653662
static Integer eval(calcParser.ExprContext e) {
654663
return switch (e) {
655664
case calcParser.MULTContext m -> eval(m.e1) * eval(m.e2);
656665
case calcParser.ADDContext a -> eval(a.e1) + eval(a.e2);
657666
case calcParser.ZAHLContext n -> Integer.parseInt(n.DIGIT().getText());
658-
default ->
659-
throw new IllegalStateException("Unhandled expr: "
660-
+ e.getClass().getSimpleName());
667+
default -> throw new IllegalStateException();
661668
};
662669
}
663670
}

0 commit comments

Comments
 (0)