Difference between revisions of "Attribute Grammars/Exercise 4: Expressions with bases (1)"

From Wiki**3

< Attribute Grammars
(Solution)
 
Line 13: Line 13:
 
The grammar uses only one set of rules for all bases (the default is 10, as shown in the second production for N). The inherited attribute '''b''' contains the base value, while the synthesized attribute '''val''' contains the actual number value.
 
The grammar uses only one set of rules for all bases (the default is 10, as shown in the second production for N). The inherited attribute '''b''' contains the base value, while the synthesized attribute '''val''' contains the actual number value.
  
  E<sub>0</sub> -> E<sub>1</sub> '''+''' N  { <font color="blue">E<sub>0</sub>.val = E<sub>1</sub>.val + N.val;</font> }
+
  E<sub>0</sub> E<sub>1</sub> '''+''' N  { <font color="blue">E<sub>0</sub>.val = E<sub>1</sub>.val + N.val;</font> }
  E -> N        { <font color="blue">E.val = N.val;</font> }
+
  E N        { <font color="blue">E.val = N.val;</font> }
  N -> R<sub>1</sub> '''#''' R<sub>2</sub>  { <font color="brown">R<sub>1</sub>.b = 10; R<sub>2</sub>.b = R<sub>1</sub>.val;</font> <font color="blue">N.val = R<sub>2</sub>.val;</font> }
+
  N R<sub>1</sub> '''#''' R<sub>2</sub>  { <font color="brown">R<sub>1</sub>.b = 10; R<sub>2</sub>.b = R<sub>1</sub>.val;</font> <font color="blue">N.val = R<sub>2</sub>.val;</font> }
  N -> R        { <font color="brown">R.b = 10;</font> <font color="blue">N.val = R.val;</font> }
+
  N R        { <font color="brown">R.b = 10;</font> <font color="blue">N.val = R.val;</font> }
  R<sub>0</sub> -> R<sub>1</sub> DIG  { <font color="brown">R<sub>1</sub>.b = R<sub>0</sub>.b;</font> <font color="blue">R<sub>0</sub>.val = R<sub>1</sub>.val * R<sub>0</sub>.b + DIG.val;</font> }
+
  R<sub>0</sub> R<sub>1</sub> DIG  { <font color="brown">R<sub>1</sub>.b = R<sub>0</sub>.b;</font> <font color="blue">R<sub>0</sub>.val = R<sub>1</sub>.val * R<sub>0</sub>.b + DIG.val;</font> }
  R -> '''DIG'''      { <font color="blue">R.val = DIG.val;</font> }
+
  R '''DIG'''      { <font color="blue">R.val = DIG.val;</font> }
  
 
The following is the annotated tree for the string
 
The following is the annotated tree for the string

Latest revision as of 12:00, 2 May 2024

Problem (in Portuguese)

Pretende-se criar uma gramática atributiva que some uma sequência de inteiros separados por +. Os inteiros podem estar codificados sob o formato decimal (sequência de dígitos entre 0 e 9, representados pelo elemento lexical DIG), ou em qualquer base entre 2 e 36 (sequências de dígitos de 0 a 9 e, acima de 10, também contendo as letras de A a Z que forem necessárias para representar todos os valores da base, também representados pelo elemento lexical DIG). Quando a base não é decimal, a representação do inteiro é precedida pelo valor decimal da base e pelo símbolo #.

O exemplo seguinte tem como resultado 241+33=274.

2 4 1 + 1 7 # 1 G

Crie a gramática para realizar a função descrita e construa a árvore semântica anotada para a entrada acima.

Solution

The grammar uses only one set of rules for all bases (the default is 10, as shown in the second production for N). The inherited attribute b contains the base value, while the synthesized attribute val contains the actual number value.

E0 → E1 + N   { E0.val = E1.val + N.val; }
E → N         { E.val = N.val; }
N → R1 # R2   { R1.b = 10; R2.b = R1.val; N.val = R2.val; }
N → R         { R.b = 10; N.val = R.val; }
R0 → R1 DIG   { R1.b = R0.b; R0.val = R1.val * R0.b + DIG.val; }
R → DIG       { R.val = DIG.val; }

The following is the annotated tree for the string

2 4 1 + 1 7 # 1 G

Note that the order of evaluation for the attributes is, first inherited, then synthesized.

AttrGramEx4Bases1.png

(there is a small bug in the image: 23 should be 17+16=33, as stated in the text)