Browsable ANSI and K+R C Grammar

CC-BY

Grammar extracted by Vadim Zaytsev, see the Grammar Zoo entry for details: c/gnu/malton-dahn/extracted
Source used for this grammar: Andrew J. Malton, James R. Cordy, Christopher Dahn, TXL Base Grammar for ANSI and K+R C, with GNU C extensions and overrides to preserve comments, May 2004

Summary

Syntax

C_compilation_unit ::=
	declaration_or_function_definition*
constant ::=
	number
	float
	hex
	long
	SP dotfloat
	charlit
	string
constant ::=
	gnu_long_int
	gnu_long_int_string+
	hexfloat
string ::=
	stringlit+
expression ::=
	assignment_expression+
expression ::=
	comment_NL* "..."
constant_expression ::=
	conditional_expression
constant_expression ::=
	restrict_qualifier
assignment_expression ::=
	conditional_expression assignment_operation?
assignment_expression ::=
	conditional_expression
	conditional_expression assignment_operation
assignment_operation ::=
	assignment_operator assignment_expression
assignment_operator ::=
	"="
	"*="
	"/="
	"%="
	"+="
	"-="
	">>="
	"<<="
	"&="
	"^="
	"|="
conditional_expression ::=
	binary_expression conditional_operation?
conditional_expression ::=
	binary_expression conditional_operation*
conditional_operation ::=
	"?" expression ":" conditional_expression
conditional_operation ::=
	"?" expression? ":" binary_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
unary_expression ::=
	alignof_expression
sizeof_expression ::=
	"sizeof" "(" type_name ")"
	"sizeof" unary_expression
sizeof_expression ::=
	"sizeof" unary_expression
	"sizeof" "(" type_name ")"
unary_operator ::=
	"*"
	"&"
	"+"
	"-"
	"!"
	"~"
	"++"
	"--"
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 ")"
nonreferential_primary ::=
	constant
	string
	"(" expression ")"
	compound_statement_expression
	compound_literal
	extension_specifier compound_statement_expression
	extension_specifier "(" expression ")"
postfix_extension ::=
	"[" expression "]"
	"(" expression? ")"
	"." id
	"->" id
	"++"
	"--"
postfix_extension ::=
	"[" expression "]"
	"(" {argument_expression ","}* ")"
	"." id
	"->" id
	"++"
	"--"
declaration ::=
	declaration_body semi
	preprocessor
declaration ::=
	comment_NL
declaration ::=
	null_declaration
	declaration_body semi
	preprocessor
declaration_body ::=
	decl_specifiers init_declarator+
	enum_specifier
	struct_or_union_specifier
declaration_body ::=
	decl_specifiers init_declarator+
	decl_qualifier* struct_or_union_specifier
	decl_qualifier* enum_specifier attribute_spec*
	local_label declarator+
	asm_spec
decl_specifiers ::=
	decl_qualifier* type_specifier? decl_qualifier*
decl_specifiers ::=
	decl_qualifier* type_specifier decl_qualifier*
	decl_qualifier*
struct_or_union_specifier ::=
	struct_or_union tagged_reference_id? "{" IN NL member_declaration* EX "}"
	struct_or_union tagged_reference_id
struct_or_union_specifier ::=
	struct_or_union attribute_spec* tagged_reference_id? "{" IN NL member_declaration* EX "}" attribute_spec*
	struct_or_union attribute_spec* tagged_reference_id
tagged_reference_id ::=
	reference_id
member_declaration ::=
	decl_specifiers member_declarator+ semi
	preprocessor
member_declaration ::=
	comment_NL
member_declaration ::=
	decl_specifiers member_declarator+ semi
	decl_qualifier* struct_or_union_specifier semi
	semi
	preprocessor
member_declarator ::=
	declarator bitfieldsize?
	bitfieldsize
member_declarator ::=
	declarator bitfieldsize? attributes_or_asm*
	bitfieldsize
bitfieldsize ::=
	":" constant_expression
decl_qualifier ::=
	sc_specifier
	cv_qualifier
	type_qualifier
decl_qualifier ::=
	sc_specifier
	type_qualifier
	attribute_spec
	extension_specifier
sc_specifier ::=
	"auto"
	"register"
	"static"
	"extern"
	"typedef"
