简体   繁体   中英

JavaCC Beginner - How do I make the program only accept if there are no duplicates?

So I'm a beginner to JavaCC and I want to only accept this string if there are no duplicates of it. So if the input is below:

A B C
A' B' C'
A'' B'' C''

The parser will only accept it if `B != B' != B''

I feel like I'm missing something basic, and I can't find many easy to explain tutorials online for JavaCC. If someone could point me in the write direction that would be great.

Thanks in advance.`

EDIT: I should point out that currently my parser accepts the input above, regardless of duplicates.

You could do something like the following. In the parser class, declare a field

Set<String> seen = new HashSet<String>() ;

(This is assuming a nonstatic parser. If the parser is static, the field must be static and must be reinitialized in the reinit function.)

Then in your productions, you can do something like this.

void start() : { Token t ;} {
    (   <A>
    t = <B> { if( seen.contains( t.image ) ) {
                  throw new ParserException( "Duplicate" ) ; }
              else { seen.add( t.image ) ; } }
        <C>
    )*
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM