Howdy. I'm toying with how to specify semantic checks for antlr v4. I can list
a bunch of tree patterns and then test stuff like this:
rule: ^( RULE r=ID .*)
{
if ( gtype==LEXER_GRAMMAR &&
Character.isLowerCase($r.text.charAt(0)) ) {
ErrorManager.grammarError(ErrorType.PARSER_RULES_NOT_ALLOWED,
fileName,
$r.token, $r.text);
}
if ( (gtype==PARSER_GRAMMAR||gtype==TREE_GRAMMAR) &&
Character.isUpperCase($r.text.charAt(0)) )
{
ErrorManager.grammarError(ErrorType.LEXER_RULES_NOT_ALLOWED,
fileName,
$r.token, $r.text);
}
}
;
ruleref
:
( ^((ROOT|BANG) r=RULE_REF ARG_ACTION?)
| ^(r=RULE_REF ARG_ACTION?)
)
{
if ( gtype==LEXER_GRAMMAR &&
Character.isLowerCase($r.text.charAt(0)) ) {
ErrorManager.grammarError(ErrorType.PARSER_RULES_NOT_ALLOWED,
fileName,
$r.token, $r.text);
}
}
;
I wonder if I can do something more readable. Perhaps these rules trigger
methods in my SemanticsPipeline class. Like unit tests, I could make them all
start with "rule" or "check" or "enforce" like:
checkInvalidRule(int gtype, Token ruleID) { ...stuff in grammar action at
moment... }
checkInvalidRuleRef(int gtype, Token ruleID) {...}
It would be easy to see the list of semantic rules ANTLR follows. it also
separates the rules from the tree structure.
Other option is to put error conditions in grammar with predicates and repeat
syntactic tree pattern.
What say you!?
Ter
_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org/mailman/listinfo/antlr-dev