[antlr-interest] problem with tree parsing -> don't get a tree walker to work correctly
jimi at temporal-wave.com
Mon Apr 16 11:12:09 PDT 2012
Without looking at your code, I suggest that it looks like this:
LBRACKET (e=expr (COMMA e=expr)*)? RBRACKET
But should look like this:
LBRACKET (e+=expr (COMMA e+=expr)*)? RBRACKET
And e will be a List type (no generics) so any code should expect that.
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Peter Götz
> Sent: Monday, April 16, 2012 10:36 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] problem with tree parsing -> don't get a tree
> walker to work correctly
> 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,
More information about the antlr-interest