6.1.1 Attribute grammars
6.1.1 Attribute Grammars
Xa means the value of a' associated to X X is a grammar symbol and a is an attribute associated toⅩ Syntax-directed semantics Attributes are associated directly with the grammar symbols of the language Given a collection of attributes al,..., ak, it implies that for each grammar rule X0,X1X2. Xn(Xo is a nonterminal) The values of the attributes Xi. aj of each grammar symbol Xi are related to the values of the attributes of the other symbols in the rule
• X.a means the value of ‘ a ’ associated to ‘X’ – X is a grammar symbol and a is an attribute associated to X • Syntax-directed semantics: – Attributes are associated directly with the grammar symbols of the language. – Given a collection of attributes a1, …, ak, it implies that for each grammar rule X0→X1X2…Xn (X0 is a nonterminal), – The values of the attributes Xi.aj of each grammar symbol Xi are related to the values of the attributes of the other symbols in the rule
An attribute grammar for attributes al, a2 ak is the collection of all attribute equations or semantic rules of the following form for all the grammar rules of the language Ⅺiaj=fj(X0.al,,X0.ak,…,x1.al,…,xn-1.al,Xnak) Where fii is a mathematical function of its arguments Typically, attribute grammars are written in tabular form as follows Grammar Rule Semantic rules Rule 1 Associated attribute equations Rule n Associated attribute equation
• An attribute grammar for attributes a1, a2, … , ak is the collection of all attribute equations or semantic rules of the following form, – for all the grammar rules of the language. • Xi.aj = fij(X0.a1,…,X0.ak, …, X1.al, …, Xn-1.a1, …Xn.ak) • Where fij is a mathematical function of its arguments • Typically, attribute grammars are written in tabular form as follows: Grammar Rule Semantic Rules Rule 1 Associated attribute equations ... Rule n Associated attribute equation
Example 6.1 consider the following simple grammar for unsigned numbers Number >number digitdigit Digt→0112314156789 The most significant attribute: numeric value(write as val) and the responding attribute grammar is as follows Grammar Rule Semantic rules Number1-number2 digit number l val= number2 val*10+digit val Jumber digit Imber val= digit. val g digit. val =0 digit digit. val= 1 digit-]2 digit. val =2 digit 3 digit val =3 digit,4 digit val 4 digit-75 digit val=5 digit 6 digit. val =6 g digit. val =7 digit→8 digit val 8 digit 9 digit. val=9 able 6.1
Example 6.1 consider the following simple grammar for unsigned numbers: Number → number digit | digit Digit → 0|1|2|3|4|5|6|7|8|9 The most significant attribute: numeric value (write as val), and the responding attribute grammar is as follows: Grammar Rule Semantic Rules Number1→number2 digit number1.val = number2.val*10+digit.val Number→digit number.val= digit.val digit→0 digit.val = 0 digit→1 digit.val = 1 digit→2 digit.val = 2 digit→3 digit.val = 3 digit→4 digit.val = 4 digit→5 digit.val = 5 digit→6 digit.val = 6 digit→7 digit.val = 7 digit→8 digit.val = 8 digit→9 digit.val = 9 table 6.1
The parse tree showing attribute computations for the number 345 is given as follows number (val=34*10+5=345) number git (val=3*10+4=34) (val=5) number 5 (val=3) (val=4 Fig 6.1
The parse tree showing attribute computations for the number 345 is given as follows number (val = 34*10+5 =345) number digit (val = 3*10+4=34) (val = 5) number digit 5 (val=3) (val=4) digit 4 3 Fig 6.1