Dynamic generation of parsed source

View: New views
2 Messages — Rating Filter:   Alert me  

Dynamic generation of parsed source

by Jean-Francois_Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
 
I have a need to tinker with the source code while it is being parsed, in other words some parsed statements will have a direct impact on the code that follows, either being a symbolic replacement, or adhoc inclusion of source code, line by line as line is the main delimiter.
 
Is there an elegant way to handle this with Sablecc, either by having a modified reader that would feed the parser resulting lines?
 
regards
 
JFG

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: Dynamic generation of parsed source

by Etienne M. Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Jean-Francois,

If you have a language which is nicely delimited by lines, then you should consider writing a "line parser", instead of a file parser.

Mainly, you need to write the grammar of a single line, e.g.
...
Productions
 line =
   {statement} statement |
   {declaration} declaration;
 statement =
...
Then, you will need to instantiate one lexer/parser for each program line. You don't need to worry, all the lexer/parser tables are stored in static variables, so the impact on the garbage collector will be minimal. In other words, your interpretation loop will look like:

...
BufferedReader programReader = ...;
String line;
while((line = programReader.readLine()) != null) {
 Node lineAst =
  new Parser(
   new Lexer(
    new PushBackReader(
     new StringReader(line), 
     1024
    )
   )
 ).parse();
 
 ... // do your stuff
}
Of course, in your case, you might want to assign a different value to the line variable, in the code above.

Have fun!

Etienne

Jean-Francois Gagnon wrote:
Hi,
 
I have a need to tinker with the source code while it is being parsed, in other words some parsed statements will have a direct impact on the code that follows, either being a symbolic replacement, or adhoc inclusion of source code, line by line as line is the main delimiter.
 
Is there an elegant way to handle this with Sablecc, either by having a modified reader that would feed the parser resulting lines? 

-- 
Etienne M. Gagnon, Ph.D.
SableCC:                                            http://sablecc.org
SableVM:                                            http://sablevm.org


_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

signature.asc (265 bytes) Download Attachment
LightInTheBox - Buy quality products at wholesale price