views:

85

answers:

2

Hi all,

Using javacc can I push some new characters in front of the inputstream ?

for example let's say that my parser parses the following syntax:

#define Paragraphs  "Paragraph+"
#define Volume "(Title,(Chapter,${Paragraphs})+)"

Book=${Volume}+;

How can I tell javacc that its scanner should preprocess ${Volume} to (Title,(Chapter,Paragraph+)+) before invoking the parser ?

Can It be achieved using the MORE statement ?

Thanks

A: 

OK, I think I've found the solution: Some java statements can be added in the TOKEN section and the current buffer is defined in a StringBuilder named 'image':

| <Y:"${"(<NAME)+ "}" >
     {
     String oldValue=image.toString();
     image.setLength(0);
     image.append(my_dict.get(oldValue));
     }
Pierre
+1  A: 

Token.image is a public field, so you could also just set it directly. Here's an example in my JavaCC book's tokenizer chapter:

TOKEN : {
   {matchedToken.image = image.append("B").toString();}
}

You can download all the book's example source code here.

tomcopeland