开发者

ANTLR: how to extract error messages when build fails

开发者 https://www.devze.com 2023-02-22 22:02 出处:网络
I\'ve built my lexer and parser in ANTLR and they work really well in the sense that when user code fails to parse, it outputs useful error messages to STDERR, showing the exact line no. etc.

I've built my lexer and parser in ANTLR and they work really well in the sense that when user code fails to parse, it outputs useful error messages to STDERR, showing the exact line no. etc.

The problem is, I need to extract this information in order to display the error messages in my Eclipse editor at the correct positions, but it doesn't seem to be available anywhere except on STDERR. I'm basically looking for some kind of myParser.getErrorMessages().

Has anybody come across a solution to this?

I found the below link, however this only works if the user code partially parses (i.e. so we still get an AST). When it fails completely, you don't get a tree back. http://tech.puredanger.com/2007/02/01/recovering-line-and-column-numbers-in-your-antlr-ast/

I also found this exact question in the official ANTLR FAQ... but I really don't understand his solution. Can anybody translate it for me? I'm not using any of the classes he refers to, and he's talking about v4 (which isn't released yet). http://www.antlr.org/wiki/display/ANTLR3/Pattern+for+returning+errors+from+ANTLR+in+data+structures%2C+not+STDERR

My code looks as follows:

FileInputStream fis = new FileInputStream("UserCode.txt");
ANTLRInputStream input = new ANTLRInputStream(fis);
MyLexer lexer = new MyLexer(input);
CommonTokenStream tokens = new CommonTokenStre开发者_StackOverflow中文版am(lexer);
MyParser parser = new MyParser(tokens);
CommonTree tree = (CommonTree)parser.flow().getTree();
MyAST ast = new MyAST(tree);


See: http://www.antlr.org/wiki/display/ANTLR3/Error+reporting+and+recovery (not sure if the examples are fully compatible with ANTLR v3.2/v3.3, but if not, there shouldn't be too many changes to get it working)

0

精彩评论

暂无评论...
验证码 换一张
取 消