Note that there are some explanatory texts on larger screens.

plurals
  1. POEBNF declaration syntax in c program
    primarykey
    data
    text
    <p>I'm a little new at programming (okay, very new), and I came across Extended Backus Naur Form, or EBNF, and decided to try to figure out how to use it. Unfortunately, even though their are tons of explanations online on how EBNF works, there is precious little on how to actually implement it. So I made a simple little program in C using it, just to see what happens. Here is what I wrote:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; mixture : [letter|digit] {letter | digit}; integer : [ "+"|"-"] digit {digit}; naturalNumber : digit {digit}; digit : "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"; letter : "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "A" | "B" | "C" | "D"| "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |"Y" | "Z"; int main() { char c[7]; c[0] = '1'; c[1] = '2'; c[2] = '3'; c[3] = 'x'; c[4] = 'y'; c[5] = 'z'; c[6] = '\0'; if(c == mixture){ printf("You have a mixture of numbers and letters"); } else if(c == integer){ printf("This is just a number"); } else if(c == naturalNumber){ printf("This is just a positive number"); } else if(c == digit){ printf("This is a plain digit"); } else if(c == letter){ printf("This is a plain letter"); } return 0; } </code></pre> <p>Right away I get a compiler error (I'm using Code Blocks) saying it expects a "=" before the ":", but I used a ":" because everything I read online seems to suggest a ":" is correct. Also, if I change it to "=" I get a compiler error that says "mixture" (and others) doesn't have a type or storage class. Well, "int" in front won't work, and "char" doesn't make sense either, since it's a mixture of both.</p> <p>I'm also not sure if the declarations are supposed to go before main() or inside of it. I ended up putting it before because one thing I read online seemed to suggest this was right. </p> <p>By the way, I'm finding absolutely no online examples in C to follow - does this mean I'm totally thinking about EBNF the wrong way and it can't be used like this at all?</p> <p>Any help is greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload