150 likes | 332 Views
SYNTAX DIRECTED TRANSLATION. COMPILERS 29 TH OCTOBER 2013. SDD. Semantic Rule. A.x = f ( X 1 .a, X 2 .b, X 3 .c ). A X 1 X 2 X 3. SDT. Embedded. A X 1 X 2 { A.x = f( X 1 .a, X 2 .b) } X 3. S – attributed SDD SDT A X 1 X 2 X 3 {a}
E N D
SYNTAX DIRECTED TRANSLATION COMPILERS29TH OCTOBER 2013
SDD Semantic Rule A.x = f ( X1.a, X2.b, X3.c ) A X1X2X3
SDT Embedded A X1X2{A.x = f( X1.a, X2.b) }X3
S – attributed SDD SDT A X1X2X3 {a} When we perform reduction, we evaluate the attribute to get the result
We have the stackX1X2X3 symbols and statesX1.a, X2.b, X3.c values
After ReductionA replaces X1X2X3A.x evaluated by the functionf( X1.a, X2.b, X3.c )
L E { Print (E.val) }E E + T { E.val = E.val + T.val }E T {E.val = T.val}T T * F { T.val= T.val * F.val}T F { T.val= F.val }F id { F.val = id }
To evaluate T.val = T.val * F.val , we need the two values; which we get form the value stack.
L – attributed SDD is suitable for top-down parsing LL (1) synthesized attributes inherited attributes
A non-terminal can have multiple inherited attributes ; for each will have some defined action In case of synthesize attribute, we append it to the end of the ruleFor predictive parsers, for each non-terminal, we have a function
E.G : Recursive Descent ParserA X1X2X3A() { if ( a == X1 ) move to next symbolif ( X1 is a NT) X1() }
This function invokes the functions of the body of the productionTo implement predictive parsing, we need to implement those functions