Search code examples
data-structuresabstract-syntax-treeyacc

yacc: how to "save" code for execution in future?


I'm stuck in misunderstanding of how to "save" grammar rule parsed by yacc into abstract syntax tree. Here's a piece of my yacc file:

expression:
              expr2                     { $$ = $1; }
            | expr2 EQ expr2            { $$ = ($1 == $3); }
            | expr2 NE expr2            { $$ = ($1 != $3); }
            | expr2 LT expr2            { $$ = ($1 < $3); }
            | expr2 LE expr2            { $$ = ($1 <= $3); }
            | expr2 GT expr2            { $$ = ($1 > $3); }
            | expr2 GE expr2            { $$ = ($1 >= $3); }
            ;

expr2:
              expr3                     { $$ == $1; }
            | expr2 PLUS expr3          { $$ = $1 + $3; }
            | expr2 MINUS expr3         { $$ = $1 - $3; }
            ;

expr3:
              expr4                     { $$ = $1; }
            | expr3 MULT expr4          { $$ = $1 * $3; }
            | expr3 DIVIDE expr4        { $$ = $1 / $3; }
            ;

as you can see all the actions are performed on the fly. I'd like to make something like:

expr2 PLUS expr3          { $$ = save_code_for_addition($1, $3); }

to store this action as a node of abstract syntax tree.

Could someone please kindly explain me what would be the inner representation for such a presaved instruction and how on earth can I later execute it via execute(Statement s), what is Statement data type? how to construct it? I would really appriciate any help, thanks.


Solution

  • I've found a manual completely answering this question: http://web.eecs.utk.edu/~bvz/teaching/cs461Sp11/notes/parse_tree/