Fix parser issue

This commit is contained in:
Arne Keller 2021-02-21 10:32:34 +01:00
parent c115baa4d4
commit 1dc5f861c3
2 changed files with 23 additions and 2 deletions

View File

@ -19,7 +19,7 @@ import java.util.Set;
* *
* @see LambdaTerm * @see LambdaTerm
*/ */
public class LambdaParser { // TODO: document syntax above ^ ? public class LambdaParser {
/** /**
* lexer to translate a String into tokens * lexer to translate a String into tokens
*/ */
@ -152,10 +152,13 @@ public class LambdaParser { // TODO: document syntax above ^ ?
if (left.isError()) { if (left.isError()) {
return left; return left;
} }
while (ATOM_START_TOKENS.contains(token.getType()) || token.getType() == TokenType.LAMBDA) { while (ATOM_START_TOKENS.contains(token.getType())
|| token.getType() == TokenType.LAMBDA || token.getType() == TokenType.LET) {
Result<LambdaTerm, ParseError> atom; Result<LambdaTerm, ParseError> atom;
if (token.getType() == TokenType.LAMBDA) { if (token.getType() == TokenType.LAMBDA) {
atom = new Result<>(parseAbstraction()); atom = new Result<>(parseAbstraction());
} else if (token.getType() == TokenType.LET) {
atom = new Result<>(parseLet());
} else { } else {
atom = parseAtom(); atom = parseAtom();
} }

View File

@ -10,6 +10,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
class LambdaParserTest { class LambdaParserTest {
private static final VarTerm X = new VarTerm("x"); private static final VarTerm X = new VarTerm("x");
private static final VarTerm Y = new VarTerm("y");
@Test @Test
void varTerm() { void varTerm() {
@ -155,6 +156,23 @@ class LambdaParserTest {
assertEquals(new AppTerm(new AbsTerm(X, X), new AbsTerm(X, X)), term.unwrap()); assertEquals(new AppTerm(new AbsTerm(X, X), new AbsTerm(X, X)), term.unwrap());
} }
@Test
void bugFoundByJohanna() {
// original term: (λx.λy.y (x y)) (λz. λa. z g a) let f = λx. let g = λy. y in g x in f 3
// reduced:
LambdaParser parser = new LambdaParser("(λx.x) let id = λy.y in id");
Result<LambdaTerm, ParseError> term = parser.parse();
if (term.isError()) {
System.err.println(term.unwrapError());
System.err.println(term.unwrapError().getCause());
}
assertEquals(new AppTerm(
new AbsTerm(X, X),
new LetTerm(new VarTerm("id"), new AbsTerm(Y, Y), new VarTerm("id"))
),
term.unwrap());
}
@Test @Test
void complicatedIdentity() { void complicatedIdentity() {
LambdaParser parser = new LambdaParser("(λx. x) (λx. x) λx. x"); LambdaParser parser = new LambdaParser("(λx. x) (λx. x) λx. x");