第1个回答 2009-06-21
aggregate_key:
STRUCT
| UNION
;
member_declaration_list:
member_declaration
| member_declaration_list member_declaration
;
member_declaration:
member_declaring_list ';'
| member_default_declaring_list ';'
;
member_default_declaring_list: /* doesn't redeclare typedef*/
type_qualifier_list member_identifier_declarator
| member_default_declaring_list ',' member_identifier_declarator
;
member_declaring_list:
type_specifier member_declarator
| member_declaring_list ',' member_declarator
;
member_declarator:
declarator bit_field_size_opt
| bit_field_size
;
member_identifier_declarator:
identifier_declarator {} bit_field_size_opt
| bit_field_size
;
bit_field_size_opt:
/* nothing */
| bit_field_size
;
bit_field_size:
':' constant_expression
;
enum_name:
ENUM '{' enumerator_list '}'
| ENUM identifier_or_typedef_name '{' enumerator_list '}'
| ENUM identifier_or_typedef_name
;
enumerator_list:
identifier_or_typedef_name enumerator_value_opt
| enumerator_list ',' identifier_or_typedef_name enumerator_value_opt
;
enumerator_value_opt:
/* Nothing */
| '=' constant_expression
;
parameter_type_list:
parameter_list
| parameter_list ',' ELLIPSIS
;
parameter_list:
parameter_declaration
| parameter_list ',' parameter_declaration
;
parameter_declaration:
declaration_specifier
| declaration_specifier abstract_declarator
| declaration_specifier identifier_declarator
| declaration_specifier parameter_typedef_declarator
| declaration_qualifier_list
| declaration_qualifier_list abstract_declarator
| declaration_qualifier_list identifier_declarator
| type_specifier
| type_specifier abstract_declarator
| type_specifier identifier_declarator
| type_specifier parameter_typedef_declarator
| type_qualifier_list
| type_qualifier_list abstract_declarator
| type_qualifier_list identifier_declarator
;
/* ANSI C section 3.7.1 states "An identifier declared as a
typedef name shall not be redeclared as a parameter". Hence the
following is based only on IDENTIFIERs */
identifier_list:
IDENTIFIER
| identifier_list ',' IDENTIFIER
;
identifier_or_typedef_name:
IDENTIFIER
| TYPEDEFname
;
type_name:
type_specifier
| type_specifier abstract_declarator
| type_qualifier_list
| type_qualifier_list abstract_declarator
;
initializer_opt:
/* nothing */
| '=' initializer
;
initializer:
'{' initializer_list '}'
| '{' initializer_list ',' '}'
| assignment_expression
;
initializer_list:
initializer
| initializer_list ',' initializer
;
/*************************** STATEMENTS *******************************/
statement:
labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
labeled_statement:
identifier_or_typedef_name ':' statement
| CASE constant_expression ':' statement
| DEFAULT ':' statement
;
compound_statement:
'{' '}'
| '{' declaration_list '}'
| '{' statement_list '}'
| '{' declaration_list statement_list '}'
;
declaration_list:
declaration
| declaration_list declaration
;
statement_list:
statement
| statement_list statement
;
expression_statement:
comma_expression_opt ';'
;
selection_statement:
IF '(' comma_expression ')' statement
| IF '(' comma_expression ')' statement ELSE statement
| SWITCH '(' comma_expression ')' statement
;
iteration_statement:
WHILE '(' comma_expression ')' statement
| DO statement WHILE '(' comma_expression ')' ';'
| FOR '(' comma_expression_opt ';' comma_expression_opt ';'
comma_expression_opt ')' statement
;
jump_statement:
GOTO identifier_or_typedef_name ';'
| CONTINUE ';'
| BREAK ';'
| RETURN comma_expression_opt ';'
;
/***************************** EXTERNAL DEFINITIONS *****************************/
translation_unit:
external_definition
| translation_unit external_definition
;
external_definition:
function_definition
| declaration
;
function_definition:
identifier_declarator compound_statement
| declaration_specifier identifier_declarator compound_statement
| type_specifier identifier_declarator compound_statement
| declaration_qualifier_list identifier_declarator compound_statement
| type_qualifier_list identifier_declarator compound_statement
| old_function_declarator compound_statement
| declaration_specifier old_function_declarator compound_statement
| type_specifier old_function_declarator compound_statement
| declaration_qualifier_list old_function_declarator compound_statement
| type_qualifier_list old_function_declarator compound_statement
| old_function_declarator declaration_list
compound_statement
| declaration_specifier old_function_declarator declaration_list
compound_statement
| type_specifier old_function_declarator declaration_list
compound_statement
| declaration_qualifier_list old_function_declarator declaration_list
compound_statement
| type_qualifier_list old_function_declarator declaration_list
compound_statement
;
declarator:
identifier_declarator
| typedef_declarator
;
typedef_declarator:
paren_typedef_declarator /* would be ambiguous as parameter*/
| parameter_typedef_declarator /* not ambiguous as param*/
;
parameter_typedef_declarator:
TYPEDEFname
| TYPEDEFname postfixing_abstract_declarator
| clean_typedef_declarator
;
/* The following have at least one '*'. There is no (redundant)
'(' between the '*' and the TYPEDEFname. */
clean_typedef_declarator:
clean_postfix_typedef_declarator
| '*' parameter_typedef_declarator
| '*' type_qualifier_list parameter_typedef_declarator
;
clean_postfix_typedef_declarator:
'(' clean_typedef_declarator ')'
| '(' clean_typedef_declarator ')' postfixing_abstract_declarator
;
/* The following have a redundant '(' placed immediately to the
left of the TYPEDEFname */
paren_typedef_declarator:
paren_postfix_typedef_declarator
| '*' '(' simple_paren_typedef_declarator ')' /* redundant paren */
| '*' type_qualifier_list
'(' simple_paren_typedef_declarator ')' /* redundant paren */
| '*' paren_typedef_declarator
| '*' type_qualifier_list paren_typedef_declarator
;
paren_postfix_typedef_declarator: /* redundant paren to left of tname*/
'(' paren_typedef_declarator ')'
| '(' simple_paren_typedef_declarator postfixing_abstract_declarator ')' /* redundant paren */
| '(' paren_typedef_declarator ')' postfixing_abstract_declarator
;
simple_paren_typedef_declarator:
TYPEDEFname
| '(' simple_paren_typedef_declarator ')'
;
identifier_declarator:
unary_identifier_declarator
| paren_identifier_declarator
;
unary_identifier_declarator:
postfix_identifier_declarator
| '*' identifier_declarator
| '*' type_qualifier_list identifier_declarator
;
postfix_identifier_declarator:
paren_identifier_declarator postfixing_abstract_declarator
| '(' unary_identifier_declarator ')'
| '(' unary_identifier_declarator ')' postfixing_abstract_declarator
;
paren_identifier_declarator:
IDENTIFIER
| '(' paren_identifier_declarator ')'
;
old_function_declarator:
postfix_old_function_declarator
| '*' old_function_declarator
| '*' type_qualifier_list old_function_declarator
;
postfix_old_function_declarator:
paren_identifier_declarator '(' identifier_list ')'
| '(' old_function_declarator ')'
| '(' old_function_declarator ')' postfixing_abstract_declarator
;
abstract_declarator:
unary_abstract_declarator
| postfix_abstract_declarator
| postfixing_abstract_declarator
;
postfixing_abstract_declarator:
array_abstract_declarator
| '(' ')'
| '(' parameter_type_list ')'
;
array_abstract_declarator:
'[' ']'
| '[' constant_expression ']'
| array_abstract_declarator '[' constant_expression ']'
;
unary_abstract_declarator:
'*'
| '*' type_qualifier_list
| '*' abstract_declarator
| '*' type_qualifier_list abstract_declarator
;
postfix_abstract_declarator:
'(' unary_abstract_declarator ')'
| '(' postfix_abstract_declarator ')'
| '(' postfixing_abstract_declarator ')'
| '(' unary_abstract_declarator ')' postfixing_abstract_declarator
;
%%
/* ----end of grammar----*/
yyerror(string)
char*string;
{
printf("parser error: %s\n", string);
}
main()
{
yyparse();
}
代码太长 分两次来
然后用yacc自动生成c语言程序代码