I was wondering how to write a three address code using ocamllex and ocamlyacc? I googled a lot about this, but I couldn't find anything using ocamlyacc. I have my parser and my lexer working(both using of course ocamlyacc and ocamllex) but now I have to write a three address code generator using them. For example, suppose that I have this parser(Calculator):
How should I write the three address code?
Parser:
input: /* empty */ { }
| input line { }
;
line: NEWLINE { }
| exp NEWLINE { }
;
exp: NUM { }
| exp PLUS exp { }
| exp MINUS exp { }
| exp MULTIPLY exp { }
| exp DIVIDE exp { }
| MINUS exp %prec NEG { }
| exp CARET exp { }
| LPAREN exp RPAREN { }
;
Example:
INPUT:
5+(5*7)
Three Address Code Output:
t1 = 5*7
t2 = 5+t1