compiler construction - Syntax directed translation -
i trying write semantic rules syntax directed translation of expression following given cfg 3 code representation.
consider,
here, why || operator required ? gen() thing needed seems required .
this set of rules describes attributed grammar, scheme computing attributes tree. such schemes purely functional (no side effects). computing attributes @ each rule/tree node, named x.y in notation using, x stands rule/tree (e.g s , e variants e1 , e2. y stands computed attribute (in case, there "code" , "place" attributes.
each rule corresponds subtree, e = e1 + e2 tree (e+ e1 e2) in s-expression notation. "e.code:=..." means compute "..." , assign result attribute "code" @ root of "e(+)" tree (a "synthesized" attribute); "e1.code:=..." means compute "..." , assign "e1" leaf (an "inherited attribute"). "x.y" not on left side of assignment means "fetch value of attribute y on node x".
the computations used "newtemp" (this should "newtemp()" isn't variable, function), "gen( ... )" , "||". "gen" doing key work of manufacturing individual instructions. result? simple answer "a string"; more sophisticated answer might binary representation of generated instructions.
the purpose of "||" assemble results of code generation steps stream of generated code; if gen result string, "||" string concatenate, if result binary records, concatenating lists of binary records single list.
you may have been confused thinking "gen" produced results , wrote them unmentioned output stream. violate spirit of attribute grammar, not allow such. "||" operator needed compute functional results being passed tree.
you might bend attribute grammar gen does write hidden stream. in case "||" operators go away, mentions of computed results such "e1.code". can make work if attribute tree evaluation left-to-right, (a big) and, order in generated subtrees combined produce result stream correct when done strictly left right. if attribute grammar said "e.code = e2.code || e1.code || gen(...)", is, reordering generated code, hidden output stream trick not work.
taking different perspective: imagine wanted evaluate attribute grammar on huge programs. if stick pure functional grammar, can evaluate attributes in parallel! use recursive procedure forks helper grains each child. [sound crazy? not, have attribute evaluator system (see bio) works on principle]. hidden output stream won't work in case, either, because parallelism may cause children visited in order.
Comments
Post a Comment