[antlr-interest] problem with tree parsing -> don't get a tree walker to work correctly

Peter Götz peter.goetz at gutefrage.net
Mon Apr 16 10:36:12 PDT 2012


Hi there, 

I am quite new to ANTLR and grammar parsing in general, but I am currently trying to implement an expression parser for arithmetic expressions of the following form: "foo:bar + bar:foo". So I have operands (foo:bar and bar:foo) and operators (+, -, * and /)
Those expressions are working fine with my current grammar and tree grammar. Now I want to enhance this parser to be able to handle functions of the form "function[foo:bar, bar:foo]". The function calls should be able to handle simple operands (like above), but also arithmetic expressions themselves as their parameters. 
I have created the attached two grammar files MetricArithmeticExpression.g to parse the expression and MetricArithmeticExpressionTreeWalker.g to build up a tree of Java objects for further use in my application. 
My problem now is that I am not able to handle parameter lists to functions, but only a single parameter. The problematic line in my MetricArithmeticExpressionTreeWalker.g is line 20, where I would like to give the list of parsed "expr" results to the FunctionNode. 
I have attached the grammar files, their compiled Lexer, Parser and TreeWalker classes (incl. .tokens files) and a simple test case for you to this message to make it easier for you to see the problem yourselves. I won't add the ANTLR jar file to not blow up this message unnecessarily (and I think you all have your copy ready ;) ). 
Can you please tell me how I can motivate ANTLR to add all the parsed "expr" nodes to my function node? I know that they are being parsed correctly, because when I debug into the TreeWalker I can see that they are generated and assigned to the variable "e", but the hand-over to the function node does only take place after all the parameters have been interpreted and so only the last one comes into the function node. 
Or did I miss something else in grammar parsing and my approach is wrong? Then please point me to the correct direction. 

Thanks a lot in advance for your help, this has now cost me half of Friday and the whole of today so far and currently I don't know what to try else. 

Best regards, 

Peter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MetricArithmeticExpressions.zip
Type: application/zip
Size: 12899 bytes
Desc: MetricArithmeticExpressions.zip
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20120416/6f2f6998/attachment.zip 


More information about the antlr-interest mailing list