antlr4-c3
antlr4-c3 copied to clipboard
Semantic Predicate not working
Hello,
it seems that semantic predicates are not handled.
Given the following grammar with semantic predicates
grammar Expr;
@parser::header {
export function isSummable(ID: string) {
// symbol table checks here.. now using a mock logic
return ID.startsWith('x');
}
}
expression
: summableIdentifier '+' NUM
| subtractableIdentifier '-' NUM
;
summableIdentifier: {isSummable(this.currentToken.text)}? ID;
subtractableIdentifier: {!isSummable(this.currentToken.text)}? ID;
ID: [a-z];
NUM: [0-9]+;
WHITESPACE
: [ \t\r\n] -> skip
;
during prediction this.currentToken
is <EOF>
so all checks are not working.
I think that prediction algorithm should walk the parsed tree until caretTokenIndex is reached, and only then determine follow sets, but maybe I'm totally wrong.
Thanks so much for this awesome library.
@mike-lischke if you have an hint on how to fix this, maybe I can help and try to make a pull request
Well, without debugging I cannot say for sure, but I suspect that this.currentToken.text
is not available in the predicate code (remember this is just using eval()
to execute things). You cannot use ANTLR runtime code in your action file, only what's passed in.
Yes, you're right, during predicate.eval()
context is not available.
I followed this official tutorial https://github.com/antlr/antlr4/blob/master/doc/predicates.md#finding-visible-predicates where it suggests to use getCurrentToken()
(should be equal to this.currentToken
in antlr4ts
).
Also a more complex case like context dependant predicates fails.
Do you know if is it possible to get current token during predicion? Thanks
The problem seems to be that the parser's token stream is at the end (EOF), which is correct because the text was already parsed to the end.
If you seek the input stream to the corresponding token (parser.tokenStream.seek(index)
before transition.predicate.eval
) the predicate get's the correct token.