[antlr-interest] Semantik Predicates

Zachary Palmer zep_antlr at bahj.com
Sun Oct 24 08:37:08 PDT 2010


Dagi,

If you're using backtracking, it seems that you've run into the same 
problem that I encountered yesterday.  The following link is related:

http://www.antlr.org/wiki/display/ANTLR3/Action+execution+during+backtracking 


It would seem that actions are not executed during backtracking, which 
means that this C++ routine of yours is not going to be evaluated.  This 
further means that the backtracker will not correctly predict which path 
to explore, since your predicate depends on the output of the action.

Of course, ANTLR *does* execute the semantic predicates during 
backtracking.  One possible way to try to hack around this would be to 
write your rule as follows:

rule: {C++ function invocation that returns whether or not an adjective 
is recognized}? ID ;

But it seems that you need the side-effect of setting 
$sentence::adjective_recognized and semantic predicates are 
contractually forbidden from having side effects.

My personal feeling is that ANTLR should have some syntax describing 
whether or not a given action should be run during backtracking or not.  
The actions in my grammar have no side effects (with the exception of 
setting a variable which is returned by the rule), but ANTLR is assuming 
that it's an action which cannot be undone and therefore not running it 
during backtracking.  Sadly, I'm afraid this doesn't address your 
situation.

Is the set of recognized adjectives dynamic for a given document or is 
it static per-document and dynamic for the language in general?  If so, 
might you be able to write a specialized lexer and then use ANTLR's 
parser normally?

Best of luck,

Zach
> Hi,
>
> I have a problem with using semantic predicates in my grammar. I have certain rules to identify simple, basic sentences of a natural language (English). In addition I want to restrict the language to a set of keywords. Those keywords are adaptively generated from an external c++ routine, meaning I cannot put them statically into my ANTLR code. Therefore, within one rule of my grammar, I'm comparing the actual token to an array of keywords. If a keyword is found, I'm setting an ANTLR variable to true. At the end of the rule it shall be evaluted if this varible is true and if so, the token shall be accepted. If the variable is false, it mustn't be accepted.
>
> Here is the sample:
> rule:    {C++ routine assigning value to $sentence::adjective_recognized}
>             {$sentence::adjective_recognized}? ID ;
>
> In my case $sentence::adjective_recognized is set to true when an adjective is found that is a keyword. The problem that I have is that the routine to set this variable is not evaluated at the beginning of the parsing. I have to initialize this variable to true in order to make the parser accept the whole sentence. If I'm initializing this variable to false the parser cannot map the input sentence to the grammar. I'm supposing the parser is trying to map the input sentence (look ahead)  in advance before actually going through all the rule actions? Is there a way to tell the parser to evaluate the variable i.e. my routine every time? Thank you very much for your help.
>
> Yours, Dagi
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>    



More information about the antlr-interest mailing list