Академический Документы
Профессиональный Документы
Культура Документы
ANSI C
!
! The C programming language evolved at Bell Labs from a series of
! programming languages: 'CPL', 'BCPL', and then 'B'. As a result, C's
! development was a combined effort between Dennis Ritchie, Ken Thompson,
! and Martin Richards.
!
! C was designed for the creation and implementation of low-level systems
! such as operating systems, device drivers, firmware, etc... To realize
! this goal, the language contains the ability to perform operations
! directly on memory and has direct access to system pointers. While this
! gives an enormous amount of control and flexibility, it also makes C a
! professional programming language - not to be used by an inexperienced
! programmer.
!
! C (and later C++) quickly became the de facto standard for developing
! operating systems, applications and most other large projects. UNIX as
! well as Windows, Linux, and Mac-OS X were developed using this
! language (and its successors).
!
! More information is available at Dennis Ritchie's website:
!
http://cm.bell-labs.com/cm/cs/who/dmr/
!
! The C grammar is inherently ambigious and requires a large number of
! LALR(1) states to parse. As a result, the time required by the GOLD
! Parser Builder to compile this grammar is extensive.
!
! C is not a line-based grammar with the notable exception of compiler
! directives (which are preceeded by a '#' character). These are usually
not
! handled directly by the actual parser, but, rather, the pre-processor.
! Before the program is analyzed by the parser, C compilers scan the code
and
! act on these commands. The final C program is then passed to the
parser.
! ----------------------------------------------------------------------! ----------------------------------------------------------------------! This grammar does not contain the compiler directives.
!
! Note: This is an ad hoc version of the language. If there are any
flaws,
! please visit the contact page and tell me.
!
! SPECIAL THANKS TO:
!
BOB MEAGHER
!
MIKE WISDOM
!
VLADIMIR MOROZOV
!
TOM VAN DIJCK
!
! Modified 06/14/2002
!
* The correct definition for "return" was added. Thanks to Bob
Meagher.
!
* Added the missing rules for labels, storage specifications, etc...
!
which were left out. Thanks to Mike Wisdom for calling this to
!
my attention.
!
! Modified 06/21/2002
!
* I fixed an error in the grammar for declaring functions.
!
! Modified 06/15/2003
!
* Vladimir Morozov fixed an error for calling functions with no
parameters
!
! Modified 01/31/2004
!
* Tom van Dijck found a bug in the grammar concerning variable
!
initialization.
!
! Modified 04/26/2004
!
* Some errors in the grammar were fixed.
!
! Modified 01/19/2005
!
* The definition for comments was modified. In ANSI C, block comments
!
cannot be nested. As a result, they are defined using the
whitespace
!
terminal.
!
! Modified 03/28/2007
!
* The commented-out definition for non-nested comments was updated.
The
!
previous version did not work in all cases.
! -----------------------------------------------------------------------
"Name"
"Version"
"Author"
"About"
languages
= 'ANSI C'
= '1973'
= 'Dennis Ritchie, Ken Thompson, Martin Richards'
= 'C is one of the most common, and complex, programming
in use today.'
= {Digit} + [abcdefABCDEF]
= [01234567]
{Id Head}
{Id Tail}
= {Letter} + [_]
= {Id Head} + {Digit}
{String Ch}
{Char Ch}
= {Printable} - ["]
= {Printable} - ['']
DecLiteral
OctLiteral
HexLiteral
FloatLiteral
=
=
=
=
StringLiteral
CharLiteral
Id
[123456789]{digit}*
0{Oct Digit}*
0x{Hex Digit}+
{Digit}*'.'{Digit}+
! ===================================================================
! Comments
! ===================================================================
Comment Start = '/*'
Comment End
= '*/'
Comment Line = '//'
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
!
= {Printable} - [/]
= {Printable} - [*]
= {Whitespace}+
| '/*' ( {Non Asterisk} | '*' {Non Slash}? )*
'*/'
!=======================================================
<Decls> ::= <Decl> <Decls>
|
<Decl>
::=
|
|
|
|
|
|
<Func Decl>
<Func Proto>
<Struct Decl>
<Union Decl>
<Enum Decl>
<Var Decl>
<Typedef Decl>
! ===================================================================
! Function Declaration
! ===================================================================
<Func Proto> ::= <Func ID> '(' <Types> ')' ';'
| <Func ID> '(' <Params> ')' ';'
| <Func ID> '(' ')' ';'
<Func Decl>
<Params>
<Param>
<Types>
::= <Type>
',' <Types>
| <Type>
<Id List>
<Func ID>
::= <Type> ID
| ID
! ===================================================================
! Type Declaration
! ===================================================================
<Typedef Decl> ::= typedef <Type> ID ';'
<Struct Decl>
<Union Decl>
<Struct Def>
';'
';'
! ===================================================================
! Variable Declaration
! ===================================================================
<Var Decl>
<Var>
::= ID <Array>
| ID <Array> '=' <Op If>
<Array>
';'
';'
';'
<Mod>
::=
|
|
|
|
|
extern
static
register
auto
volatile
const
! ===================================================================
! Enumerations
! ===================================================================
<Enum Decl>
<Enum Def>
';'
<Enum Val>
::=
|
|
|
Id
Id '=' OctLiteral
Id '=' HexLiteral
Id '=' DecLiteral
! ===================================================================
! Types
! ===================================================================
<Type>
<Base>
::=
|
|
|
|
|
<Sign>
::= signed
| unsigned
|
<Scalar>
::=
|
|
|
|
|
|
|
|
<Sign> <Scalar>
struct Id
struct '{' <Struct Def> '}'
union Id
union '{' <Struct Def> '}'
enum Id
char
int
short
long
short int
long int
float
double
void
::=
|
|
|
|
|
|
<Var Decl>
Id ':'
!Label
if '(' <Expr> ')' <Stm>
if '(' <Expr> ')' <Then Stm> else <Stm>
while '(' <Expr> ')' <Stm>
for '(' <Arg> ';' <Arg> ';' <Arg> ')' <Stm>
<Normal Stm>
<Then Stm>
::=
|
|
|
|
|
|
|
|
|
<Arg>
<Expr> ';'
goto Id ';'
break ';'
continue ';'
return <Expr> ';'
';'
!Null statement
::= <Expr>
|
<Case Stms> ::= case <Value> ':' <Stm List> <Case Stms>
| default ':' <Stm List>
|
<Block>
<Stm List>
::=
|
! ===================================================================
! Here begins the C's 15 levels of operator precedence.
! ===================================================================
<Expr>
<Op Assign>
::=
|
|
|
|
|
|
|
|
|
|
<Op If>
<Op Or>
<Op And>
<Op BinOR>
<Op BinXOR>
<Op BinAND>
<Op
<Op
<Op
<Op
<Op
<Op
<Op
<Op
<Op
<Op
<Op
If>
If>
If>
If>
If>
If>
If>
If>
If>
If>
If>
'='
'+='
'-='
'*='
'/='
'^='
'&='
'|='
'>>='
'<<='
<Op
<Op
<Op
<Op
<Op
<Op
<Op
<Op
<Op
<Op
Assign>
Assign>
Assign>
Assign>
Assign>
Assign>
Assign>
Assign>
Assign>
Assign>
<Op Equate>
<Op
<Op
<Op
<Op
<Op
Compare>
Compare>
Compare>
Compare>
Shift>
'<'
'>'
'<='
'>='
<Op
<Op
<Op
<Op
Shift>
Shift>
Shift>
Shift>
<Op Shift>
<Op Add>
<Op Mult>
::=
|
|
|
<Op
<Op
<Op
<Op
<Op Unary>
::=
|
|
|
|
|
|
|
|
|
|
|
|
'!'
<Op Unary>
'~'
<Op Unary>
'-'
<Op Unary>
'*'
<Op Unary>
'&'
<Op Unary>
'++'
<Op Unary>
'--'
<Op Unary>
<Op Pointer> '++'
<Op Pointer> '--'
'(' <Type> ')' <Op Unary>
!CAST
sizeof '(' <Type> ')'
sizeof '(' ID <Pointers> ')'
<Op Pointer>
<Value>
OctLiteral
HexLiteral
DecLiteral
StringLiteral
CharLiteral
FloatLiteral
Id '(' <Expr> ')'
Id '(' ')'
::=
|
|
|
|
|
|
|
| Id
| '(' <Expr> ')'