For a given grammar: X 0 X 0 | 1 X 1 | Y | 1 X 0 | 0 X 1 Y 1 | 0 | A: Create syntax directed definition (SDD) to get the value and the length of the string. B: SDD should also generate an error message if the given string is not acceptable by the grammar. C: Convert the SDD into an equivalent L-attributed SDD and identify each attribute. D: Convert the L-attributed SDD into an equivalent syntax directed translation (SDT)
Answers
Answer:
For a given grammar: X 0 X 0 | 1 X 1 | Y | 1 X 0 | 0 X 1 Y 1 | 0 | A: Create syntax directed definition (SDD) to get the value and the length of the string. B: SDD should also generate an error message if the given string is not acceptable by the grammar. C: Convert the SDD into an equivalent L-attributed SDD and identify each attribute. D: Convert the L-attributed SDD into an equivalent syntax directed translation (SDT)
Answer:
Given,
X-> 0X0|1X1|Y
Y-> 1|0
A) Create syntax directed definition(SDD)
to get the value and the length of the string
productions Semantic Rules
X->0X0 X.Val=0X0
X->1X1 X.Val=1X1
X->Y Y1=0
Y->0 Y.Val=0;
Y->1 Y.val=1
Parse Tree
X-> 0X0|1X1|Y
Y-> 1|0
X
X
0
X
0
1
X
1
Y
Y
1
or
0
B) Sdd should also shows error message, if the given string is not accessible by the grammer
bool checkSentence(char str[])
{
// Calculate the length of the string.
int len = strlen(str);
// Check that the first character lies in [A-Z].
// Otherwise return false.
if (str[0] < 'A' || str[0] > 'Z')
return false;
}
c) Convert the SDD into an equivalent L-attributed SDD and Identified each attribute
1. S-attributed SDT :
· If an SDT uses only synthesized attributes, it is called as S-attributed SDT.
· S-attributed SDTs are evaluated in bottom-up parsing, as the values of the parent nodes depend upon the values of the child nodes.
· Semantic actions are placed in rightmost place of RHS.
2. L-attributed SDT:
· If an SDT uses both synthesized attributes and inherited attributes with a restriction that inherited attribute can inherit values from left siblings only, it is called as L-attributed SDT.
· Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing manner.
· Semantic actions are placed anywhere in RHS.
Y-> 1|0
X-> 0X0|1X1|Y
D ) Convert the L attribute SDD into an equivalent syntax directed translation (SDT)
X-> 0X0|1X1|Y
Y-> 1|0
X′ → X Accept 1 : x → SL if (S.sign=*)X.val=0 * X.val=0; else X.val =1 X.val=1; 2 : X → + X.sign = ’X’; 3 : X → Y; Y if(Y → 1 or Y.val = 0);
X-> 0X0|1X1|Y
Y-> 1|0
Explanation: