[antlr-interest] Trouble with syntactic predicates that reference semantic predicates that rely on a current dynamic attribute scope

Sam Harwell sharwell at pixelminegames.com
Fri Jul 25 11:51:24 PDT 2008


Sorry for the confusion.

My grammar has 100+ rules. I wrote this example quickly to illustrate
the issue. In the actual grammar, the scope access syntax is correct. (I
bet it only takes 1 guess to figure out what language I'm parsing...)

I got around the issue by adding an auto-incrementing id in scope with
the syntactic predicate, then checking in the semantic predicate to see
if the id matches a reference id in the top level scope containing the
variable I need ('a' in this example). I placed the id check in a
function called HaveValidDeclSpecifierSeqScope() so it's somewhat easy
to reference, but it's still rather "unfriendly".

Sam

-----Original Message-----
From: Terence Parr [mailto:parrt at cs.usfca.edu] 
Sent: Friday, July 25, 2008 12:32 PM
To: Sam Harwell
Cc: antlr mailing list
Subject: Re: [antlr-interest] Trouble with syntactic predicates that
reference semantic predicates that rely on a current dynamic attribute
scope

Hi Sam,

Wow!I had not thought of this. The problem is that the predicate gets  
hoisted out of rule2 and is evaluated before entry into rule2. The  
code to push the new scope is not executed. crap...yeah, there are  
general issues with predicates to get hoisted out of context... I  
cannot really look inside to see your actions to know what to do. I  
will say, however, that you are not using proper syntax to access the  
scope. it must be $rule2::a not a. In fact, I am surprised that works  
at all! is that really compile?  int a will be inside of a class.  'a'  
all by itself should not compile.
Ter

On Jul 25, 2008, at 9:33 AM, Sam Harwell wrote:

> Wow that's a big statement!
>
> Consider the following setup:
>
> rule2
> scope
> {
>     int a;
> }
> @init
> {
>     a = -1;
> }
> : { (a < 0) }? rule3 { ((a=1) >= 0) /* intentional assignment */ }?
> ;
>
> rule1
> : ((rule2) => rule2)+
> ;
>
> rule0
> : (rule1) => rule1
> | rule4
> ;
>
> In evaluating the syntactic predicate in rule0, the semantic  
> predicate at the beginning of rule2 is evaluated without pushing a  
> new dynamic attribute scope. I can detect the lack of a dynamic  
> scope (rule2_stack.Count == 0), but there is a path for rule0 to be  
> reached inside rule2, in which case the semantic predicate is  
> checking the value of a in the wrong scope. Is this a bug?
>
> Sam
>



More information about the antlr-interest mailing list