简体   繁体   中英

How do I read abstract programming information

I had a test yesterday and I found myself having to understand something I had no knowledge about.

Please see the following page:

declaration-list:
  declaration
  declaration-list declaration 
declaration-specifiers:
  storage-class-specifier declaration-specifiersopt
  type-specifier declaration-specifiersopt
  type-qualifier declaration-specifiersopt 
storage-class specifier: one of
  auto register static extern typedef 
type specifier: one of
  void char short int long float double signed
  unsigned struct-or-union-specifier enum-specifier typedef-name 
type-qualifier: one of
  const volatile 
struct-or-union-specifier:
  struct-or-union identifieropt { struct-declaration-list }
  struct-or-union identifier 
struct-or-union: one of
  struct union 
struct-declaration-list:
  struct declaration
  struct-declaration-list struct declaration 
init-declarator-list:
  init-declarator
  init-declarator-list, init-declarator 
init-declarator:
  declarator
  declarator = initializer 
struct-declaration:
  specifier-qualifier-list struct-declarator-list; 
specifier-qualifier-list:
  type-specifier specifier-qualifier-listopt
  type-qualifier specifier-qualifier-listopt 
struct-declarator-list:
  struct-declarator
  struct-declarator-list , struct-declarator 
struct-declarator:
  declarator
  declaratoropt : constant-expression 
enum-specifier:
  enum identifieropt { enumerator-list }
  enum identifier 

This is from the book "The C Programming Language" by Brian W. Kernighan Dennis M. Ritchie but I saw it on the internet and on many other books.

I tried googling it but frankly, I have no Idea how is it called so I couldn't find any relevant information. Our professors have never tought us to read it and I now believe it's extremely important to know how to read it.

I'm looking for reference to guides or a short explanation on how to approach it.

I apologize if that's a repost but I couldn't find any related post.

The first definition...

declaration-list:
    declaration
    declaration-list declaration

...can be read as: "A declaration-list is either a declaration or a declaration-list followed by a declaration."

This recursive definition allows for the following as examples of a declaration-list:

  • declaration
  • declaration declaration
  • declaration declaration declaration
  • etc.

The full grammar definition will contain additional rules describing what makes up a valid declaration, and so on.

It is called the syntax notation. It is described in C11 6.1 :

  1. In the syntax notation used in this clause, syntactic categories (nonterminals) are indicated by italic type , and literal words and character set members (terminals) by bold type . A colon (:) following a nonterminal introduces its definition. Alternative definitions are listed on separate lines, except when prefaced by the words ''one of''. An optional symbol is indicated by the subscript opt , so that

    { expression opt }

    indicates an optional expression enclosed in braces.

So, to take the first non-terminal as an example:

declaration-list :
declaration
declaration-list declaration

means that a declaration-list is a single declaration , or alternatively a declaration-list followed by a single declaration (ie it would define itself recursively).

By the way, these declaration-lists are the old and long obsolete way how function parameters were typed in C:

some_function(a, b)
    int a;
    double b;
{
    printf("a = %d, b = %f\n", a, b);
}

The int a; double b; int a; double b; part is the optional declaration-list , and double b; is a single declaration . So the declaration-list is defined recursively as being a declaration list that is of one declaration, or a declaration list that is followed by a declaration.


To actually internalize meaning to these syntactic constructs, you'd better read the C11 standard draft n1570 online or the PDF version . Note also, that the grammar just gives the syntax of a C program, but the constraints and semantics of the C program are written in text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM