27 March 2000 Release 2.22 Notes for New Users of PCCTS Version 1.33MR22
26
#135.
There's no simple way to avoid evaluation of a semantic predicate for validation after use in prediction
#136.
What is the "context" of a semantic predicate ?
Answer due to TJP: The context of a predicate is the set of
k
-strings (comprised of lookahead symbols) that can be
matched following the execution of a predicate. For example,
a : <<p>>? alpha ;
The context of "p" is Look(alpha) where Look(alpha) is the set of lookahead
k
-strings for alpha.
class_name: <<isClass(LT(1)->getText())>>? ID ;
The context of
<<isClass ...>>?
is ID for
k
=1. Only
k
=1 is used since only LT(1) is referenced in the
semantic predicate. It is important to use "prc on" for proper operation. The old notation:
class_name: <<LT(1)==ID ? isClass(LT(1)->getText()) : 1>>? ID ;
/* Obsolete notation incompatiable with -prc on */
shouldn't be used for new grammars - it is not compatible with "prc on". The only reason "prc on" is not the
default is backward compatibility.
Here is an example that won't work because it doesn't have context check in the predicates:
a : ( class_name | Num )
| type_name
;
class_name : <<isClass(LT(1)->getText())>>? ID ;
type_name : <<isType(LT(1)->getText())>>? ID ;
The prediction for production one of rule "a" will be:
if ( LT(1) in { ID, Num } && isClass(LT(1)->getText())) {...}
Clearly, Num will never satisfy isClass(), so the production will never match.
When you ask
ANTLR
to compute context, it can check for missing predicates. With prc on, for this grammar:
a : b
| <<isVar(LT(1)->getText())>>? ID
| <<isPositive(LT(1)->getText()>>? Num
;
b : <<isType(LT(1)->getText())>>? ID
| Num
;
ANTLR
reports:
warning alt 1 of rule itself has no predicate to resolve
ambiguity upon { Num }
#137.
Use
ANTLR
option "-info p" for information on how semantic predicates are being handled and hoisted
When the user selects option "-info p" the program will generate detailed information about predicates. If the user
selects "-mrhoist on" additional detail will be provided explaining the promotion and suppression of predicates. The
output is part of the generated file and sandwiched between #if 0/#endif statements.
Consider the following k=1 grammar:
start : ( all ) * ; /* 3 */
all : a /* 4 */
| b /* 5 */
; /* 6 */
a : c B ; /* 7 */
c : <<pc(LT(1))>>? /* 8 */
| B /* 9 */
; /* 10 */
b : <<pb(LT(1))>>? X ; /* 11 */
Below is an excerpt of the output for rule "start" when used with -mrhoist on and -info p.