Ivan Senji
Joined: 03 Sep 2004 Posts: 8 Location: Zagreb, Croatia
|
Posted: Sun Sep 12, 2004 6:18 am Post subject: Generating d code now.. |
|
|
Now you can add D code into the grammar definition and it will generate D code (a recursive descent parser of (la)lr generated parse tree)
The example is in the tests\translate-gram and it is a grammar that translates simple expressions into postfix form.
Code fragments are delimited with {{{ and }}} (maybe change it because
it is a little bit ugly).
The code between the first {{{ and }}} int the grammar file is wher you put a main() of your parser.
Other rules look like this.
<S> -> <A> <B> <C>
{{{ inout int x, inout int y /*parameters for S*/
part 0 //before A is executed
//some D code here
call x+1, y+1 /*optional: what params to pass to A*/
part 1 //before B is executed
//...D code
call x /*pass this param to B*/
part 2 //before C is executed
//D code
call y,x,x+y /*pass this to C*/
part 3 //after C is executed
printf("\nDone parsing S ");
}}}
Maybe it has some more bugs i couldn't find and the format of the embeded code fragments is ugly, but i didn't have better ideas at the moment.
To use it compile main.d lr.d ivan_set.d ivan_nodetree.d into (for example) lr.exe
With grammar file "test1" call it like this:
lr -i test1 +forseRecalc //optionaly "+lalr 0" for lalr table
this will generate a file test1.d wich has to be compiled with lr.d, ivan_set.d and ivan_nodetree.d to get the final parser.
PS I know my descriptions are awfull and i hope this doesn't sound too complicated. Looking at translate-gram example should make everything clearer |
|