📄 cansi.y
字号:
struct
: struct_or_union T_LBRACE
{ $$ = StructHeader( $1 ); }
| struct_or_union any_id T_LBRACE
{ $$ = StructIHeader( $2, $1 ); }
| struct_decl T_SEMI
;
struct_decl
: struct type_tqs_specifiers struct_declarator
{ StartStructDecl( $1, $2 );
StructDecl( $1, $3 );
PopType();
$$ = $1; }
| struct tq_specs struct_declid
{ StartStructDecl( $1, EndBase( $2 ) );
StructDecl( $1, $3 );
PopType();
$$ = $1; }
| struct_decl T_COMMA struct_declarator
{ StructDecl( $1, $3 );
$$ = $1; }
;
struct_declid
: declid
| declid T_COLON constant_expression
{ $$ = FieldType( $1, ConValue( $3 ) ); }
| T_COLON constant_expression
{ $$ = FieldType( NULL, ConValue( $2 ) ); }
;
struct_declarator
: declarator
| declarator T_COLON constant_expression
{ $$ = FieldType( $1, ConValue( $3 ) ); }
| T_COLON constant_expression
{ $$ = FieldType( NULL, ConValue( $2 ) ); }
;
enum_specifier
: T_ENUM T_LBRACE enumerator_list T_RBRACE
{ $$ = UntaggedTypeEnum( $3 ); }
| T_ENUM any_id T_LBRACE enumerator_list T_RBRACE
{ $$ = TaggedTypeEnum( $2, $4 ); }
| T_ENUM any_id
{ $$ = GetEnumType( $2 ); }
;
enumerator_list
: enumerator
{ $$ = AddToEnumList( NULL, $1 ); }
| enumerator_list T_COMMA enumerator
{ $$ = AddToEnumList( $1, $3 ); }
;
enumerator
: T_ID
{ $$ = EnumId( $1, VALUE_NOT_SPECIFIED, FALSE ); }
| T_ID T_ASS constant_expression
{ $$ = EnumId( $1, ConValue( $3 ), TRUE ); }
;
type_qualifier
: T_CONST
{ $$ = CONSTANT_MOD; }
| T_VOLATILE
{ $$ = VOLATILE_MOD; }
;
declarator
: direct_declarator
| pointer direct_declarator
{ $$ = AttachDecl( $1, $2 ); }
;
direct_declarator
: any_id
{ $$ = Decl( $1 ); }
| T_LPAR declarator T_RPAR
{ $$ = $2; }
| direct_declarator T_LSQR constant_expression T_RSQR
{ $$ = ArrayType( $1, ConValue( $3 ) ); }
| direct_declarator T_LSQR T_RSQR
{ $$ = ArrayType( $1, VALUE_NOT_SPECIFIED ); }
| direct_declarator T_LPAR T_RPAR
{ $$ = FuncType( $1, NULL ); }
| direct_declarator T_LPAR parameter_type_list T_RPAR
{ $$ = FuncType( $1, $3 ); }
| direct_declarator T_LPAR identifier_list T_RPAR
{ $$ = FuncType( $1, $3 ); }
;
declid
: direct_declid
| pointer direct_declid
{ $$ = AttachDecl( $1, $2 ); }
;
direct_declid
: T_ID
{ $$ = Decl( $1 ); }
| T_LPAR declid T_RPAR
{ $$ = $2; }
| direct_declid T_LSQR constant_expression T_RSQR
{ $$ = ArrayType( $1, ConValue( $3 ) ); }
| direct_declid T_LSQR T_RSQR
{ $$ = ArrayType( $1, VALUE_NOT_SPECIFIED ); }
| direct_declid T_LPAR T_RPAR
{ $$ = FuncType( $1, NULL ); }
| direct_declid T_LPAR parameter_type_list T_RPAR
{ $$ = FuncType( $1, $3 ); }
| direct_declid T_LPAR identifier_list T_RPAR
{ $$ = FuncType( $1, $3 ); }
;
decl_parmtid
: direct_parmtid
| pointer direct_parmtid
{ $$ = AttachDecl( $1, $2 ); }
;
direct_parmtid
: T_TYPE_ID
{ $$ = Decl( $1 ); }
| T_LPAR pointer direct_parmtid T_RPAR
{ $$ = AttachDecl( $2, $3 ); }
| direct_parmtid T_LSQR constant_expression T_RSQR
{ $$ = ArrayType( $1, ConValue( $3 ) ); }
| direct_parmtid T_LSQR T_RSQR
{ $$ = ArrayType( $1, VALUE_NOT_SPECIFIED ); }
| direct_parmtid T_LPAR T_RPAR
{ $$ = FuncType( $1, NULL ); }
| direct_parmtid T_LPAR parameter_type_list T_RPAR
{ $$ = FuncType( $1, $3 ); }
| direct_parmtid T_LPAR identifier_list T_RPAR
{ $$ = FuncType( $1, $3 ); }
;
pointer
: T_STAR
{ $$ = PointerType( NULL, Decl( NULL ) ); }
| T_STAR tq_specs
{ $$ = PointerType( $2, Decl( NULL ) ); }
| T_STAR pointer
{ $$ = PointerType( NULL, $2 ); }
| T_STAR tq_specs pointer
{ $$ = PointerType( $2, $3 ); }
;
parameter_type_list
: parameter_list
{ $$ = $1; }
| parameter_list T_COMMA T_DOTDOT T_DOT
{ $$ = AddEllipsis( $1 ); }
;
parameter_list
: parameter_declaration
{ $$ = StartParmList( $1 );
PopType(); }
| parameter_list T_COMMA parameter_declaration
{ $$ = AddParm( $1, $3 );
PopType(); }
;
parameter_declaration
: declaration_specifiers declid
{ AttachBaseType( $1, $2 );
$$ = $2; }
| declaration_specifiers decl_parmtid
{ AttachBaseType( $1, $2 );
$$ = $2; }
| sc_tq_specs declid
{ AttachBaseType( EndBase( $1 ), $2 );
$$ = $2; }
| sc_tq_specs abstract_declarator
{ AttachBaseType( EndBase( $1 ), $2 );
$$ = $2; }
| declaration_specifiers
{ $$ = Decl( NULL );
AttachBaseType( $1, $$ ); }
| declaration_specifiers abstract_declarator
{ AttachBaseType( $1, $2 );
$$ = $2; }
;
identifier_list
: T_ID
{ $$ = StartIdList( $1 ); }
| identifier_list T_COMMA T_ID
{ $$ = AddId( $1, $3 ); }
;
type_name
: type_tqs_specifiers
{ $$ = BaseStack->tptr; }
| type_tqs_specifiers abstract_declarator
{ AttachBaseType( BaseStack->tptr, $2 );
$$ = DeclType( $2 ); }
;
abstract_declarator
: pointer
{ $$ = AttachDecl( $1, Decl( NULL ) ); }
| direct_abstract_declarator
{ $$ = $1; }
| pointer direct_abstract_declarator
{ $$ = AttachDecl( $1, $2 ); }
;
direct_abstract_declarator
: T_LPAR abstract_declarator T_RPAR
{ $$ = $2; }
| T_LSQR T_RSQR
{ $$ = ArrayType( Decl( NULL ), VALUE_NOT_SPECIFIED ); }
| T_LSQR constant_expression T_RSQR
{ $$ = ArrayType( Decl( NULL ), ConValue( $2 ) ); }
| direct_abstract_declarator T_LSQR T_RSQR
{ $$ = ArrayType( $1, VALUE_NOT_SPECIFIED ); }
| direct_abstract_declarator T_LSQR constant_expression T_RSQR
{ $$ = ArrayType( $1, ConValue( $3 ) ); }
| T_LPAR T_RPAR
{ $$ = FuncType( Decl( NULL ), NULL ); }
| T_LPAR parameter_type_list T_RPAR
{ $$ = FuncType( Decl( NULL ), $2 ); }
| direct_abstract_declarator T_LPAR T_RPAR
{ $$ = FuncType( $1, NULL ); }
| direct_abstract_declarator T_LPAR parameter_type_list T_RPAR
{ $$ = FuncType( $1 , $3 ); }
;
initializer
: assignment_expression
{ InitExpr( $1 ); }
| T_LBRACE beg_initializer initializer_list T_RBRACE
{ PopInitEntry(); }
| T_LBRACE beg_initializer initializer_list T_COMMA T_RBRACE
{ PopInitEntry(); }
;
initializer_list
: initializer
| initializer_list T_COMMA initializer
;
beg_initializer
:
{ PushInitEntry(); }
;
statement
: statement_beg statement1
;
statement1
: labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
statement_beg
:
{ ELinenum(); }
;
labeled_statement
: label statement
;
label
: T_ID T_COLON
{ LabelStmt( $1 ); }
| T_CASE constant_expression T_COLON
{ CaseStmt( $2 ); }
| T_DEFAULT T_COLON
{ DefaultStmt(); }
;
compound_statement
: beg_compound T_RBRACE
{ PopCompound(); }
| beg_compound statement_list T_RBRACE
{ PopCompound(); }
| beg_compound declaration_list T_RBRACE
{ PopCompound(); }
| beg_compound declaration_list statement_list T_RBRACE
{ PopCompound(); }
;
beg_compound
: T_LBRACE
{ PushCompound();
BlockType = SL_BLOCK; }
;
declaration_list
: declaration
| declaration_list declaration
;
statement_list
: statement
| statement_list statement
;
expression_statement
: expression T_SEMI
{ RootEval( $1 ); }
| T_SEMI
;
selection_statement
: if_beg statement
{ AltBlock( $1 );
EndBlock( $1 ); }
| if_else_beg statement
{ AltBlock( $1 );
EndBlock( $1 ); }
| switch_beg statement
{ EndSwitch( $1 ); }
;
if_beg
: T_IF T_LPAR expression T_RPAR
{ $$ = BegBlock( C_COND );
JFAlt( $$, $3 ); }
;
if_else_beg
: if_beg matched_stat T_ELSE
{ JEnd( $1 );
AltBlock( $1 );
$$ = $1; }
;
switch_beg
: T_SWITCH T_LPAR expression T_RPAR
{ $$ = SwitchStmt( $3 ); }
;
iteration_statement
: while_beg statement
{ AltBlock( $1 );
JBeg( $1 );
EndBlock( $1 ); }
| do_beg statement T_WHILE T_LPAR expression T_RPAR T_SEMI
{ AltBlock( $1 );
JFEnd( $1, $5 );
JBeg( $1 );
EndBlock( $1 ); }
| for_beg opt_expression T_RPAR statement
{ AltBlock( $1 );
RootEval( $2 );
JBeg( $1 );
EndBlock( $1 ); }
;
while_beg
: T_WHILE T_LPAR expression T_RPAR
{ $$ = BegBlock( C_LOOP );
JFEnd( $$, $3 ); }
;
do_beg
: T_DO
{ $$ = BegBlock( C_LOOP ); }
;
for_beg
: T_FOR T_LPAR opt_expression T_SEMI opt_expression T_SEMI
{ $$ = ForStmt( $3, $5 ); }
;
jump_statement
: T_GOTO T_ID T_SEMI
{ GotoStmt( $2 ); }
| T_CONTINUE T_SEMI
{ ContinueStmt(); }
| T_BREAK T_SEMI
{ BreakStmt(); }
| T_RETURN opt_expression T_SEMI
{ ReturnStmt( $2 ); }
;
matched_stat
: statement_beg matched_stat2
;
matched_stat2
: labelled_matched_stat
| compound_statement
| expression_statement
| jump_statement
| do_beg statement T_WHILE T_LPAR expression T_RPAR T_SEMI
{ AltBlock( $1 );
JFEnd( $1, $5 );
JBeg( $1 );
EndBlock( $1 ); }
| if_else_beg matched_stat
{ AltBlock( $1 );
EndBlock( $1 ); }
| while_beg matched_stat
{ JBeg( $1 );
AltBlock( $1 );
EndBlock( $1 ); }
| for_beg opt_expression T_RPAR matched_stat
{ AltBlock( $1 );
RootEval( $2 );
JBeg( $1 );
EndBlock( $1 ); }
| switch_beg matched_stat
{ EndSwitch( $1 ); }
;
labelled_matched_stat
: label matched_stat
;
opt_expression
:
{ $$ = NULL; }
| expression
;
translation_unit
: external_definition
| translation_unit external_definition
;
external_definition
: function_definition
| declaration
;
function_definition
: function_header function_beg function_body
{ FuncEpilogue( FuncPtr );
BlockType = SL_EXTERNAL; }
;
function_header
: declaration_specifiers declarator
{ AttachBaseType( BaseStack->tptr, $2 );
FuncPtr = FuncHeader( $2 );
PopType(); }
| default_specifiers declid
{ AttachBaseType( BaseStack->tptr, $2 );
FuncPtr = FuncHeader( $2 );
PopType(); }
| declid
{ AttachBaseType( FindDefinedType( TYPE_INT ), $1 );
StorageClass = SC_NULL;
FuncPtr = FuncHeader( $1 ); }
;
function_body
: declaration_list function_prologue compound_statement
| function_prologue compound_statement
;
function_beg
:
{ BlockType = SL_FUNC; }
;
function_prologue
:
{ FuncPrologue( FuncPtr );
BlockType = SL_BLOCK; }
;
%%
/* function section */
extern char SynError;
int Parser() {
SynError = 0;
yyparse();
}
static yyerror( char *msg ) {
SynError = 1;
Synmsg( msg, NULL );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -