parsingcompiler-constructionabstract-syntax-treecontext-free-grammarsemantic-analysis

Synthesized vs Inherited Attributes


How can I find if an attribute is synthesized or inherited from the productions of a grammar?

I guess for that the attribute must be predefined in the problem -- if its value depends on child or parent nodes. But is there a way to analyse if an attribute is inherited or synthesized from grammar productions.


Solution

  • Synthesized Attribute: An attribute that gets its values from the attributes attached to the children of its non-terminal.

    Inherited Attribute: An attribute that gets its values from the attributes attached to the parent (or siblings) of its non-terminal.

             **PRODUCTION**                             **SEMANTIC RULES**
    
                 T->FT’                                    T’.inh=F.val
                                                           T.val=T’.syn
    
               T’->*FT1’                              T1’.inh=T’.inh*F.val
                                                          T’.syn=T1’.syn
    
                 T’->Ɛ                                    T’.syn=T’.inh
    
                 F->id                                   F.val=id.lexval
    

    As you can see from the given grammar rules(productions), inh is an inherited attribute and syn is a synthesized attribute.


    Further Read: Attribute Grammars.