type_specifier ::=
	simple_type_name
	enum_specifier
	struct_or_union_specifier
type_specifier ::=
	typeof_expression
	complex_specifier
type_qualifier ::=
	"long"
	"short"
	"signed"
	"unsigned"
type_qualifier ::=
	bit_qualifier
	inline_qualifier
	cv_qualifier
	restrict_qualifier
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
enum_specifier ::=
	"enum" tagged_reference_id
	"enum" tagged_reference_id? "{" {enumerator ","}* ","? "}"
enumerator ::=
	reference_id enumerator_value?
enumerator_value ::=
	"=" constant_expression
init_declarator ::=
	declarator initialization?
init_declarator ::=
	declarator initialization? attributes_or_asm*
declarator ::=
	ptr_operator* base_declarator SPON declarator_extension*
declarator ::=
	attribute_spec* ptr_operator* base_declarator declarator_extension* attribute_spec*
base_declarator ::=
	reference_id
	"(" declarator ")"
base_declarator ::=
	attribute_spec* reference_id
	attribute_spec* "(" 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
ptr_operator ::=
	"*" ptr_qualifier*
cv_qualifier ::=
	"const"
	"volatile"
cv_qualifier ::=
	"const"
	"volatile"
	"__const"
	"__const__"
	"const__"
	"__volatile__"
	"__volatile"
	"volatile__"
type_name ::=
	type_specifiers abstract_declarator?
type_specifiers ::=
	type_qualifier+ type_specifier? type_qualifier*
	type_specifier type_qualifier*
type_specifiers ::=
	type_qualifier* type_specifier type_qualifier*
	type_qualifier*
abstract_declarator ::=
	ptr_operator+ declarator_extension*
	"(" abstract_declarator ")" declarator_extension*
abstract_declarator ::=
	ptr_operator+ declarator_extension*
	array_declarator_extension*
	"(" abstract_declarator ")" declarator_extension*
argument_declaration_list ::=
	{argument_declaration ","}*
argument_declaration_list ::=
	"..." comment_NL*
argument_declaration ::=
	decl_specifiers argument_declarator?
	"..."
argument_declaration ::=
	comment_NL* "..."
argument_declaration ::=
	"..." comment_NL*
argument_declarator ::=
	declarator
	abstract_declarator
initialization ::=
	"=" initializer
	"(" constant_expression ")"
initialization ::=
	comment_NL* "..."
initializer ::=
	expression
	NL "{" IN {initializer ","}* ","? EX "}"
initializer ::=
	comment_NL* "..."
initializer ::=
	assignment_expression
	NL "{" IN {designated_initializer ","}* ","? EX "}"
statement ::=
	label* unlabeled_statement
	preprocessor
statement ::=
	comment_NL
statement ::=
	label
label ::=
	label_id ":"
	EX SP SP "case" constant_expression ":" IN NL
	EX SP SP "default" ":" IN NL
label ::=
	label_id ":" attribute_spec*
	EX SP SP "case" constant_expression ":" IN NL
	EX SP SP "case" constant_expression SP "..." SP 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
unlabeled_statement ::=
	asm_statement
null_statement ::=
	semi
compound_statement ::=
	"{" IN NL compound_statement_body "}" ";"? NL
compound_statement ::=
	comment_NL* "..."
compound_statement ::=
	"{" NL compound_statement_body* "}" ";"? NL
compound_statement_body ::=
	statement* EX
	declaration compound_statement_body
compound_statement_body ::=
	IN declaration EX
	IN function_definition EX
	IN statement EX
expression_statement ::=
	expression semi
if_statement ::=
	"if" "(" expression ")" statement else_statement?
if_statement ::=
	"if" "(" expression ")" comment_NL* statement else_statement?
switch_statement ::=
	"switch" "(" expression ")" statement
switch_statement ::=
	"switch" "(" expression ")" comment_NL* statement
else_statement ::=
	"else" statement
while_statement ::=
	"while" "(" expression ")" statement
do_statement ::=
	"do" statement "while" "(" expression ")" semi
for_statement ::=
	"for" "(" expression? ";" expression? ";" expression? ")" statement
for_statement ::=
	"for" "(" declaration_body ";" expression? ";" expression? ")" statement
