1

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

Thomas
  • 366
  • 6
  • 19

1 Answers1

1

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.

GRosenberg
  • 5,843
  • 2
  • 19
  • 23
  • but i think there is an issue with the WS parsing, isn't? 'cause the lexer dont recognize it, however i defined WS – Thomas Oct 08 '15 at 07:05
  • No. The error message shows what is next in the actual input character stream, not the next token. If there is a question about what the lexer is doing, [dump the token stream](http://stackoverflow.com/questions/29197727/antlr-4-5-parser-error-during-runtime/29198883#29198883). The space token will be correctly marked as hidden. – GRosenberg Oct 08 '15 at 21:13