开发者

Decorating parse tree using attribute grammar

开发者 https://www.devze.com 2023-04-13 03:51 出处:网络
Given the following attribute grammar for type declarations, I need to be able to produce a parse tree for any given string, for example \"A, B : C;\", and then decorate the tree.

Given the following attribute grammar for type declarations, I need to be able to produce a parse tree for any given string, for example "A, B : C;", and then decorate the tree.

I can generally do this for simple attribute grammars and when its obvious what t开发者_如何学Pythonhe attributes are, but I can not decipher what out_tab and in_tab are. Yes, this is my homework and I am not asking for the solution, I am asking for guidance on what these attributes mean and possible examples to assist me.

decl -> ID decl_tail
    decl.t := decl_tail.t
    decl_tail.in_tab := insert(decl,in_tab, ID.n, decl_tail.t)
    decl.out_tab := decl_tail.out_tab
decl_tail -> , decl
    decl_tail.t := decl.t
    decl.in_tab := decl_tail.in_tab
    decl_tail.out_tab := decl.out_tab
decl_tail -> : ID ;
    decl_tail.t := ID.n
    decl_tail.out_tab := decl_tail.in_tab


Several years after, I hope you finished well your home work :)

about your exercise:

decl.t and decl_tail.t are synthetized atributes that copy the Type name (ID.n) and pass it for each parent production in the parsing tree

when a production decl -> ID decl_tail is reached, the in_tab attributte keeps a kind of list of relationship between identifiers and types (it is not clear the type of in_tab but we can assume a list of tuples (identifier; type)). This attribute is inherited, so the list will start to be constructed in the top most production (leftmost identifier), and continue constructing it from left to right and finish it in the rightmost identifier.

Then the list is finished to be constructed when decl_tail -> : ID; is reached so a synthesised attribute (out_tab) is used to synthesise the result again to the starting symbol.

this drawing was the best I could do for drawing the decorated tree and graph dependence:

Decorating parse tree using attribute grammar

Blue arrows are the synthetizing of the t attribute, the green arrows are how the in list is constructed and the red arrows are how the result is synthesized to the initial symbol

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号