11

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.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
user2047167
  • 139
  • 1
  • 1
  • 7
  • i.e. An attribute t must be predefined to be S or I. Could I tell from the semantic rules associated with productions or not. Please give an example. – user2047167 Apr 11 '15 at 11:00

3 Answers3

12

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.

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
  • 1
    @user2047167-Yes, val is a syn. attribute as T.val is taking values from its child nodeT'.syn; you're absolutely correct! – Am_I_Helpful Apr 11 '15 at 11:07
  • 2
    What if there is an attribute that in one rule uses parent's attribute for its evaluation and in another rule it uses its children value for evaluation – user2047167 Apr 11 '15 at 11:07
  • 1
    @user2047167- You should check [L-attributed SDT] from (http://www.tutorialspoint.com/compiler_design/compiler_design_semantic_analysis.htm#L-attributedSDT). If you've further problem, comment here. – Am_I_Helpful Apr 11 '15 at 11:09
  • Is there a quick thumb rule by which I can classify an attributes. I know syn attributes have evaluate only by using the values in their production body..I guess most of the rules that involve syn attributes have the LHS nonterminal of producion on the LHS of the semantic rule and rules involving Inherited attributes have RHS non terminals of the production on the LHS of the semantic rule. I think by this I can figure very easily the nature of attributes. Am I right ? – user2047167 Apr 11 '15 at 11:11
  • @user2047167-Again ,you aren't clear to me. I request you to ask a new question with an example of your's as a follow up question to this. I'll help you there. – Am_I_Helpful Apr 11 '15 at 11:19
1

The attribute which takes data values from its child nodes is called a synthesized attribute.

These are also called s-attributed production. The attribute which takes values from parents or sibling nodes is called inherited attributes. The production rule having inherited attribute(Each inherited attributes is restricted to inherit either from a parent or left sibling only) are called L-attributed productions.

-2

Lets look at the calculator

PRODUCTION

  1. L ->E $
  2. E ->E1 + T
  3. E ->T
  4. T ->T1 * F
  5. T ->F
  6. F ->(E)
  7. F ->number

SEMANTIC RULES

  1. print(E.val)
  2. E.val := E1.val + T.val
  3. E.val := T.val
  4. T.val := T1.val * F.val
  5. T.val := F.val
  6. F.val := E.val
  7. F.val := number.lexval