Backuss naur form grammar variable declaration in c

  • Thread starter Thread starter blob84
  • Start date Start date
  • Tags Tags
    Form Variable
Click For Summary
SUMMARY

The discussion focuses on creating a Backus-Naur Form (BNF) grammar for C variable declarations, specifically addressing legal combinations of keywords such as int, char, float, double, signed, unsigned, long, short, volatile, and const. The proposed grammar includes rules for variable declarations, but it fails to correctly handle the order of type specifiers, particularly allowing invalid combinations like "int unsigned". The grammar also lacks provisions for the volatile and const specifiers and does not account for newer C standards that permit declarations like "long long".

PREREQUISITES
  • Understanding of Backus-Naur Form (BNF) syntax
  • Familiarity with C programming language variable declarations
  • Knowledge of C data types and specifiers
  • Awareness of C language standards and updates
NEXT STEPS
  • Study BNF grammar rules for programming languages
  • Research C variable declaration syntax and rules
  • Learn about the implications of C language standards on variable declarations
  • Explore tools for parsing C code and validating grammar
USEFUL FOR

C programmers, computer science students, and software developers interested in compiler design and syntax validation for 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
5K
  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
7K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 14 ·
Replies
14
Views
5K