Browsable ANSI and K+R C Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: c/gnu/cordy-malton/extracted
Source used for this grammar: Andrew J. Malton, James R. Cordy, TXL Base Grammar for ANSI and K+R C, February 2002

Summary

Syntax

C_compilation_unit ::=
	declaration_or_function_definition*
constant ::=
	number
	float
	hex
	long
	SP dotfloat
	charlit
	string
string ::=
	stringlit+
expression ::=
	assignment_expression+
constant_expression ::=
	conditional_expression
assignment_expression ::=
	conditional_expression assignment_operation?
assignment_operation ::=
	assignment_operator assignment_expression
assignment_operator ::=
	"="
	"*="
	"/="
	"%="
	"+="
	"-="
	">>="
	"<<="
	"&="
	"^="
	"|="
conditional_expression ::=
	binary_expression conditional_operation?
conditional_operation ::=
	"?" expression ":" conditional_expression
binary_expression ::=
	unary_expression
	binary_expression binary_operator unary_expression
binary_operator ::=
	"+"
	"-"
	"*"
	"/"
	"%"
	"=="
	"!="
	"<"
	">"
	"<="
	">="
	"||"
	"&&"
	"|"
	"^"
	"&"
	"<<"
	">>"
unary_expression ::=
	postfix_expression
	unary_operator SPOFF unary_expression SPON
	"(" type_name ")" unary_expression
	sizeof_expression
sizeof_expression ::=
	"sizeof" "(" type_name ")"
	"sizeof" unary_expression
unary_operator ::=
	"*"
	"&"
	"+"
	"-"
	"!"
	"~"
	"++"
	"--"
postfix_expression ::=
	reference
	nonreference
reference ::=
	reference_id
	reference_expression
reference_id ::=
	id
reference_expression ::=
	unannotated_reference_base postfix_extension*
unannotated_reference_base ::=
	reference_id
	nonreferential_primary
nonreference ::=
	nonreferential_primary postfix_extension*
nonreferential_primary ::=
	constant
	string
	"(" expression ")"
postfix_extension ::=
	"[" expression "]"
	"(" expression? ")"
	"." id
	"->" id
	"++"
	"--"
declaration ::=
	declaration_body semi
	preprocessor
declaration_body ::=
	decl_specifiers init_declarator+
	enum_specifier
	struct_or_union_specifier
decl_specifiers ::=
	decl_qualifier* type_specifier? decl_qualifier*
struct_or_union_specifier ::=
	struct_or_union tagged_reference_id? "{" (IN NL) member_declaration* EX "}"
	struct_or_union tagged_reference_id
tagged_reference_id ::=
	reference_id
member_declaration ::=
	decl_specifiers member_declarator+ semi
	preprocessor
member_declarator ::=
	declarator bitfieldsize?
	bitfieldsize
bitfieldsize ::=
	":" constant_expression
decl_qualifier ::=
	sc_specifier
	cv_qualifier
	type_qualifier
sc_specifier ::=
	"auto"
	"register"
	"static"
	"extern"
	"typedef"
type_specifier ::=
	simple_type_name
	enum_specifier
	struct_or_union_specifier
type_qualifier ::=
	"long"
	"short"
	"signed"
	"unsigned"
simple_type_name ::=
	"char"
	"int"
	"void"
	"float"
	"double"
	type_id
type_id ::=
	reference_id
struct_or_union ::=
	"struct"
	"union"
enum_specifier ::=
	"enum" tagged_reference_id? "{" {enumerator ","}* "}"
	"enum" tagged_reference_id
enumerator ::=
	reference_id enumerator_value?
enumerator_value ::=
	"=" constant_expression
init_declarator ::=
	declarator initialization?
declarator ::=
	ptr_operator* base_declarator SPON declarator_extension*
base_declarator ::=
	reference_id
	"(" declarator ")"
declarator_extension ::=
	function_declarator_extension
	array_declarator_extension
function_declarator_extension ::=
	"(" argument_declaration_list ")" cv_qualifier*
array_declarator_extension ::=
	"[" constant_expression? "]"
ptr_operator ::=
	"*" cv_qualifier* SPOFF
cv_qualifier ::=
	"const"
	"volatile"
type_name ::=
	type_specifiers abstract_declarator?
type_specifiers ::=
	type_qualifier+ type_specifier? type_qualifier*
	type_specifier type_qualifier*
abstract_declarator ::=
	ptr_operator+ declarator_extension*
	"(" abstract_declarator ")" declarator_extension*
argument_declaration_list ::=
	{argument_declaration ","}*
argument_declaration ::=
	decl_specifiers argument_declarator?
	"..."
argument_declarator ::=
	declarator
	abstract_declarator
initialization ::=
	"=" initializer
	"(" constant_expression ")"
initializer ::=
	expression
	NL "{" IN {initializer ","}* ","? EX "}"
statement ::=
	label* unlabeled_statement
	preprocessor
label ::=
	label_id ":"
	EX SP SP "case" constant_expression ":" (IN NL)
	EX SP SP "default" ":" (IN NL)
label_id ::=
	id
unlabeled_statement ::=
	expression_statement
	if_statement
	for_statement
	while_statement
	switch_statement
	do_statement
	null_statement
	jump_statement
	compound_statement
null_statement ::=
	semi
compound_statement ::=
	"{" (IN NL) compound_statement_body "}" ";"? NL
compound_statement_body ::=
	statement* EX
	declaration compound_statement_body
expression_statement ::=
	expression semi
if_statement ::=
	"if" "(" expression ")" statement else_statement?
switch_statement ::=
	"switch" "(" expression ")" statement
else_statement ::=
	"else" statement
while_statement ::=
	"while" "(" expression ")" statement
do_statement ::=
	"do" statement "while" "(" expression ")" semi
for_statement ::=
	"for" "(" expression? ";" expression? ";" expression? ")" statement
jump_statement ::=
	"goto" label_id semi
	"continue" semi
	"break" semi
	"return" expression? semi
declaration_or_function_definition ::=
	declaration
	function_definition
function_definition ::=
	NL decl_specifiers declarator KR_parameter_decls? compound_statement NL
KR_parameter_decls ::=
	(NL IN) declaration+ EX
semi ::=
	";" NL
program ::=
	C_compilation_unit
preprocessor ::=
	"#define" id "(" id+ ")" expression NL
	"#define" id expression NL
	EX "#else" (IN NL)
	EX "#endif" (NL NL)
	NL "#if" expression (IN NL)
	NL "#ifdef" id (IN NL)
	NL "#ifndef" id (IN NL)
	"#ident" stringlit NL
	"#include" stringlit NL
	"#include" "<" SPOFF filepath ">" SPON NL
	"#line" integernumber stringlit? NL
	"#undef" id NL
	"#LINK" stringlit NL
filepath ::=
	file_id slash_fileid*
file_id ::=
	id
	key
slash_fileid ::=
	slash file_id
slash ::=
	"/"
	"\"
	"."
	":"

GrammarLabMaintained by Dr. Vadim Zaytsev a.k.a. @grammarware. Last updated in September 2015. []