------------------------------------------------------------------------------
MC logo
Plain C CFG
[^] Syntax
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[ECFG for Tucker and Noonan's Clite Language] [Plain C CFG] [Abstract Syntax for for Tucker and Noonan's Clite Language] [Derivation Problem] [Regular Expression Problems]
This a grammar for plain C reformatted from this Yacc file. Literal tokens are in bold code font, other tokens are bold italic.
TranslationUnitExternalDecl
|TranslationUnit ExternalDecl
ExternalDeclFunctionDefinition
|Decl
FunctionDefinitionDeclSpecs Declarator DeclList CompoundStat
|Declarator DeclList CompoundStat
|DeclSpecs Declarator CompoundStat
|Declarator CompoundStat
DeclDeclSpecs InitDeclaratorList ;
|DeclSpecs ;
DeclListDecl
|DeclList Decl
DeclSpecsStorageClassSpec DeclSpecs
|StorageClassSpec
|TypeSpec DeclSpecs
|TypeSpec
|TypeQualifier DeclSpecs
|TypeQualifier
StorageClassSpecauto | register | static | extern | typedef
TypeSpecvoid | char | short | int | long | float | double | signed | unsigned
|StructOrUnionSpec | EnumSpec | TypedefName
TypeQualifierconst | volatile
StructOrUnionSpecStructOrUnion Id { StructDeclList }
|StructOrUnion { StructDeclList }
|StructOrUnion Id
StructOrUnionstruct | union
StructDeclListStructDecl
|StructDeclList StructDecl
InitDeclaratorListInitDeclarator
|InitDeclaratorList , InitDeclarator
InitDeclaratorDeclarator
|Declarator = Initializer
StructDeclSpecQualifierList StructDeclaratorList ;
SpecQualifierListTypeSpec SpecQualifierList
|TypeSpec
|TypeQualifier SpecQualifierList
|TypeQualifier
StructDeclaratorListStructDeclarator
|StructDeclaratorList , StructDeclarator
StructDeclaratorDeclarator
|Declarator : ConstExp
|: ConstExp
EnumSpecenum Id { EnumeratorList }
|enum { EnumeratorList }
|enum Id
EnumeratorListEnumerator
|EnumeratorList , Enumerator
EnumeratorId
|Id = ConstExp
DeclaratorPointer DirectDeclarator
|DirectDeclarator
DirectDeclaratorId
|( Declarator )
|DirectDeclarator [ ConstExp ]
|DirectDeclarator [ ]
|DirectDeclarator ( ParamTypeList )
|DirectDeclarator ( IdList )
|DirectDeclarator ( )
Pointer* TypeQualifierList
|*
|* TypeQualifierList Pointer
|* Pointer
TypeQualifierListTypeQualifier
|TypeQualifierList TypeQualifier
ParamTypeListParamList
|ParamList , ...
ParamListParamDecl
|ParamList , ParamDecl
ParamDeclDeclSpecs Declarator
|DeclSpecs AbstractDeclarator
|DeclSpecs
IdListId
|IdList , Id
InitializerAssignmentExp
|{ InitializerList }
|{ InitializerList , }
InitializerListInitializer
|InitializerList , Initializer
TypeNameSpecQualifierList AbstractDeclarator
|SpecQualifierList
AbstractDeclaratorPointer
|Pointer DirectAbstractDeclarator
|DirectAbstractDeclarator
DirectAbstractDeclarator( AbstractDeclarator )
|DirectAbstractDeclarator [ ConstExp ]
|[ ConstExp ]
|DirectAbstractDeclarator [ ]
|[ ]
|DirectAbstractDeclarator ( ParamTypeList )
|( ParamTypeList )
|DirectAbstractDeclarator ( )
|( )
TypedefNameId
StatLabeledStat | ExpStat | CompoundStat | SelectionStat | IterationStat | JumpStat
LabeledStatId : Stat
|case ConstExp : Stat
|default : Stat
ExpStatExp ;
|;
CompoundStat{ DeclList StatList }
|{ StatList }
|{ DeclList }
|{ }
StatListStat
|StatList Stat
SelectionStatif ( Exp ) Stat
|if ( Exp ) Stat else Stat
|switch ( Exp ) Stat
IterationStatwhile ( Exp ) Stat
|do Stat while ( Exp ) ;
|for ( Exp ; Exp ; Exp ) Stat
|for ( Exp ; Exp ; ) Stat
|for ( Exp ; ; Exp ) Stat
|for ( Exp ; ; ) Stat
|for ( ; Exp ; Exp ) Stat
|for ( ; Exp ; ) Stat
|for ( ; ; Exp ) Stat
|for ( ; ; ) Stat
JumpStatJumpSpec ;
JumpSpecgoto Id | continue | break | return Exp | return
ExpAssignmentExp
|Exp , AssignmentExp
AssignmentExpConditionalExp
|UnaryExp AssignmentOperator AssignmentExp
AssignmentOperator= | *= | /= | %= | += | -= | <<= | >>= | &= | ^= | |=
ConditionalExpLogicalOrExp
|LogicalOrExp ? Exp : ConditionalExp
ConstExpConditionalExp
LogicalOrExpLogicalAndExp
|LogicalOrExp || LogicalAndExp
LogicalAndExpInclusiveOrExp
|LogicalAndExp && InclusiveOrExp
InclusiveOrExpExclusiveOrExp
|InclusiveOrExp | ExclusiveOrExp
ExclusiveOrExpAndExp
|ExclusiveOrExp ^ AndExp
AndExpEqualityExp
|AndExp & EqualityExp
EqualityExpRelationalExp
|EqualityExp == RelationalExp
|EqualityExp != RelationalExp
RelationalExpShiftExpression
|RelationalExp < ShiftExpression
|RelationalExp > ShiftExpression
|RelationalExp <= ShiftExpression
|RelationalExp >= ShiftExpression
ShiftExpressionAdditiveExp
|ShiftExpression << AdditiveExp
|ShiftExpression >> AdditiveExp
AdditiveExpMultExp
|AdditiveExp + MultExp
|AdditiveExp - MultExp
MultExpCastExp
|MultExp * CastExp
|MultExp / CastExp
|MultExp % CastExp
CastExpUnaryExp
|( TypeName ) CastExp
UnaryExpPostfixExp
|++ UnaryExp
|-- UnaryExp
|UnaryOperator CastExp
|sizeof UnaryExp
|sizeof ( TypeName )
UnaryOperator& | * | + | - | ~ | !
PostfixExpPrimaryExp
|PostfixExp [ Exp ]
|PostfixExp ( ArgumentExpList )
|PostfixExp ( )
|PostfixExp . Id
|PostfixExp -> Id
|PostfixExp ++
|PostfixExp --
PrimaryExpId | IntConst | FloatConst | EnumerationConst | StringConst | ( Exp )
ArgumentExpListAssignmentExp
|ArgumentExpList , AssignmentExp

Nothing Helps

In the above grammar, the replacement is always a non-empty string. Right sides may be empty, usually written as ε. A rule can then take the form Whatever → ε, meaning that the non-terminal Whatever is allowed to just disappear during a derivation. This can simplify certain things. For instance, the C for statement may omit any of the initialization, test, or increment parts of the header. In the above, this is represented by giving eight versions of the for header, with each combination of presence or omission. But using an epsilon rule, we can write this more concisely.
IterationStatwhile ( Exp ) Stat
|do Stat while ( Exp ) ;
|for ( OptExp ; OptExp ; OptExp ) Stat
OptExpExp | ε
Epsilon may help in other places as well, but this seems the most dramatic.