I need to implement compiler (lexical, syntax and semantic analyzers). I have already implemented the lexical from flex++, syntax from hand (recursive descent), and know I'm working on semantic (adding semantic rules to the existing parser). My question is - how do I implement attributes. For example, for production:
TYPE -> integer
I need to add semantic rule:
TYPE.type = integer
So here I need to create some structure for TYPE, and so on for all the grammar symbols. So how do I implement that structures? Do I need to make some struct for each grammar symbol and it need to be global for scope of file?
p.s. maybe it is not the best compiler implementation, it is just requirment for assignment.
It is easiest if you define a type (typically a struct) for each terminal and nonterminal in your language. Then each use of a (non)terminal has access to the type, and each assignment of an attribute goes against the corresponding slot in the structure associated with (non)terminal.
I suggest you name your implementation structures after the grammar tokens. So, the type of the attribute structure for T would be "T_attributes { ... } "
For your example, "type" would be a slot in the T_attributes. Probably what you meant to write in the abstract was:
TYPE -> 'integer' ; -- 'integer is a keyword'
TYPE.type = "int"; -- the type of an 'integer' is "int"
Given you are doing this by hand, you will hand-compile the attribute assignment to execute when a tree walk hits the TYPE node.