SDT For Control Statements
SDT For Control Statements
while (x > 0) {
x = x - 1;
print("Decremented x: " + x);
}
Parsing this sequence according to the grammar produces a parse tree.
During the parsing process, semantic actions associated with each
production rule are executed.
Parse Tree:
S
/ / \
if while other_statement
/ \ | |
expr S expr "other_statement"
/ \ |
"x > 0" S other_statement
/ \
"x is positive" "x is non-positive"
Semantic Actions Execution:
generate_code("x > 0");
generate_code(S1); print("if (expr)");
generate_code("x > 0");
generate_code(S2); print("while (expr)");
print("x is positive");
print("x is non-positive");
print("Decremented x: " + x)