rewrite parser with C Syntax BNF file
This commit is contained in:
parent
e7835b4fac
commit
a7d63e51e5
@ -112,103 +112,330 @@ int yylex(YYSTYPE *, YYLTYPE *);
|
|||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
translation_unit: translation_unit_item
|
translation_unit :
|
||||||
| translation_unit translation_unit_item
|
| external_decl
|
||||||
|
| translation_unit external_decl
|
||||||
;
|
;
|
||||||
|
|
||||||
translation_unit_item: declaration
|
external_decl : function_definition
|
||||||
| function_definition
|
| decl
|
||||||
;
|
;
|
||||||
|
|
||||||
declaration: type TOK_IDENTIFIER TOK_SEMICOLON
|
function_definition : decl_specs declarator decl_list compound_stat
|
||||||
| typedef_declaration
|
| declarator decl_list compound_stat
|
||||||
|
| decl_specs declarator compound_stat
|
||||||
|
| declarator compound_stat
|
||||||
;
|
;
|
||||||
|
|
||||||
typedef_declaration: TOK_TYPEDEF type TOK_IDENTIFIER TOK_SEMICOLON
|
decl : decl_specs init_declarator_list TOK_SEMICOLON
|
||||||
|
| decl_specs TOK_SEMICOLON
|
||||||
;
|
;
|
||||||
|
|
||||||
type: TOK_VOID
|
decl_list : decl
|
||||||
| opt_int_type_qualifiers int_type
|
| decl_list decl
|
||||||
| opt_float_type_qualifiers float_type
|
|
||||||
;
|
;
|
||||||
|
|
||||||
int_type: TOK_CHAR
|
decl_specs : storage_class_spec decl_specs
|
||||||
| TOK_SHORT
|
| storage_class_spec
|
||||||
| TOK_INT
|
| type_spec decl_specs
|
||||||
| TOK_LONG
|
| type_spec
|
||||||
| TOK_LONG TOK_LONG
|
| type_qualifier decl_specs
|
||||||
|
| type_qualifier
|
||||||
;
|
;
|
||||||
|
|
||||||
float_type: TOK_FLOAT
|
storage_class_spec : TOK_AUTO | TOK_REGISTER | TOK_STATIC | TOK_EXTERN | TOK_TYPEDEF
|
||||||
| TOK_DOUBLE
|
|
||||||
;
|
;
|
||||||
|
|
||||||
opt_int_type_qualifiers:
|
type_spec : TOK_VOID | TOK_CHAR | TOK_SHORT | TOK_INT | TOK_LONG | TOK_FLOAT
|
||||||
| int_type_qualifiers
|
| TOK_DOUBLE | TOK_SIGNED | TOK_UNSIGNED
|
||||||
|
| struct_or_union_spec
|
||||||
|
| enum_spec
|
||||||
|
| typedef_name
|
||||||
;
|
;
|
||||||
|
|
||||||
int_type_qualifiers: int_type_qualifier
|
type_qualifier : TOK_CONST | TOK_VOLATILE
|
||||||
| int_type_qualifiers int_type_qualifier
|
|
||||||
;
|
;
|
||||||
|
|
||||||
int_type_qualifier: TOK_VOLATILE
|
struct_or_union_spec : struct_or_union TOK_IDENTIFIER TOK_LCURLY struct_decl_list TOK_RCURLY
|
||||||
| TOK_CONST
|
| struct_or_union TOK_LCURLY struct_decl_list TOK_RCURLY
|
||||||
| TOK_STATIC
|
| struct_or_union TOK_IDENTIFIER
|
||||||
| TOK_EXTERN
|
|
||||||
| TOK_SIGNED
|
|
||||||
| TOK_UNSIGNED
|
|
||||||
| TOK_SHORT
|
|
||||||
| TOK_LONG
|
|
||||||
;
|
;
|
||||||
|
|
||||||
opt_float_type_qualifiers:
|
struct_or_union : TOK_STRUCT | TOK_UNION
|
||||||
| float_type_qualifiers
|
|
||||||
;
|
;
|
||||||
|
|
||||||
float_type_qualifiers: float_type_qualifier
|
struct_decl_list : struct_decl
|
||||||
| float_type_qualifiers float_type_qualifier
|
| struct_decl_list struct_decl
|
||||||
;
|
;
|
||||||
|
|
||||||
float_type_qualifier: TOK_VOLATILE
|
init_declarator_list : init_declarator
|
||||||
| TOK_CONST
|
| init_declarator_list TOK_COMMA init_declarator
|
||||||
| TOK_STATIC
|
|
||||||
| TOK_EXTERN
|
|
||||||
| TOK_LONG
|
|
||||||
;
|
;
|
||||||
|
|
||||||
function_definition: function_header compound_statement
|
init_declarator : declarator
|
||||||
|
| declarator TOK_ASSIGN initializer
|
||||||
;
|
;
|
||||||
|
|
||||||
function_header: type TOK_IDENTIFIER TOK_LPAREN opt_parameter_list TOK_RPAREN
|
struct_decl : spec_qualifier_list struct_declarator_list TOK_SEMICOLON
|
||||||
;
|
;
|
||||||
|
|
||||||
opt_parameter_list:
|
|
||||||
| parameter_list
|
spec_qualifier_list : type_spec spec_qualifier_list
|
||||||
|
| type_spec
|
||||||
|
| type_qualifier spec_qualifier_list
|
||||||
|
| type_qualifier
|
||||||
;
|
;
|
||||||
|
|
||||||
parameter_list: parameter
|
struct_declarator_list : struct_declarator
|
||||||
| parameter_list TOK_COMMA parameter
|
| struct_declarator_list TOK_COMMA struct_declarator
|
||||||
;
|
;
|
||||||
|
|
||||||
parameter: type TOK_IDENTIFIER
|
struct_declarator : declarator
|
||||||
|
| declarator TOK_COLON const_exp
|
||||||
|
| TOK_COLON const_exp
|
||||||
;
|
;
|
||||||
|
|
||||||
compound_statement: TOK_LBRACKET opt_statement_list TOK_RBRACKET
|
enum_spec : TOK_ENUM TOK_IDENTIFIER TOK_LCURLY enumerator_list TOK_RCURLY
|
||||||
|
| TOK_ENUM TOK_LCURLY enumerator_list TOK_RCURLY
|
||||||
|
| TOK_ENUM TOK_IDENTIFIER
|
||||||
;
|
;
|
||||||
|
|
||||||
opt_statement_list:
|
enumerator_list : enumerator
|
||||||
| statement_list
|
| enumerator_list TOK_COMMA enumerator
|
||||||
;
|
;
|
||||||
|
|
||||||
statement_list: statement
|
enumerator : TOK_IDENTIFIER
|
||||||
| statement_list statement
|
| TOK_IDENTIFIER TOK_ASSIGN const_exp
|
||||||
;
|
;
|
||||||
|
|
||||||
statement: declaration
|
declarator : pointer direct_declarator
|
||||||
| expression TOK_SEMICOLON
|
| direct_declarator
|
||||||
;
|
;
|
||||||
|
|
||||||
expression: TOK_IDENTIFIER
|
direct_declarator : TOK_IDENTIFIER
|
||||||
|
| TOK_LPAREN declarator TOK_RPAREN
|
||||||
|
| direct_declarator TOK_LBRACKET const_exp TOK_RBRACKET
|
||||||
|
| direct_declarator TOK_LBRACKET TOK_RBRACKET
|
||||||
|
| direct_declarator TOK_LPAREN param_type_list TOK_RPAREN
|
||||||
|
| direct_declarator TOK_LPAREN id_list TOK_RPAREN
|
||||||
|
| direct_declarator TOK_LPAREN TOK_RPAREN
|
||||||
|
;
|
||||||
|
|
||||||
|
pointer : TOK_TIMES type_qualifier_list
|
||||||
|
| TOK_TIMES
|
||||||
|
| TOK_TIMES type_qualifier_list pointer
|
||||||
|
| TOK_TIMES pointer
|
||||||
|
;
|
||||||
|
|
||||||
|
type_qualifier_list : type_qualifier
|
||||||
|
| type_qualifier_list type_qualifier
|
||||||
|
;
|
||||||
|
|
||||||
|
param_type_list : param_list
|
||||||
|
| param_list TOK_COMMA TOK_ELLIPSES
|
||||||
|
;
|
||||||
|
|
||||||
|
param_list : param_decl
|
||||||
|
| param_list TOK_COMMA param_decl
|
||||||
|
;
|
||||||
|
|
||||||
|
param_decl : decl_specs declarator
|
||||||
|
| decl_specs abstract_declarator
|
||||||
|
| decl_specs
|
||||||
|
;
|
||||||
|
|
||||||
|
id_list : TOK_IDENTIFIER
|
||||||
|
| id_list TOK_COMMA TOK_IDENTIFIER
|
||||||
|
;
|
||||||
|
|
||||||
|
initializer : assignment_exp
|
||||||
|
| TOK_LCURLY initializer_list TOK_RCURLY
|
||||||
|
| TOK_LCURLY initializer_list TOK_COMMA TOK_RCURLY
|
||||||
|
;
|
||||||
|
|
||||||
|
initializer_list : initializer
|
||||||
|
| initializer_list TOK_COMMA initializer
|
||||||
|
;
|
||||||
|
|
||||||
|
type_name : spec_qualifier_list abstract_declarator
|
||||||
|
| spec_qualifier_list
|
||||||
|
;
|
||||||
|
|
||||||
|
abstract_declarator : pointer
|
||||||
|
| pointer direct_abstract_declarator
|
||||||
|
| direct_abstract_declarator
|
||||||
|
;
|
||||||
|
|
||||||
|
direct_abstract_declarator: TOK_LPAREN abstract_declarator TOK_RPAREN
|
||||||
|
| direct_abstract_declarator TOK_LBRACKET const_exp TOK_RBRACKET
|
||||||
|
| TOK_LBRACKET const_exp TOK_RBRACKET
|
||||||
|
| direct_abstract_declarator TOK_LBRACKET TOK_RBRACKET
|
||||||
|
| TOK_LBRACKET TOK_RBRACKET
|
||||||
|
| direct_abstract_declarator TOK_LPAREN param_type_list TOK_RPAREN
|
||||||
|
| TOK_LPAREN param_type_list TOK_RPAREN
|
||||||
|
| direct_abstract_declarator TOK_LPAREN TOK_RPAREN
|
||||||
|
| TOK_LPAREN TOK_RPAREN
|
||||||
|
;
|
||||||
|
|
||||||
|
typedef_name : TOK_IDENTIFIER
|
||||||
|
;
|
||||||
|
|
||||||
|
stat : labeled_stat
|
||||||
|
| exp_stat
|
||||||
|
| compound_stat
|
||||||
|
| selection_stat
|
||||||
|
| iteration_stat
|
||||||
|
| jump_stat
|
||||||
|
;
|
||||||
|
|
||||||
|
labeled_stat : TOK_IDENTIFIER TOK_COLON stat
|
||||||
|
| TOK_CASE const_exp TOK_COLON stat
|
||||||
|
| TOK_DEFAULT TOK_COLON stat
|
||||||
|
;
|
||||||
|
|
||||||
|
exp_stat : exp TOK_SEMICOLON
|
||||||
|
| TOK_SEMICOLON
|
||||||
|
;
|
||||||
|
|
||||||
|
compound_stat : TOK_LCURLY decl_list stat_list TOK_RCURLY
|
||||||
|
| TOK_LCURLY stat_list TOK_RCURLY
|
||||||
|
| TOK_LCURLY decl_list TOK_RCURLY
|
||||||
|
| TOK_LCURLY TOK_RCURLY
|
||||||
|
;
|
||||||
|
|
||||||
|
stat_list : stat
|
||||||
|
| stat_list stat
|
||||||
|
;
|
||||||
|
|
||||||
|
selection_stat : TOK_IF TOK_LPAREN exp TOK_RPAREN stat
|
||||||
|
| TOK_IF TOK_LPAREN exp TOK_RPAREN stat TOK_ELSE stat
|
||||||
|
| TOK_SWITCH TOK_LPAREN exp TOK_RPAREN stat
|
||||||
|
;
|
||||||
|
|
||||||
|
iteration_stat : TOK_WHILE TOK_LPAREN exp TOK_RPAREN stat
|
||||||
|
| TOK_DO stat TOK_WHILE TOK_LPAREN exp TOK_RPAREN TOK_SEMICOLON
|
||||||
|
| TOK_FOR TOK_LPAREN exp TOK_SEMICOLON exp TOK_SEMICOLON exp TOK_RPAREN stat
|
||||||
|
| TOK_FOR TOK_LPAREN exp TOK_SEMICOLON exp TOK_SEMICOLON TOK_RPAREN stat
|
||||||
|
| TOK_FOR TOK_LPAREN exp TOK_SEMICOLON TOK_SEMICOLON exp TOK_RPAREN stat
|
||||||
|
| TOK_FOR TOK_LPAREN exp TOK_SEMICOLON TOK_SEMICOLON TOK_RPAREN stat
|
||||||
|
| TOK_FOR TOK_LPAREN TOK_SEMICOLON exp TOK_SEMICOLON exp TOK_RPAREN stat
|
||||||
|
| TOK_FOR TOK_LPAREN TOK_SEMICOLON exp TOK_SEMICOLON TOK_RPAREN stat
|
||||||
|
| TOK_FOR TOK_LPAREN TOK_SEMICOLON TOK_SEMICOLON exp TOK_RPAREN stat
|
||||||
|
| TOK_FOR TOK_LPAREN TOK_SEMICOLON TOK_SEMICOLON TOK_RPAREN stat
|
||||||
|
;
|
||||||
|
|
||||||
|
jump_stat : TOK_GOTO TOK_IDENTIFIER TOK_SEMICOLON
|
||||||
|
| TOK_CONTINUE TOK_SEMICOLON
|
||||||
|
| TOK_BREAK TOK_SEMICOLON
|
||||||
|
| TOK_RETURN exp TOK_SEMICOLON
|
||||||
|
| TOK_RETURN TOK_SEMICOLON
|
||||||
|
;
|
||||||
|
|
||||||
|
exp : assignment_exp
|
||||||
|
| exp TOK_COMMA assignment_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
assignment_exp : conditional_exp
|
||||||
|
| unary_exp assignment_operator assignment_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
assignment_operator : TOK_ASSIGN | TOK_TIMESEQUALS | TOK_DIVIDEEQUALS | TOK_MODEQUALS | TOK_PLUSEQUALS | TOK_MINUSEQUALS | TOK_LSHIFTEQUALS
|
||||||
|
| TOK_RSHIFTEQUALS | TOK_BITANDEQUALS | TOK_XOREQUALS | TOK_BITOREQUALS
|
||||||
|
;
|
||||||
|
|
||||||
|
conditional_exp : logical_or_exp
|
||||||
|
| logical_or_exp TOK_QUESTION exp TOK_COLON conditional_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
const_exp : conditional_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
logical_or_exp : logical_and_exp
|
||||||
|
| logical_or_exp TOK_OR logical_and_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
logical_and_exp : inclusive_or_exp
|
||||||
|
| logical_and_exp TOK_AND inclusive_or_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
inclusive_or_exp : exclusive_or_exp
|
||||||
|
| inclusive_or_exp TOK_BITOR exclusive_or_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
exclusive_or_exp : and_exp
|
||||||
|
| exclusive_or_exp TOK_XOR and_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
and_exp : equality_exp
|
||||||
|
| and_exp TOK_BITAND equality_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
equality_exp : relational_exp
|
||||||
|
| equality_exp TOK_EQUALS relational_exp
|
||||||
|
| equality_exp TOK_NOTEQUALS relational_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
relational_exp : shift_expression
|
||||||
|
| relational_exp TOK_LESS shift_expression
|
||||||
|
| relational_exp TOK_GREATER shift_expression
|
||||||
|
| relational_exp TOK_LESSEQ shift_expression
|
||||||
|
| relational_exp TOK_GREATEREQ shift_expression
|
||||||
|
;
|
||||||
|
|
||||||
|
shift_expression : additive_exp
|
||||||
|
| shift_expression TOK_LSHIFT additive_exp
|
||||||
|
| shift_expression TOK_RSHIFT additive_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
additive_exp : mult_exp
|
||||||
|
| additive_exp TOK_PLUS mult_exp
|
||||||
|
| additive_exp TOK_MINUS mult_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
mult_exp : cast_exp
|
||||||
|
| mult_exp TOK_TIMES cast_exp
|
||||||
|
| mult_exp TOK_DIVIDE cast_exp
|
||||||
|
| mult_exp TOK_MOD cast_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
cast_exp : unary_exp
|
||||||
|
| TOK_LPAREN type_name TOK_RPAREN cast_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
unary_exp : postfix_exp
|
||||||
|
| TOK_INCREMENT unary_exp
|
||||||
|
| TOK_DECREMENT unary_exp
|
||||||
|
| unary_operator cast_exp
|
||||||
|
| TOK_SIZEOF unary_exp
|
||||||
|
| TOK_SIZEOF TOK_LPAREN type_name TOK_RPAREN
|
||||||
|
;
|
||||||
|
|
||||||
|
unary_operator : TOK_BITAND | TOK_TIMES | TOK_PLUS | TOK_MINUS | TOK_BITNOT | TOK_NOT
|
||||||
|
;
|
||||||
|
|
||||||
|
postfix_exp : primary_exp
|
||||||
|
| postfix_exp TOK_LBRACKET exp TOK_RBRACKET
|
||||||
|
| postfix_exp TOK_LPAREN argument_exp_list TOK_RPAREN
|
||||||
|
| postfix_exp TOK_LPAREN TOK_RPAREN
|
||||||
|
| postfix_exp TOK_DOT TOK_IDENTIFIER
|
||||||
|
| postfix_exp TOK_ARROW TOK_IDENTIFIER
|
||||||
|
| postfix_exp TOK_INCREMENT
|
||||||
|
| postfix_exp TOK_DECREMENT
|
||||||
|
;
|
||||||
|
|
||||||
|
primary_exp : const
|
||||||
|
| TOK_STR_CONST
|
||||||
|
| TOK_LPAREN exp TOK_RPAREN
|
||||||
|
;
|
||||||
|
|
||||||
|
argument_exp_list : assignment_exp
|
||||||
|
| argument_exp_list TOK_COMMA assignment_exp
|
||||||
|
;
|
||||||
|
|
||||||
|
const : TOK_INT_CONST
|
||||||
|
| TOK_CHAR_CONST
|
||||||
|
| TOK_FLOAT_CONST
|
||||||
|
| TOK_IDENTIFIER
|
||||||
;
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
Loading…
x
Reference in New Issue
Block a user