C BNF 語法

translation-unit:
	external-declaration
	translation-unit external-declaration

external-declaration:
	function-definition
	declaration

function-definition:
	declaration-specifiers(opt) declarator declaration-list(opt) compound-statement

declaration:
	declaration-specifiers init-declarator-list(opt);

declaration-list:
	declaration
	declaration-list declaration

declaration_specifiers:
	storage-class-specifier declaration-specifiers(opt)
	type-specifier declaration-specifiers(opt)
	type-qualifier declaration-specifiers(opt)

storage-class-specifier:  one of
	auto  register  static  extern  typedef

type-specifier:  one of
	void   char  short  int  long  float  double  signed
	unsigned  struct-of-union-specifier  enum-specifier  typedef-name

type-qualifier:  one of
	const  volatile

struct-or-union-specifier:
	struct-or-union identifier(opt)  {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-list(opt)
	type-qualifier specifier-qualifier-list(opt)

struct-declarator-list:
	struct-declarator
	struct-declarator-list , struct-declarator

struct-declarator:
	declarator
	declarator(opt) : constant-expression

enum-specifier:
	enum identifier(opt) { enumerator-list }
	enum identifier

enumerator-list:
	enumerator
	enumerator-list , enumerator

enumerator:
	identifier
	identifier = constant-expression

declarator:
	pointer(opt) direct-declarator

direct-declarator:
	identifier
	(declarator)
	direct-declarator [ constant-expression(opt) ]
	direct-declarator ( parameter-type-list )
	direct-declarator ( identifier-list(opt) )

pointer:
	* type-qualifier-list(opt)
	* type-qualifier-list(opt) pointer

type-qualifier-list:
	type-qualifier
	type-qualifier-list type-qualifier

parameter-type-list:
	parameter-list
	parameter-list , ...

parameter-list:
	parameter-declaration
	parameter-list , parameter-declaration

parameter-declaration:
	declaration-specifiers declarator
	declaration-specifiers abstract-declarator(opt)

identifier-list:
	identifier
	identifier-list , identifier

initializer:
	assignment-expression
	{ initializer-list }
	{ initializer-list , }

initializer-list:
	initializer
	initializer-list , initializer

type-name:
	specifier-qualifier-list abstract-declarator(opt)

abstract-declarator:
	pointer
	pointer(opt) direct-abstract-declarator

direct-abstract-declarator:
	( abstract-declarator )
	direct-abstract-declarator(opt) [ constant-expression(opt) ]
	direct-abstract-declarator(opt) ( parameter-type-list(opt) )

typedef-name:
	identifier

statement:
	labeled-statement
	expression-statement
	compound-statement
	selection-statement
	iteration-statement
	jump-statement

labeled-statement:
	identifier : statement
	case constant-expression : statement
	default : statement

expression-statement:
	expression(opt);

compound-statement:
	{ declaration-list(oopt) statement-list(opt) }

statement-list:
	statement
	statement-list statement

selection-statement:
	if ( expression ) statement
	if ( expression ) statement else statement
	switch ( expression ) statement

iteration-statement:
	while ( expression ) statement
	do statement while ( expression ) ;
	for ( expression(opt) ; expression(opt) ; expression(opt) ) statement

jump-statement:
	goto identifier;
	continue;
	break;
	return expression(opt);

expression:
	assignment-expression
	expression , assignment-expression

assignment-expression:
	conditional-expression
	unary-expression assignment-operator assignment-expression

assignment-operator:  one of 
	=  *=  /=  %=  +=  -=  <<=  >>=  &=  ^=  |=

conditional-expression:
	logical-OR-expression
	logical-OR-expression ? expression : conditional-expression

constant-expression:
	conditional-expression

logical-OR-expression:
	logical-AND-expression
	logical-OR-expression || logical-AND-expression

logical-AND-expression:
	inclusive-OR-expression
	logical-AND-expression && inclusive-OR-expression

inclusive-OR-expression:
	exclusive-OR-expression
	inclusive-OR-expression | exclusive-OR-expression

exclusive-OR-expression:
	AND-expression
	exclusive-OR-expression ^ AND-expression

AND-expression:
	equality-expression
	AND-expression & equality-expression

equality-expression:
	relational-expression 
	equality-expression == relational-expression
	equality-expression != relational-expression

relational-expression:
	shift-expression
	relational-expression < shift-expression
	relational-expression > shift-expression
	relational-expression <= shift-expression
	relational-expression <= shift-expression

shift-expression:
	additive-expression
	shift-expression << additive-expression
	shift-expression >> additive-expression

additive-expression:
	multiplicative-expression
	additive-expression + multiplicative-expression
	additive-expression - multiplicative-expression

multiplicative-expression:
	cast-expression
	multiplicative-expression * cast-expression
	multiplicative-expression / cast-expression
	multiplicative-expression % cast-expression

cast-expression:
	unary-expression
	( type-name ) cast-expression

unary-expression:
	postfix-expression
	++ unary-expression
	-- unary-expression
	unary-operator cast-expression
	sizeof unary-expression
	sizeof ( type-name )

unary-operator:  one of 
	&  *  +  -  ~  !

postfix-expression:
	primary-expression
	postfix-expression [ expression ]
	postfix-expression ( argument-expression-list(opt) )
	postfix-expression . identifier
	postfix-expression -> identifier
	postfix-expression ++
	postfix-expression --

primary-expression:
	identifier
	constant
	string
	( expression )

argument-expression-list:
	assignment-expression
	argument-expression-list , assignment-expression

constant:
	integer-constant
	character-constant
	floating-constant
	enumeration-constant

control-line:
	# define identifier token-sequence
	# define identifier ( identifier , ... , identifier ) token-sequence
	# undef identifier
	# include <filename>
	# include "filename"
	# include token-sequence
	# line constant "filename"
	# line constant
	# error token-sequence(opt)
	# pragma token-sequence(opt)
	#
	preprocessor-conditional

preprocessor-conditional:
	if-line text elif-parts else-part(opt) # endif

if-line:
	# if constant-expression
	# ifdef identifier
	# ifndef identifier

elif-parts:
	elif-line text
	elif-parts(opt)
	
elif-line:
	# elif constant-expression

else-part:
	else-line text

else-line:
	# else

程式設計課程 首頁

製作日期: 09/13/2008 by 丁培毅 (Pei-yih Ting)
E-mail: pyting@mail.ntou.edu.tw TEL: 02 24622192x6615
海洋大學 電機資訊學院 資訊工程學系 Lagoon