Search code examples
javastringparsingcompiler-constructionjavacc

How do I return a string from a javacc method?


I have this method in javacc to parse a url (e.g. /books/id/1). How do I make this method return the whole url string?

void path() :
{}
{
  (< SLASH > ( < IDENTIFIER >))+
}

I have tried to create a Token t to absorve all the tokens but it's not working.

void path() :
{Token t = new Token();}
{
  t = ((< SLASH > ( < IDENTIFIER >))+)
}

Solution

  • I managed to get the string using the following code:

    String path() :
    {String path = "";
    Token slash = null;
    Token id = null;}
    {
      (slash = < SLASH > ( id = < IDENTIFIER >)
      {
        path += slash.toString() + id.toString();
      })+
      {
        return path;
      }  
    }