• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JavaCC problem with duplicate Tokens

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I always get into the following situation:


Imagine I have the following file to parse

Foo Ball hall
Zoe Ball mall

Bar Ball hall
Zoe Ball anythingelse


Well, the file might be defined that way, that I know there will be group of lines (2 in this case)
One is alway starting with "Zoe" (the second one, as "Zoe Ball mall" and "Zoe Ball anythingelse")
The second token will always be "Ball" in every line.



parse()
{
String found = "";
}
{
(
// First line
(
<ALPHANUM>
<BALLTOKEN>
<ALPHANUM>
{ found = "Hey, I am in line one";}
)
//Second line, the one with zoe.
(
<ALPHANUMSTARTSWITHZ>
<BALLTOKEN>
<ALPHANUM>
{ found = "Hey, I am in line two!!! yaey.";}
)
)*
}

TOKEN : { <ALPHANUM ["a-z","A-Z","0-9"]> }
TOKEN : { <ALPHASTARTINGWITHZ "Z"(["a-z"])*> }
TOKEN : { <BALLTOKEN "Ball" >}

What is gonna happen?!

Foo will match <ALPHANUM>
Ball will match <BALLTOKEN>
hall will match <ALPHANUM>

Zoe is supposed to match <ALPHANUMSTARTINGWITHZ> but will match <ALPHANUM> and an Exception will be thrown, because Zoe also matches <ALPHANUM>
Ball
mall

How can I resolve that problem?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you will need to consider LOOKAHEAD to resolve the ambiguity.
 
Basti Schaf
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved the problems with states like

Not depending on the example, defined in the first tag, but a general example.
I don't think that LOOKAHEAD would be the right sollution, because in fact the JavaCC just thought it found an TokenAB even if it was just a TokenA.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic