Backuss naur form grammar variable declaration in c

In summary, the grammar should be able to recognize legal C variable declarations consisting of keywords such as int, char, float, double, signed, unsigned, long, short, volatile, and const, followed by a variable name. The type specifier can include a combination of these keywords and the variable name, but the order and usage of these keywords should follow the proper syntax for C variable declarations. The solution may need to consider additional specifiers and account for variations in newer versions of the C standard.
  • #1
blob84
25
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
  • #2
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;
 

1. What is the purpose of Backus-Naur Form (BNF) in C programming?

BNF is a notation system used to describe the syntax of a programming language. In C programming, BNF is used to define the rules for variable declaration, which is essential for writing correct and efficient code.

2. How is a variable declared using BNF in C programming?

In BNF, a variable declaration in C programming follows the form variable_name :: = type identifier, where the variable name is on the left side of the double colon (::) and the data type and identifier are on the right side.

3. What are the different data types that can be declared using BNF in C programming?

The most commonly used data types in C programming are int for integers, float for floating-point numbers, char for characters, and double for double-precision floating-point numbers. Other data types include long, short, and void.

4. Can multiple variables be declared in a single BNF declaration in C programming?

Yes, multiple variables can be declared in a single BNF declaration by separating them with commas. For example, int a, b, c; declares three integer variables named a, b, and c.

5. Are there any restrictions on variable names in BNF variable declarations in C programming?

Yes, variable names in BNF variable declarations must follow certain rules, such as starting with a letter or underscore, not containing any special characters (except underscore), and not being a reserved keyword in C programming. Failure to follow these rules can result in syntax errors.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top