Reputation: 366
i want to implement a parser for lambda expressions. But i get "mismatched input ' ' expecting ')' " error for that input: (\x.x x) (\x.x x) , dont know why...
I have a grammar:
grammar Lambda;
lambda_expression : VARIABLE
| '\\' VARIABLE '.' lambda_expression
| ('(' lambda_expression ')')+
| EOF
;
VARIABLE : 'x' | 'y' | 'z' | 'v' | 'w'
;
WS : (' ')+ -> channel(HIDDEN);
and this is my main class:
public static void main(String[] args) throws IOException {
// TODO code application logic here
ANTLRInputStream input = new ANTLRInputStream("(\\x.x x) (\\x.x x)");
LambdaLexer lex = new LambdaLexer(input) ;
CommonTokenStream tokens = new CommonTokenStream(lex);
LambdaParser parser = new LambdaParser(tokens);
parser.lambda_expression();
parser.setBuildParseTree(true);
LambdaParser.Lambda_expressionContext tree = parser.lambda_expression();
System.out.println(tree.toStringTree(parser));
}
I'm using antlr4-4.1-complete.jar
Upvotes: 1
Views: 951
Reputation: 6001
The recursion on lambda_expression
of alt3 (
-> alt2 \x.
-> alt1 x
matches (\x.x
, leaving the parser wanting to then complete the match of alt3 with a )
.
Changing alt2 to
| '\\' VARIABLE ( '.' lambda_expression )+ lambda_expression
might be the solution, depending on whether it actually reflects your allowed lambda syntax.
Upvotes: 1