start rewriting grammar

This commit is contained in:
Josh Holtrop 2018-04-24 21:10:48 -04:00
parent 89e003b330
commit e7835b4fac

View File

@ -112,340 +112,103 @@ int yylex(YYSTYPE *, YYLTYPE *);
%%
translation_unit: external_declaration
| translation_unit external_declaration
translation_unit: translation_unit_item
| translation_unit translation_unit_item
;
constant: TOK_FLOAT_CONST
| TOK_INT_CONST
| TOK_IDENTIFIER
| TOK_CHAR_CONST
translation_unit_item: declaration
| function_definition
;
declaration: type TOK_IDENTIFIER TOK_SEMICOLON
| typedef_declaration
;
typedef_declaration: TOK_TYPEDEF type TOK_IDENTIFIER TOK_SEMICOLON
;
type: TOK_VOID
| opt_int_type_qualifiers int_type
| opt_float_type_qualifiers float_type
;
int_type: TOK_CHAR
| TOK_SHORT
| TOK_INT
| TOK_LONG
| TOK_LONG TOK_LONG
;
primary_expression: TOK_IDENTIFIER
| constant
| TOK_STR_CONST
| TOK_LPAREN expression TOK_RPAREN
;
expression: assignment_expression
| expression TOK_COMMA assignment_expression
float_type: TOK_FLOAT
| TOK_DOUBLE
;
constant_expression: conditional_expression
opt_int_type_qualifiers:
| int_type_qualifiers
;
int_type_qualifiers: int_type_qualifier
| int_type_qualifiers int_type_qualifier
;
conditional_expression: logical_or_expression
| logical_or_expression TOK_QUESTION expression TOK_COLON conditional_expression
;
assignment_expression: conditional_expression
| unary_expression TOK_ASSIGN assignment_expression
;
postfix_expression: primary_expression
| postfix_expression TOK_LBRACKET expression TOK_RBRACKET
| postfix_expression TOK_LPAREN TOK_RPAREN
| postfix_expression TOK_LPAREN argument_expression_list TOK_RPAREN
| postfix_expression TOK_DOT TOK_IDENTIFIER
| postfix_expression TOK_ARROW TOK_IDENTIFIER
| postfix_expression TOK_INCREMENT
| postfix_expression TOK_DECREMENT
int_type_qualifier: TOK_VOLATILE
| TOK_CONST
| TOK_STATIC
| TOK_EXTERN
| TOK_SIGNED
| TOK_UNSIGNED
| TOK_SHORT
| TOK_LONG
;
argument_expression_list: assignment_expression
| argument_expression_list TOK_COMMA assignment_expression
;
unary_expression: postfix_expression
| TOK_INCREMENT unary_expression
| TOK_DECREMENT unary_expression
| unary_operator
| cast_expression
| TOK_SIZEOF unary_expression
| TOK_SIZEOF TOK_LPAREN type_name TOK_RPAREN
;
unary_operator: TOK_BITAND
| TOK_TIMES
| TOK_PLUS
| TOK_MINUS
| TOK_BITNOT
| TOK_NOT
;
cast_expression: unary_expression
| TOK_LPAREN type_name TOK_RPAREN cast_expression
;
multiplicative_expression: cast_expression
| multiplicative_expression TOK_TIMES cast_expression
| multiplicative_expression TOK_DIVIDE cast_expression
| multiplicative_expression TOK_MOD cast_expression
opt_float_type_qualifiers:
| float_type_qualifiers
;
additive_expression: multiplicative_expression
| additive_expression TOK_PLUS multiplicative_expression
| additive_expression TOK_MINUS multiplicative_expression
;
shift_expression: additive_expression
| shift_expression TOK_LSHIFT additive_expression
| shift_expression TOK_RSHIFT additive_expression
;
relational_expression: shift_expression
| relational_expression TOK_LESS shift_expression
| relational_expression TOK_GREATER shift_expression
| relational_expression TOK_LESSEQ shift_expression
| relational_expression TOK_GREATEREQ shift_expression
float_type_qualifiers: float_type_qualifier
| float_type_qualifiers float_type_qualifier
;
equality_expression: relational_expression
| equality_expression TOK_EQUALS relational_expression
| equality_expression TOK_NOTEQUALS relational_expression
;
and_expression: equality_expression
| and_expression TOK_BITAND equality_expression
;
exclusive_or_expression: and_expression
| exclusive_or_expression TOK_XOR and_expression
;
inclusive_or_expression: exclusive_or_expression
| inclusive_or_expression TOK_BITOR exclusive_or_expression
;
logical_and_expression: inclusive_or_expression
| logical_and_expression TOK_AND inclusive_or_expression
;
logical_or_expression: logical_and_expression
| logical_or_expression TOK_OR logical_and_expression
;
declaration: declaration_specifiers
| declaration_specifiers init_declarator_list
;
declaration_specifiers: storage_class_specifier
| storage_class_specifier declaration_specifiers
| type_specifier
| type_specifier declaration_specifiers
| type_qualifier
| type_qualifier declaration_specifiers
;
init_declarator_list: init_declarator
| init_declarator_list TOK_COMMA init_declarator
float_type_qualifier: TOK_VOLATILE
| TOK_CONST
| TOK_STATIC
| TOK_EXTERN
| TOK_LONG
;
init_declarator: declarator
| declarator TOK_ASSIGN initializer
;
storage_class_specifier: TOK_AUTO
| TOK_REGISTER
| TOK_STATIC
| TOK_EXTERN
| TOK_TYPEDEF
;
type_specifier: TOK_VOID
| TOK_CHAR
| TOK_SHORT
| TOK_INT
| TOK_LONG
| TOK_FLOAT
| TOK_DOUBLE
| TOK_SIGNED
| TOK_UNSIGNED
| struct_or_union_specifier
| enum_specifier
| typedef_name
;
type_qualifier: TOK_CONST
| TOK_VOLATILE
;
declarator: direct_declarator
| pointer direct_declarator
;
direct_declarator: TOK_IDENTIFIER
| TOK_LPAREN declarator TOK_RPAREN
| direct_declarator TOK_LBRACKET TOK_RBRACKET
| direct_declarator TOK_LBRACKET constant_expression TOK_RBRACKET
| direct_declarator TOK_LPAREN parameter_type_list TOK_RPAREN
| direct_declarator TOK_LPAREN TOK_RPAREN
| direct_declarator TOK_LPAREN identifier_list TOK_RPAREN
;
pointer: TOK_TIMES
| TOK_TIMES type_qualifier_list
| TOK_TIMES pointer
;
parameter_type_list: parameter_list
| parameter_list TOK_COMMA TOK_ELLIPSES
function_definition: function_header compound_statement
;
parameter_list: parameter_declaration
| parameter_list TOK_COMMA parameter_declaration
;
type_qualifier_list: type_qualifier
| type_qualifier_list type_qualifier
;
enum_specifier: TOK_ENUM TOK_LBRACKET enumerator_list TOK_RBRACKET
| TOK_ENUM TOK_IDENTIFIER TOK_LBRACKET enumerator_list TOK_RBRACKET
| TOK_ENUM TOK_IDENTIFIER
;
enumerator_list: enumerator
| enumerator_list TOK_COMMA enumerator
function_header: type TOK_IDENTIFIER TOK_LPAREN opt_parameter_list TOK_RPAREN
;
enumerator: enumeration_constant
| enumeration_constant TOK_ASSIGN constant_expression
;
enumeration_constant: TOK_IDENTIFIER;
struct_or_union_specifier: struct_or_union TOK_LBRACKET struct_declaration_list TOK_RBRACKET
| struct_or_union TOK_IDENTIFIER TOK_LBRACKET struct_declaration_list TOK_RBRACKET
| struct_or_union TOK_IDENTIFIER
;
struct_or_union: TOK_STRUCT
| TOK_UNION
;
struct_declaration_list: struct_declaration
| struct_declaration_list struct_declaration
;
struct_declaration: specifier_qualifier_list struct_declarator_list;
specifier_qualifier_list: type_specifier
| type_specifier specifier_qualifier_list
| type_qualifier
| type_qualifier specifier_qualifier_list
;
struct_declarator_list: struct_declarator
| struct_declarator_list TOK_COMMA struct_declarator
;
struct_declarator: declarator
| type_specifier TOK_COLON constant_expression
| type_specifier declarator TOK_COLON constant_expression
;
parameter_declaration: declaration_specifiers declarator
| declaration_specifiers
| declaration_specifiers abstract_declarator
;
identifier_list: TOK_IDENTIFIER
| identifier_list TOK_COMMA TOK_IDENTIFIER
;
abstract_declarator: pointer
| direct_abstract_declarator
| pointer direct_abstract_declarator
;
direct_abstract_declarator: TOK_LPAREN abstract_declarator TOK_RPAREN
| TOK_LBRACKET TOK_RBRACKET
| direct_abstract_declarator TOK_LBRACKET TOK_RBRACKET
| TOK_LBRACKET constant_expression TOK_RBRACKET
| direct_abstract_declarator TOK_LBRACKET constant_expression TOK_RBRACKET
| TOK_LPAREN TOK_RPAREN
| direct_abstract_declarator TOK_LPAREN TOK_RPAREN
| TOK_LPAREN parameter_type_list TOK_RPAREN
| direct_abstract_declarator TOK_LPAREN parameter_type_list TOK_RPAREN
;
initializer: assignment_expression
| TOK_LBRACKET initializer_list TOK_RBRACKET
| TOK_LBRACKET initializer_list TOK_COMMA TOK_RBRACKET
;
initializer_list: initializer
| initializer_list TOK_COMMA initializer
;
type_name: specifier_qualifier_list
| specifier_qualifier_list abstract_declarator
;
typedef_name: TOK_IDENTIFIER;
statement: labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
jump_statement: TOK_GOTO TOK_IDENTIFIER TOK_SEMICOLON
| TOK_CONTINUE TOK_SEMICOLON
| TOK_BREAK TOK_SEMICOLON
| TOK_RETURN TOK_SEMICOLON
| TOK_RETURN expression TOK_SEMICOLON
;
compound_statement: TOK_LBRACKET TOK_RBRACKET
| TOK_LBRACKET declaration_list TOK_RBRACKET
| TOK_LBRACKET statement_list TOK_RBRACKET
| TOK_LBRACKET declaration_list TOK_RBRACKET
| TOK_LBRACKET declaration_list statement_list TOK_RBRACKET
opt_parameter_list:
| parameter_list
;
declaration_list: declaration
| declaration_list declaration
;
parameter_list: parameter
| parameter_list TOK_COMMA parameter
;
parameter: type TOK_IDENTIFIER
;
compound_statement: TOK_LBRACKET opt_statement_list TOK_RBRACKET
;
opt_statement_list:
| statement_list
;
statement_list: statement
| statement_list statement
;
expression_statement: TOK_SEMICOLON
| expression TOK_SEMICOLON
;
statement: declaration
| expression TOK_SEMICOLON
;
iteration_statement: TOK_WHILE TOK_LPAREN expression TOK_RPAREN statement
| TOK_DO statement TOK_WHILE TOK_LPAREN expression TOK_RPAREN TOK_SEMICOLON
| TOK_FOR TOK_LPAREN expressionopt TOK_SEMICOLON expressionopt TOK_SEMICOLON expressionopt TOK_RPAREN statement
;
expression: TOK_IDENTIFIER
;
expressionopt:
| expression
;
selection_statement: TOK_IF TOK_LPAREN expression TOK_RPAREN statement
| TOK_IF TOK_LPAREN expression TOK_RPAREN statement TOK_ELSE statement
| TOK_SWITCH TOK_LPAREN expression TOK_RPAREN statement
;
labeled_statement: TOK_IDENTIFIER TOK_COLON statement
| TOK_CASE constant_expression TOK_COLON statement
| TOK_DEFAULT TOK_COLON statement
;
external_declaration: function_definition
| declaration
;
function_definition: declarator declaration_list compound_statement
| declaration_specifiers declarator declaration_list compound_statement
| declarator compound_statement
| declaration_specifiers declarator compound_statement
;
%%