jump_statement ::=
	"goto" label_id semi
	"continue" semi
	"break" semi
	"return" expression? semi
jump_statement ::=
	"goto" ptr_operator 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
preprocessor ::=
	EX "#endif" comment_NL? NL
	"..."
preprocessor ::=
	"..." comment_NL?
preprocessor ::=
	"#" integernumber string integernumber* NL
	"#pragma" SPON id* "(" {id ","}* ")" NL
filepath ::=
	file_id slash_fileid*
file_id ::=
	id
	key
slash_fileid ::=
	slash file_id
slash ::=
	"/"
	"\"
	"."
	":"
comment_NL ::=
	comment NL
null_declaration ::=
	semi
local_label ::=
	"__label__"
ptr_qualifier ::=
	restrict_qualifier
	cv_qualifier
restrict_qualifier ::=
	"__restrict"
	"restrict__"
	"restrict"
	"__restrict__"
designator ::=
	array_designator
	struct_or_union_designator
array_designator ::=
	"[" constant_expression "]" postfix_extension*
	"[" constant_expression SP "..." SP constant_expression "]" postfix_extension*
struct_or_union_designator ::=
	"."? id postfix_extension*
designator_assignment ::=
	":"
	"="
extension_specifier ::=
	"__extension__"
bit_qualifier ::=
	"long"
	"short"
	"signed"
	"unsigned"
	"__signed__"
	"__signed"
inline_qualifier ::=
	"__inline"
	"__inline__"
	"inline__"
	"inline"
attributes_or_asm ::=
	attribute_spec
	asm_spec
attribute_spec ::=
	"__attribute__" "('(" {attribute_extension ","}* ")')"
attribute_extension ::=
	attribute_alias "(" string ")"
	attribute_aligned
	attribute_aligned "(" tagged_attribute_number ")"
	attribute_aligned "(" alignof_expression ")"
	attribute_always_inline
	attribute_cdecl
	attribute_cleanup "(" tagged_attribute_id ")"
	attribute_const
	attribute_constructor
	attribute_deprecated
	attribute_destructor
	attribute_dllexport
	attribute_dllimport
	attribute_eightbit
	attribute_exception "(" exception_function {exception_arg ","}* ")"
	attribute_far
	attribute_funvector
	attribute_format "(" format_archetype "," tagged_attribute_number "," tagged_attribute_number ")"
	attribute_format_arg "(" tagged_attribute_number ")"
	attribute_interrupt
	attribute_interrupt "(" string ")"
	attribute_interrupt_handler
	attribute_interrupt_handler "(" string ")"
	attribute_longcall
	attribute_long_call
	attribute_malloc
	attribute_may_alias
	attribute_mode "(" tagged_attribute_id ")"
	attribute_model "(" attribute_model_name ")"
	attribute_naked
	attribute_near
	attribute_no_check_mem
	attribute_no_instrument
	attribute_nocommon
	attribute_noinline
	attribute_nonnull
	attribute_nonnull "(" tagged_attribute_number+ ")"
	attribute_noreturn
	attribute_nothrow
	attribute_packed
	attribute_pure
	attribute_regparm "(" tagged_attribute_number ")"
	attribute_section "(" string ")"
	attribute_shared
	attribute_short_call
	attribute_signal "(" string ")"
	attribute_sp_switch "(" string ")"
	attribute_stdcall
	attribute_tiny
	attribute_tls_model "(" tls_model_type ")"
	attribute_trans_union
	attribute_trap_exit "(" tagged_attribute_number ")"
	attribute_used
	attribute_unused
	attribute_vector_size "(" tagged_attribute_number ")"
	attribute_visibility "(" visibility_type ")"
	attribute_weak
tagged_attribute_id ::=
	id
tagged_attribute_number ::=
	number
attribute_near ::=
	"__near__"
	"__near"
	"near__"
	"near"
attribute_far ::=
	"__far__"
	"__far"
	"far__"
	"far"
attribute_visibility ::=
	"__visibility__"
	"__visibility"
	"visibility__"
	"visibility"
visibility_type ::=
	"default"
	"hidden"
	"protected"
	"internal"
attribute_nonnull ::=
	"__nonnull__"
	"__nonnull"
	"nonnull__"
	"nonnull"
