[antlr-interest] How do you use template output with backtracking on in a validating semantic predicate?

William Halliburton whalliburton at gmail.com
Mon Jul 2 00:27:21 PDT 2007


> This all sounds a little roundabout. What's the problem you're trying to solve?

I am trying to parse a c like languages and have a rule like the following.

stringComparison
    : ^('==' left=expression right=STRING_LITERAL) ->
stringEqual(left={$left.st},right={$right.text})
    | ^('!=' left=expression right=STRING_LITERAL) ->
stringNotEqual(left={$left.st},right={$right.text})
    | ^(op=relationalOperator left=expression right=STRING_LITERAL) ->
stringCompareTo(left={$left.st},right={$right.text},op={$op.st})
    ;

I would like to be able to use the text of "expression" in my semantic
predicates and the only way I see to get the text is $expression.st as
$expression.text does not seem to work for tree grammars. BUT $expression.st
is not set during backtracking.

would like a rule like:

    : ^('==' left=expression right=expression) {isString($left.st} &&
isString($right.st)}? -> stringEqual(left={$left.st},right={$right.st})

where isString checks the string of the expression against a symbol table.

Thank you,
Will
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070702/da16f598/attachment.html 


More information about the antlr-interest mailing list