0

I'm kind of confused as to whether or not my answer is correct. I'm not asking anyone to give me the right answer if I'm wrong but rather I would like to know if my understanding for this topic is correct because if it is not, then if possible I would like be pointed to a decent reference so I can study more on this until I really understand it.

What I'm supposed to do is to create a Context-Free Grammar rule and derive it using leftmost derivation for this statement:

if ( a >= b ) { w = x; z = x / y + w; }

Here is the grammar rule I made

Here is my leftmost derivation

  • What is your doubt? I'd say the grammar doesn't quite express the language, but it's close enough for a first attempt; the derivation looks ok at a glance, although I find it difficult to read images. You should get in the habit of pasting text here, not images. – rici Dec 01 '21 at 20:47
  • Note that a comparison is an *expression* (whose value is true or false), not a *statement*. An `if` statement is a statement, so it can appear in the body of an `if` statement, but not as the condition. Also, an expression (including comparisons) does not necessarily have a variable on the left hand side; `x + y < 4` is a perfectly reasonable condition. Those are things which could be improved. – rici Dec 01 '21 at 20:51
  • @rici Sorry for the late reply. But thank you so much for the corrections and advice, I'll be keeping those in mind as I keep going with learning on this topic. Your feedback is much appreciated :) –  Dec 03 '21 at 15:26

0 Answers0