Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Backuss naur form grammar variable declaration in c

  1. Oct 14, 2011 #1
    1. The problem statement, all variables and given/known data
    Write a grammar that recognize a C variable declaration made up only of legal combinations of the following keywords: int char float double signed unsigned long short volatile const
    and a variable name.
    The grammar should be able to accepts all such legal declarations for example:
    volatile unsigned int a; ...
    but :
    volatile signed unsigned int a;
    should not be accepted.

    2. Relevant equations
    declaration ::= variable

    3. The attempt at a solution
    1. declarations ::= variable ; declaration
    2. variable ::= keyword name
    3. keyword ::= type
    4. type ::= type-int | type-char | float | double
    5. type-int ::= int type-l type-sign
    6. type-char ::= char type-sign
    7. type-sign ::= signed | unsigned | empty
    8. type-l ::= long | short | empty

    I think that my solution is not correct.
  2. jcsd
  3. Oct 14, 2011 #2
    Wouldn't this allow declarations such as int unsigned, when it should be allowing declarations such as unsigned int? In addition you don't seem to have any items for the volatile and const specifiers.

    Also, the "int" may be unnecessary in some declarations. The following declarations should be allowed:

    Code (Text):

    short a;
    unsigned short b;
    signed short c;
    long d;
    unsigned long e;
    signed long f;
    signed g; //same as signed int
    unsigned h; //same as unsigned int
    Additionally these are allowed by more recent versions of the C standard:

    Code (Text):

    long long i;
    unsigned long long j;
    signed long long k;
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook