Backus Naur Form

We recently discussed the Backus Naur Form in our lesson. I actually read and studied some examples but in written language but I'm kinda confused about this examples which has only signs. I don't get why the fourth and the first one should be invalid. hope you can help me! thx


Here is the example:


The following BNF defines a language for the description of train formations for railways:
train = open | compositions.
open = loco cars.
loco = "*" | "*" loco.
cars = "-" | "-" cars.
compositions = composition | composition compositions.
composition = "<" open loco ">".
a) Which of the following formations are valid according to the BNF?

i. <*-*><**>
ii. <*--------------------------*>
iii. ****-
iv. <*-------*>*----
loco means "one or more '*'". cars means "one or more '-'". open means "one or more '*' and then one or more '-'". open loco means "one or more '*' and then one or more '-' and then one or more '*'". Therefore:
"<*-*>" -> Valid
"<***-*>" -> Valid
"<*---*>" -> Valid
"<*-***>" -> Valid
"<**---------*****>" -> Valid
"<**>" -> Invalid. Missing one or more '-'.
"<--->" -> Invalid. '*' must follow '<'.
"<---***-->" -> Invalid. '*' must follow '<'.
"<*-------*>*----" -> Invalid. The train production only allows the end of string to follow compositions.

This has nothing to do with C++, though.
Topic archived. No new replies allowed.