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
Grammar rule Semantic rules Based-num num basechar Based-num. val= num. val Based-num base= basechar base Basechar 0 Basechar base =8 Basechar→d Basechar base 10 Num1>num2 digit numl. val If digit. val error or num2. val=error Then error Else num2. val*numl, base+digit val Num2, ba 1. ba Digit base= num l base Num→ digit num. val =digit. val Digit base= num base Digit 0 digit. val =0 Digit→1 digit. val Digit→7 digit. val=7 Digit >8 digit. val =if digit base =8 then error else 8 Digit→9 digit. val= if digit base =8 then error else 9 Tab 6.4
Grammar Rule Semantic Rules Based-num→num basechar Based-num.val = num.val Based-num.base = basechar.base Basechar →o Basechar.base = 8 Basechar→ d Basechar.base = 10 Num1→num2 digit num1.val = If digit.val = error or num2.val = error Then error Else num2.val*num1.base+digit.val Num2.base = num1.base Digit.base = num1.base Num → digit num.val = digit.val Digit.base = num.base Digit →0 digit.val = 0 Digit →1 digit.val = 1 … … Digit →7 digit.val = 7 Digit →8 digit.val = if digit.base = 8 then error else 8 Digit →9 digit.val = if digit.base = 8 then error else 9 Tab 6.4