Question

I have a parser and an AST. Now i want a Treewalkergrammar. In the AST there is a token "=".

The Treewalker should find the "=" token in:

relationalExpression
: (numericExpression) (     (^(EQUAL relationalExpression))   
                        | (^(NOT_EQUAL relationalExpression)) 
                  )? 
;

Although the top token should be "=" or "!=", the compiler just looks into "numericExpression" and finds out it is not a "=" token. Finally it results the error "no viable alternative at input '='".

So "=" is the top token for the parser, but not for the treewalker.

What is wrong here?

Thank a lot

Was it helpful?

Solution

You probably meant to write relationalExpression in its tree form, where numericExpression is likely a child of EQUAL instead of a sibling of it.

// tree parser rule:
relationalExpression
  : ^(EQUAL numericExpression relationalExpression)
  | ^(NOT_EQUAL numericExpression relationalExpression)
  | numericExpression
  ;

This is of course assuming that your parser has a relationalExpression rule with either the following form (which uses the AST operator ^):

// parser rule:
relationalExpression
  : numericExpression
    ( EQUAL^ relationalExpression
    | NOT_EQUAL^ relationalExpression
    )?
  ;

Or the following, which uses the rewrite operator ->:

// parser rule:
relationalExpression
  : numericExpression
    ( EQUAL relationalExpression
      -> ^(EQUAL numericExpression relationalExpression)
    | NOT_EQUAL relationalExpression
      -> ^(NOT_EQUAL numericExpression relationalExpression)
    )?
  ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top