⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp.grm

📁 a LALR(1) grammar for C
💻 GRM
字号:

/* C++ Grammar ...so far.  by John M. Dlugosz */

/* TOKENS. */

   <error>
   <identifier>  => KW_SEARCH
   <operator>    => OP_SEARCH
   <punctuator>  => OP_SEARCH
   <number>
   <string>
   <eof>
   <type>

/* KEYWORDS. */

   auto break case cdecl char class const continue default delete do
   double else enum extern far float for friend goto huge if inline int
   interrupt long near new operator overload pascal private protected
   public register return short signed sizeof static struct switch this
   typedef union unsigned virtual void volatile while

/* OPERATORS. */

   || &&
   < <= == > >=
   + - * / %
   ? ++ -- '->'
   ! ~ ^ '|' & >> <<
   = <<= != %= &= *= += -= /= |= >>= ^=

/* PUNCTUATORS. */

   '...' . , : ; [ ] { } ( ) ::

/* NONTERMINALS. */

Input -> File_and_tell <eof>

File_and_tell -> File      => AllDoneNow  1   /* normal completion */

File -> Item | File Item

Item -> Declaration
   /* or Definition.  not in yet. */


/**************************************************************
To recognize a declaration, the storage class and type appear
once.  They are remembered.  Each declaration is seperated by
commas, and share the same type.  The FinishedDeclarator calls
an action for each one found.
****************/

Declaration
   -> StorageClass Type_w/const Declarators? ;


Declarators?
   ->
   -> Declarators

Declarators
   -> FinishedDeclarator
   -> FinishedDeclarator , Declarators

FinishedDeclarator -> Declarator Initializer?  => Declaration 1
                   -> Decl2 ( 1+Expression-List )  => Declaration 1
                      /* constructor */

/*********************************/


Initializer?
   ->
   -> = Expression
   -> = { 1+Expression-List }

Arg-Initializer?
   ->
   -> = Expression

1+Expression-List  /* a non-empty list */
   -> Expression
   -> Expression , 1+Expression-List


StorageClass
   ->            => StoreStorage 0
   -> MustStorageClass

MustStorageClass
   -> static     => StoreStorage 1
   -> extern     => StoreStorage 2
   -> typedef    => StoreStorage 3
   -> auto       => StoreStorage 4
   -> register   => StoreStorage 5
   -> inline     => StoreStorage 6


Type_w/const  /* const may appear before or after the type name */
   ->                                            => StoreType 0
   -> Const/Volatile? Type Const/Volatile?       => StoreBaseConstVol


Type
   -> char            => StoreType 1
   -> signed char     => StoreType 2
   -> unsigned char   => StoreType 3
   -> int             => StoreType 4
   -> short           => StoreType 4
   -> short int       => StoreType 4
   -> signed int      => StoreType 4
   -> signed short    => StoreType 4
   -> signed short int      => StoreType 4
   -> unsigned        => StoreType 5
   -> unsigned int    => StoreType 5
   -> unsigned short    => StoreType 5
   -> unsigned short int    => StoreType 5
   -> long            => StoreType 6
   -> signed long     => StoreType 6
   -> unsigned long   => StoreType 7
   -> float           => StoreType 8
   -> double          => StoreType 9
   -> long double     => StoreType 10
   -> void            => StoreType 11
   -> enum Tag        => StoreType 12
   -> AggrDef         => StoreType 13


/* class/struct/union stuff */

PureAggrDef
   -> AggrType Tag AggrBody?

AggrDef
   -> PureAggrDef
   -> AggrType AggrBody

AggrType
   -> Class
   -> union       => AggrForm 0

Class
   -> struct      => AggrForm 1
   -> class       => AggrForm 2

Tag
   -> <identifier>    => StoreTag 1
   -> <type>          => StoreTag 2

/* members of a class/struct/union */


AggrBody?
   ->             => StoreAggrBody 0
   -> AggrBody    => StoreAggrBody 1

AggrBody
   -> { Aggr-Member-List }

Aggr-Member-List
   -> Start-Nested-Type A-Member-List? End-Nested-Type

A-Member-List?
   ->
   -> A-Member-List

A-Member-List
   -> A-Member-List  Member-Declaration
   -> Member-Declaration

Member-Declaration
   -> Friend? StorageClass Type_w/const Declarator Initializer? ;  => Declaration 3
   -> ProtectionSpecifier :

ProtectionSpecifier
   -> public        => ProtectionSpecifier 0
   -> private       => ProtectionSpecifier 2
   -> protected     => ProtectionSpecifier 3

Friend?
   ->         => IsFriend 0
   -> friend  => IsFriend 1




OverloadableOp -> * | / | = | + /* and all the others */

Elipses? -> | '...'

/* Declarations */

Declarator
   -> Decl2
   -> ReachAttribute * Const/Volatile? Declarator    => TypeModifier 3
   -> ReachAttribute & Const/Volatile? Declarator    => TypeModifier 4

Decl2
   -> Decl2 ( Arg-Declaration-List )       => TypeModifier 1
   -> Decl2 [ ConstExp? ]                  => TypeModifier 2
   -> Decl3

Decl3
   -> Dname          => Dname 1
   -> ( Declarator )

Const/Volatile?  /* const or volotile, neither, or both */
  ->                   => ConstVol 0
  -> const             => ConstVol 1
  -> volatile          => ConstVol 2
  -> const volatile    => ConstVol 3
  -> volatile const    => ConstVol 3


ReachAttribute
  ->         => ReachType 0
  -> near    => ReachType 4
  -> far     => ReachType 8

Dname
  -> SimpleDname
  -> <type> :: SimpleDname

SimpleDname
  -> <identifier>
  -> <type>
  -> ~ <type>
  -> Operator-FunctionName

Operator-FunctionName
   -> operator OverloadableOp  /* overload operator */
   -> operator <type> /* conversion operator */
       /* this should really allow any abstract type definition, not just
          a simple type name.  I'll change it later */
   -> operator <identifier>  /* ERROR production */


/* Argument list for function declarations */

Arg-Declaration-List
   -> Start-Nested-Type A-Decl-List? Elipses? End-Nested-Type

Start-Nested-Type    -> =>  NestedType 1
End-Nested-Type      -> =>  NestedType 0

A-Decl-List?
   ->
   -> A-Decl-List

A-Decl-List
   -> A-Decl-List , Argument-Declaration
   -> Argument-Declaration

Argument-Declaration
   -> MustStorageClass Type_w/const Declarator Arg-Initializer? => Declaration 2

/* Expressions */

ConstExp?
   ->
   -> ConstExp

ConstExp -> Expression    /* semantics will check */

Expression
   /* stub out for now */
   -> <identifier>
   -> <number>
   -> <string>


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -