Backuss naur form grammar variable declaration in c

  • Thread starter Thread starter blob84
  • Start date Start date
  • Tags Tags
    Form Variable
Click For Summary
The discussion focuses on creating a Backus-Naur Form (BNF) grammar for recognizing valid C variable declarations using specific keywords and variable names. Participants express concerns that the proposed grammar may incorrectly allow invalid combinations, such as "int unsigned," while missing essential specifiers like "volatile" and "const." They highlight that certain declarations, such as "short a;" and "unsigned long e;", should be valid without requiring an explicit type like "int." The conversation also notes that more recent C standards permit additional types like "long long." The overall goal is to refine the grammar to accurately reflect the rules of C variable declarations.
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;
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K