attribute_nothrow ::=
	"__nothrow__"
	"__nothrow"
	"nothrow__"
	"nothrow"
attribute_may_alias ::=
	"__may_alias__"
	"__may_alias"
	"may_alias__"
	"may_alias"
attribute_tls_model ::=
	"__tls_model__"
	"__tls_model"
	"tls_model__"
	"tls_model"
tls_model_type ::=
	"global-dynamic"
	"local-dynamic"
	"initial-exec"
	"local-exec"
attribute_cleanup ::=
	"__cleanup__"
	"__cleanup"
	"cleanup__"
	"cleanup"
attribute_dllexport ::=
	"__dllexport__"
	"__dllexport"
	"dllexport__"
	"dllexport"
attribute_used ::=
	"__used__"
	"__used"
	"used__"
	"used"
attribute_always_inline ::=
	"__always_inline__"
	"__always_inline"
	"always_inline__"
	"always_inline"
attribute_noinline ::=
	"__noinline__"
	"__noinline"
	"noinline__"
	"noinline"
attribute_vector_size ::=
	"__vector_size__"
	"__vector_size"
	"vector_size__"
	"vector_size"
attribute_deprecated ::=
	"__deprecated__"
	"__deprecated"
	"deprecated__"
	"deprecated"
attribute_mode ::=
	"__mode__"
	"__mode"
	"mode__"
	"mode"
attribute_format ::=
	"__format__"
	"__format"
	"format__"
	"format"
attribute_format_arg ::=
	"__format_arg__"
	"__format_arg"
	"format_arg__"
	"format_arg"
attribute_aligned ::=
	"__aligned__"
	"__aligned"
	"aligned__"
	"aligned"
attribute_weak ::=
	"weak"
	"__weak__"
	"__weak"
	"weak__"
attribute_alias ::=
	"alias"
	"__alias__"
	"__alias"
	"alias__"
attribute_no_check_mem ::=
	"no_check_memory_usage"
	"__no_check_memory_usage__"
	"__no_check_memory_usage"
	"no_check_memory_usage__"
attribute_regparm ::=
	"regparm"
	"__regparm__"
	"__regparm"
	"regparm__"
attribute_stdcall ::=
	"stdcall"
	"__stdcall__"
	"__stdcall"
	"stdcall__"
attribute_cdecl ::=
	"cdecl"
	"__cdecl__"
	"__cdecl"
	"cdecl__"
attribute_longcall ::=
	"longcall"
	"__longcall__"
	"__longcall"
	"longcall__"
attribute_long_call ::=
	"long_call"
	"__long_call__"
	"__long_call"
	"long_call__"
attribute_short_call ::=
	"short_call"
	"__short_call__"
	"__short_call"
	"short_call__"
attribute_dllimport ::=
	"dllimport"
	"__dllimport__"
	"__dllimport"
	"dllimport__"
attribute_exception ::=
	"exception"
	"__exception__"
	"__exception"
	"exception__"
exception_function ::=
	stringlit* stringlit
	tagged_attribute_id
exception_arg ::=
	stringlit* stringlit
	tagged_attribute_id
attribute_funvector ::=
	"function_vector"
	"__function_vector__"
	"__function_vector"
	"function_vector__"
attribute_interrupt ::=
	"interrupt"
	"__interrupt__"
	"__interrupt"
	"interrupt__"
attribute_interrupt_handler ::=
	"interrupt_handler"
	"__interrupt_handler__"
	"__interrupt_handler"
	"interrupt_handler__"
attribute_sp_switch ::=
	"sp_switch"
	"__sp_switch__"
	"__sp_switch"
	"sp_switch__"
attribute_trap_exit ::=
	"trap_exit"
	"__trap_exit__"
	"__trap_exit"
	"trap_exit__"
attribute_eightbit ::=
	"eightbit_data"
	"__eightbit_data__"
	"__eightbit_data"
	"eightbit_data__"
attribute_tiny ::=
	"tiny_data"
	"__tiny_data__"
	"__tiny_data"
	"tiny_data__"
attribute_signal ::=
	"signal"
	"__signal__"
	"__signal"
	"signal__"
attribute_naked ::=
	"naked"
	"__naked__"
	"__naked"
	"naked__"
