What is a semantic predicate in ANTLR?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ANTLR 4
For predicates in ANTLR 4, checkout these stackoverflow Q&A’s:
ANTLR 3
A semantic predicate is a way to enforce extra (semantic) rules upon grammar
actions using plain code.
There are 3 types of semantic predicates:
Example grammar
Let’s say you have a block of text consisting of only numbers separated by
comma’s, ignoring any white spaces. You would like to parse this input making
sure that the numbers are at most 3 digits “long” (at most 999). The following
grammar (
Numbers.g) would do such a thing:Testing
The grammar can be tested with the following class:
Test it by generating the lexer and parser, compiling all
.javafiles andrunning the
Mainclass:When doing so, nothing is printed to the console, which indicates that nothing
went wrong. Try changing:
into:
and do the test again: you will see an error appearing on the console right after the string
777.Semantic Predicates
This brings us to the semantic predicates. Let’s say you want to parse
numbers between 1 and 10 digits long. A rule like:
would become cumbersome. Semantic predicates can help simplify this type of rule.
1. Validating Semantic Predicates
A validating semantic predicate is nothing
more than a block of code followed by a question mark:
To solve the problem above using a validating
semantic predicate, change the
numberrule in the grammar into:The parts
{ int N = 0; }and{ N++; }are plain Java statements of whichthe first is initialized when the parser “enters” the
numberrule. The actualpredicate is:
{ N <= 10 }?, which causes the parser to throw aFailedPredicateExceptionwhenever a number is more than 10 digits long.
Test it by using the following
ANTLRStringStream:which produces no exception, while the following does thow an exception:
2. Gated Semantic Predicates
A gated semantic predicate is similar to a validating semantic predicate,
only the gated version produces a syntax error instead of a
FailedPredicateException.The syntax of a gated semantic predicate is:
To instead solve the above problem using gated predicates to match numbers up to 10 digits long you would write:
Test it again with both:
and:
and you will see the last on will throw an error.
3. Disambiguating Semantic Predicates
The final type of predicate is a disambiguating semantic predicate, which looks a bit like a validating predicate (
{boolean-expression}?), but acts more like a gated semantic predicate (no exception is thrown when the boolean expression evaluates tofalse). You can use it at the start of a rule to check some property of a rule and let the parser match said rule or not.Let’s say the example grammar creates
Numbertokens (a lexer rule instead of a parser rule) that will match numbers in the range of 0..999. Now in the parser, you’d like to make a distinction between low- and hight numbers (low: 0..500, high: 501..999). This could be done using a disambiguating semantic predicate where you inspect the token next in the stream (input.LT(1)) to check if it’s either low or high.A demo:
If you now parse the string
"123, 999, 456, 700, 89, 0", you’d see the following output: