Example 6.2 consider the following grammar for simple integer arithmetic expressions Exp> exp term exp -term term Term term*factor factor Factor>(exp) number The principal attribute of an exp(or term or factor) is its numeric value(write as val)and the attribute equations for the val attribute are given as follows Grammar Rule Semantic rules expl→exp2+term expl. val=exp2. val+term. val Xp1→exp2-term expl. val=exp2. val-erm val expl, term expl. val= term. val term1→term2* factor term l. val=term 2 val factor val term→ factor term, val=factor val factor>(exp) factor. val=exp. val factor→> number factor.ⅴal= numberⅴal table 6.2
Example 6.2 consider the following grammar for simple integer arithmetic expressions: Exp → exp + term | exp-term | term Term → term*factor | factor Factor → (exp)| number The principal attribute of an exp (or term or factor) is its numeric value (write as val) and the attribute equations for the val attribute are given as follows Grammar Rule Semantic Rules exp1→exp2+term exp1.val=exp2.val+term.val exp1→exp2-term exp1.val=exp2.val-erm.val exp1→ term exp1.val= term.val term1→term2*factor term1.val=term2.val*factor.val term→factor term.val=factor.val factor→(exp) factor.val=exp.val factor→number factor.val=number.val table 6.2
Given the expression (34-3)*42, the computations implied by this attribute grammar by attaching equations to nodes in a parse tree is as follows (val=1302) (val=31*42=1302) factor val=34-3=31) term (val =3) number (val=34) (val =3) Fig6.2
Given the expression (34-3)*42 , the computations implied by this attribute grammar by attaching equations to nodes in a parse tree is as follows Fig6.2 Exp (val = 1302) term (val = 31*42=1302) term (val = 31) factor (val = 42) Exp ( (val = 34-3=31) ) Exp (val = 34) term (val = 3) factor (val = 31) number (val = 42) term (val = 34) factor (val =3) factor (val = 34) number (val = 3) number (val = 34) - *
Example 6.3 consider the following simple grammar of variable declarations in a C-like syntax Decl→ type var-list Type int floa Var- list id, var- list id Define a data type attribute for the variables given by the identifiers in a declaration and write equations expressing how the data type attribute is related to the type of the declaration as fc allOwS We use the name dtype to distinguish the attribute from the nonterminal type) Grammar Rule Semantic rules decl→ type var-list var-list dtype type dtype ype→int type dtyp ge type→ float type dtype real var-list1-id. var-list2 iddtype= var-listl dtype list2 dtype= var-listl dtype var-list→id id type= var-list dtype table 6.3
Example 6.3 consider the following simple grammar of variable declarations in a C-like syntax: Decl → type var-list Type→int | float Var-list→id, var-list |id Define a data type attribute for the variables given by the identifiers in a declaration and write equations expressing how the data type attribute is related to the type of the declaration as follows: (We use the name dtype to distinguish the attribute from the nonterminal type) Grammar Rule Semantic Rules decl→type var-list var-list.dtype = type.dtype type→int type.dtype = integer type→ float type.dtype = real var-list1→id,var-list2 id.dtype = var-list1.dtype var-list2.dtype= var-list1.dtype var-list→id id.type = var-list.dtype table 6.3
Note that there is no equation involving the dtype of the nonterminal decl It is not necessary for the value of an attribute to be specified for all grammar symbols Parse tree for the string float x, y showing the dtype attribute as specified by the attribute grammar above is as follows de Var-list (dtype real) (dtype real) float (dtype =real) (dtype= real) (y) (dtype= real) Fig6.3 Attribute grammars may involve several interdependent attributes
Note that there is no equation involving the dtype of the nonterminal decl. It is not necessary for the value of an attribute to be specified for all grammar symbols Parse tree for the string float x,y showing the dtype attribute as specified by the attribute grammar above is as follows: Fig6.3 Attribute grammars may involve several interdependent attributes. decl Type (dtype = real) Var-list (dtype = real) Var-list (dtype = real) Id (x) (dtype = real) Id (y) (dtype = real) float
Example 6.4 consider the following grammar, where numbers may be octal or decimal, suppose this is indicated by a one-character suffix o(for octal)or d(for decimal) Based-num >num basechar Basechar> old Num>num digit digit Digt→0|123456789 In this case num and digit require a new attribute base, which is used to compute the val attribute The attribute grammar for base and val is given as follows
Example 6.4 consider the following grammar, where numbers may be octal or decimal, suppose this is indicated by a one-character suffix o(for octal) or d(for decimal): Based-num → num basechar Basechar → o|d Num → num digit | digit Digit → 0|1|2|3|4|5|6|7|8|9 In this case num and digit require a new attribute base, which is used to compute the val attribute. The attribute grammar for base and val is given as follows