attribute_model ::=
	"model"
	"__model__"
	"__model"
	"model__"
attribute_model_name ::=
	"small"
	"__small__"
	"__small"
	"small__"
	"medium"
	"__medium__"
	"__medium"
	"medium__"
	"large"
	"__large__"
	"__large"
	"large__"
attribute_nocommon ::=
	"nocommon"
	"__nocommon__"
	"nocommon__"
	"__nocommon"
attribute_shared ::=
	"shared"
	"__shared__"
	"__shared"
	"shared__"
attribute_malloc ::=
	"malloc"
	"__malloc__"
	"__malloc"
	"malloc__"
format_archetype ::=
	"printf"
	"__printf__"
	"__printf"
	"printf__"
	"scanf"
	"__scanf__"
	"__scanf"
	"scanf__"
	"strftime"
	"__strftime__"
	"__strftime"
	"strftime__"
	"strfmon"
	"__strfmon__"
	"__strfmon"
	"strfmon__"
attribute_noreturn ::=
	"__noreturn__"
	"__noreturn"
	"noreturn__"
	"noreturn"
attribute_const ::=
	"__const__"
	"__const"
	"const__"
	"const"
attribute_pure ::=
	"__pure__"
	"__pure"
	"pure__"
	"pure"
attribute_trans_union ::=
	"__transparent_union__"
	"__transparent_union"
	"transparent_union__"
	"transparent_union"
attribute_unused ::=
	"__unused__"
	"__unused"
	"unused__"
	"unused"
attribute_packed ::=
	"__packed__"
	"__packed"
	"packed__"
	"packed"
attribute_no_instrument ::=
	"no_instrument_function"
	"__no_instrument_function__"
	"__no_instrument_function"
	"no_instrument_function__"
attribute_section ::=
	"section"
	"__section__"
	"__section"
	"section__"
attribute_constructor ::=
	"constructor"
	"__constructor__"
	"__constructor"
	"constructor__"
attribute_destructor ::=
	"destructor"
	"__destructor__"
	"__destructor"
	"destructor__"
asm_statement ::=
	asm_spec semi
asm_spec ::=
	asm_or_cv+ "(" asm_template asm_output_operands? asm_input_operands? asm_clobbered_registers? ")"
asm_or_cv ::=
	"__asm__"
	"asm"
	"asm__"
	"__asm"
	cv_qualifier
asm_template ::=
	string
asm_output_operands ::=
	":" {asm_operand ","}*
asm_input_operands ::=
	":" {asm_operand ","}*
asm_operand ::=
	string "(" expression ")"
asm_clobbered_registers ::=
	":" {string ","}*
compound_statement_expression ::=
	"(" compound_statement ")"
compound_literal ::=
	"(" struct_or_union_specifier ")" "{" {designated_initializer ","}* ","? "}"
	"(" compound_literal_array_type ")" "{" {designated_initializer ","}* ","? "}"
	"(" compound_literal ")"
compound_literal_array_type ::=
	type_specifiers compound_literal_array_declarator
compound_literal_array_declarator ::=
	ptr_operator* array_declarator_extension*
	"(" compound_literal_array_declarator ")" array_declarator_extension*
typeof_expression ::=
	typeof_specifier "(" simple_type_name ")"
	typeof_specifier "(" expression ")"
typeof_specifier ::=
	"__typeof"
	"typeof"
	"__typeof__"
	"typeof__"
complex_specifier ::=
	"_Complex" simple_type_or_qualifier*
simple_type_or_qualifier ::=
	simple_type_name
	type_qualifier
gnu_long_int ::=
	gnu_long_int_string
	gnu_long_int_charlit
gnu_long_int_string ::=
	"L" SPOFF string SPON
gnu_long_int_charlit ::=
	"L" SPOFF charlit SPON
hexfloat ::=
	extension_specifier hex
argument_expression ::=
	assignment_expression
	type_name
alignof_expression ::=
	"__alignof__" "(" unary_expression ")"
	"__alignof" "(" unary_expression ")"
	"__alignof__" "(" type_name ")"
	"__alignof" "(" type_name ")"
designated_initializer ::=
	assignment_expression
	designator* designator_assignment? assignment_expression
	designator* designator_assignment? NL "{" IN {designated_initializer ","}* ","? EX "}"

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