Backuss naur form grammar variable declaration in c

  • Thread starter Thread starter blob84
  • Start date Start date
  • Tags Tags
    Form Variable
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 3K views
blob84
Messages
25
Reaction score
0

Homework Statement


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.

Homework Equations


declaration ::= variable


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.
 
Physics news on Phys.org
blob84 said:
5. type-int ::= int type-l type-sign

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:
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:
long long i;
unsigned long long j;
signed long long k;