Parsing JSP-like texts.

I have such a problem: there are text with JSP-like special tags in it:

<%! <some declaration> %>

<some text goes here><%= <some expression> %><another text goes here>

<% <some code> %>

<%! <declaration again> %>

<all the remaining text>

Declarations and code are written in special formal language, it's not Java. I want to construct program in it using algorithm similar with using by JSP container, here's the needed output:

<some declaration>

<declaration again>

out.print("<some text goes here>" + <some expression> +"<another text goes here>");

<some code>

out.print("<all the remaining text>");

I parse the text using finite automata, it has states indicating which tag we have met. The question is: is there any way to collect all declarations in the top of output file without using some temporary storage? With it the algorithm looks like not very effecient, because if we had two declaration areas separated with a little text it will be something like copying almost all file content into temporary storage (like StringBuffer) and then flushing it all to the output.

[1343 byte] By [i_am_neuron] at [2007-9-27 21:07:00]
# 1

You can parse the file twice: first for the declarations, and secondly for the remaining parts.

If your language supports some kind of #include directive, then you can generate two files: one for the declarations, and another one with an "#include declarations" and then the remaining code.

remistigri at 2007-7-7 2:49:54 > top of Java-index,Other Topics,Algorithms...
# 2

There's no way to do this using finite automata and no temporary storage. There are mathematical reasons for this that I won't go into.

The simplest might be to use an #include directive, as the other write suggests.

If you don't have such a thing, you'll be forced to use some kind of temporary storage.

If you *really* don't want to use any RAM for the storage, maybe create two files simultaneously, one for the declarations and one for the code, then later merge the two.

Yours, Mike H...

Mike40033 at 2007-7-7 2:49:54 > top of Java-index,Other Topics,Algorithms...