Question 1. Write a BNF grammar for the for loop statement of C. Assume this is
ID: 3743189 • Letter: Q
Question
Question 1. Write a BNF grammar for the for loop statement of C. Assume this is the 89 standard that does NOT allow new variables as C++ did (not allowed is the int in for (int i=0; i<5; i++). Also assume block is NOT a statement (different books take different take on this). Further assume <expression> and <statement> and <block> nonterminals are already defined (you dont' define them, just use them). Do not use the extended BNF notation.Question 2. And For the above, write informal operational semantics (as you would give to someone new to C, trying to explain what it is) Question 1. Write a BNF grammar for the for loop statement of C. Assume this is the 89 standard that does NOT allow new variables as C++ did (not allowed is the int in for (int i=0; i<5; i++). Also assume block is NOT a statement (different books take different take on this). Further assume <expression> and <statement> and <block> nonterminals are already defined (you dont' define them, just use them). Do not use the extended BNF notation.
Question 2. And For the above, write informal operational semantics (as you would give to someone new to C, trying to explain what it is) Question 1. Write a BNF grammar for the for loop statement of C. Assume this is the 89 standard that does NOT allow new variables as C++ did (not allowed is the int in for (int i=0; i<5; i++). Also assume block is NOT a statement (different books take different take on this). Further assume <expression> and <statement> and <block> nonterminals are already defined (you dont' define them, just use them). Do not use the extended BNF notation.
Question 2. And For the above, write informal operational semantics (as you would give to someone new to C, trying to explain what it is)
Explanation / Answer
1. The for loop in C looks like this:
int i;
for (i = 0; i < 5; i++) {
//statement
}
The BNF grammer of this block is:
<for_loop> ::= for(<expression>;<expression>;<expression>){<statement>}
2.
B Bool ::= true | false | E = E | E < E | ... | B&B | ¬B | ...
E Exp ::= n | E + E | E * E ...
for (I1 := n); (if B); (I1 := E) do
(statement)
I1 and I2 are two integers and E is an expression
In for loop, we first assign an integer value to I1. We then check if an expression is true. If it is true, we perform the statements else skip. After performing the statements, we perform some expression and store in I1 and again check the condition.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.