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_item
|
||||
;
|
||||
translation_unit :
|
||||
| external_decl
|
||||
| translation_unit external_decl
|
||||
;
|
||||
|
||||
translation_unit_item: declaration
|
||||
| function_definition
|
||||
;
|
||||
external_decl : function_definition
|
||||
| decl
|
||||
;
|
||||
|
||||
declaration: type TOK_IDENTIFIER TOK_SEMICOLON
|
||||
| typedef_declaration
|
||||
;
|
||||
function_definition : decl_specs declarator decl_list compound_stat
|
||||
| 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
|
||||
| opt_int_type_qualifiers int_type
|
||||
| opt_float_type_qualifiers float_type
|
||||
;
|
||||
decl_list : decl
|
||||
| decl_list decl
|
||||
;
|
||||
|
||||
int_type: TOK_CHAR
|
||||
| TOK_SHORT
|
||||
| TOK_INT
|
||||
| TOK_LONG
|
||||
| TOK_LONG TOK_LONG
|
||||
;
|
||||
decl_specs : storage_class_spec decl_specs
|
||||
| storage_class_spec
|
||||
| type_spec decl_specs
|
||||
| type_spec
|
||||
| type_qualifier decl_specs
|
||||
| type_qualifier
|
||||
;
|
||||
|
||||
float_type: TOK_FLOAT
|
||||
| TOK_DOUBLE
|
||||
;
|
||||
storage_class_spec : TOK_AUTO | TOK_REGISTER | TOK_STATIC | TOK_EXTERN | TOK_TYPEDEF
|
||||
;
|
||||
|
||||
opt_int_type_qualifiers:
|
||||
| int_type_qualifiers
|
||||
;
|
||||
type_spec : TOK_VOID | TOK_CHAR | TOK_SHORT | TOK_INT | TOK_LONG | TOK_FLOAT
|
||||
| TOK_DOUBLE | TOK_SIGNED | TOK_UNSIGNED
|
||||
| struct_or_union_spec
|
||||
| enum_spec
|
||||
| typedef_name
|
||||
;
|
||||
|
||||
int_type_qualifiers: int_type_qualifier
|
||||
| int_type_qualifiers int_type_qualifier
|
||||
;
|
||||
type_qualifier : TOK_CONST | TOK_VOLATILE
|
||||
;
|
||||
|
||||
int_type_qualifier: TOK_VOLATILE
|
||||
| TOK_CONST
|
||||
| TOK_STATIC
|
||||
| TOK_EXTERN
|
||||
| TOK_SIGNED
|
||||
| TOK_UNSIGNED
|
||||
| TOK_SHORT
|
||||
| TOK_LONG
|
||||
;
|
||||
struct_or_union_spec : struct_or_union TOK_IDENTIFIER TOK_LCURLY struct_decl_list TOK_RCURLY
|
||||
| struct_or_union TOK_LCURLY struct_decl_list TOK_RCURLY
|
||||
| struct_or_union TOK_IDENTIFIER
|
||||
;
|
||||
|
||||
opt_float_type_qualifiers:
|
||||
| float_type_qualifiers
|
||||
;
|
||||
struct_or_union : TOK_STRUCT | TOK_UNION
|
||||
;
|
||||
|
||||
float_type_qualifiers: float_type_qualifier
|
||||
| float_type_qualifiers float_type_qualifier
|
||||
;
|
||||
struct_decl_list : struct_decl
|
||||
| struct_decl_list struct_decl
|
||||
;
|
||||
|
||||
float_type_qualifier: TOK_VOLATILE
|
||||
| TOK_CONST
|
||||
| TOK_STATIC
|
||||
| TOK_EXTERN
|
||||
| TOK_LONG
|
||||
;
|
||||
init_declarator_list : init_declarator
|
||||
| init_declarator_list TOK_COMMA init_declarator
|
||||
;
|
||||
|
||||
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
|
||||
;
|
||||
|
||||
parameter_list: parameter
|
||||
| parameter_list TOK_COMMA parameter
|
||||
;
|
||||
spec_qualifier_list : type_spec spec_qualifier_list
|
||||
| type_spec
|
||||
| type_qualifier spec_qualifier_list
|
||||
| type_qualifier
|
||||
;
|
||||
|
||||
parameter: type TOK_IDENTIFIER
|
||||
;
|
||||
struct_declarator_list : struct_declarator
|
||||
| struct_declarator_list TOK_COMMA struct_declarator
|
||||
;
|
||||
|
||||
compound_statement: TOK_LBRACKET opt_statement_list TOK_RBRACKET
|
||||
;
|
||||
struct_declarator : declarator
|
||||
| declarator TOK_COLON const_exp
|
||||
| TOK_COLON const_exp
|
||||
;
|
||||
|
||||
opt_statement_list:
|
||||
| statement_list
|
||||
;
|
||||
enum_spec : TOK_ENUM TOK_IDENTIFIER TOK_LCURLY enumerator_list TOK_RCURLY
|
||||
| TOK_ENUM TOK_LCURLY enumerator_list TOK_RCURLY
|
||||
| TOK_ENUM TOK_IDENTIFIER
|
||||
;
|
||||
|
||||
statement_list: statement
|
||||
| statement_list statement
|
||||
;
|
||||
enumerator_list : enumerator
|
||||
| enumerator_list TOK_COMMA enumerator
|
||||
;
|
||||
|
||||
statement: declaration
|
||||
| expression TOK_SEMICOLON
|
||||
;
|
||||
enumerator : TOK_IDENTIFIER
|
||||
| TOK_IDENTIFIER TOK_ASSIGN const_exp
|
||||
;
|
||||
|
||||
expression: TOK_IDENTIFIER
|
||||
;
|
||||
declarator : pointer direct_declarator
|
||||
| direct_declarator
|
||||
;
|
||||
|
||||
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