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

Re: Grammar question




Todd,

> I'm trying to develop a SableCC grammar that includes
> a production of numerous "at-most-one" items whose
> order is irrelevant.

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).

You can make order irrelevant quite easily with something like:

sentance = word* ;

word = {a} A
     | {b} B
     | {c} C ;

Then, you would write a walker something like

  /*  note: this is only psuedocode!  */
  public void caseASentance (ASentance node) {

     PWord aWord, bWord, cWord;

     Object allWords[] = node.getWords().toArray();
     for(int i = 0; i < allWords.length; i++) {

       if (allWords[i] instanceof AWord && aWord == null)
           aWord = allWords[i];
       if (allWords[i] instanceof BWord && bWord == null)
           bWord = allWords[i];
       if (allWords[i] instanceof CWord && cWord == null)
           cWord = allWords[i];
     }
  }


Let me know if you come across a better solution!

Roger


> For example, using the tokens A, B, and C, all the
> following would be acceptable: ABC, CAB, C, A, BAC,
> AB, etc.  Unacceptable examples include: CC, ABB,
> ACAB.  Basically any string with duplicates is out,
> and I don't care about the ordering of the items that
> ARE there.
>
> Any hints?  I'm afraid that I'm going to have to
> enumerate all the permutations.  This wouldn't be bad
> with 3 elements, but in my actual application I have
> 5, which makes for 120 things OR'ed together.  Ick.
>
> Thanks for reading.  Any suggestions would be
> appreciated!
>
> --
> R. Todd Ogrin
> toddogrin@yahoo.com
>
> __________________________________________________
> Do You Yahoo!?
> Make a great connection at Yahoo! Personals.
> http://personals.yahoo.com
>