[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Grammar question



> I've had the same problem, but haven't got an elegant solution. As far as
> I know, the 'at-most-once' information can only be dealt with semantically
> (i.e. by a separate walk of the tree).


Weeding invalid construct after parsing, as Roger suggests, is the best approach 
for dealing with such constructs.

> word = {a} A
>      | {b} B
>      | {c} C ;
> 
> Then, you would write a walker something like
> 


Here's my preferred pseudocode:

class Weeder extends DepthFirstAdaptor
{
   private boolean a,b,c;

   public void caseAAWord(AAWord node)
   {
     if(a)
       throw new RuntimeException("more than one A");
       // or better, you declare your own runtime-exception type.
     a = true;
   }

   // same for B and C
}

now, in your "main", after parsing, you do something like:

   ast.apply(new Weeder);

Hope this helps.

Etienne
-- 
Etienne M. Gagnon                    http://www.info.uqam.ca/~egagnon/
SableVM:                                       http://www.sablevm.org/
SableCC:                                       http://www.sablecc.org/