parser/c_compiler/parser.y
changeset 37 0fbbe329c3a2
child 39 46d7ec9d63bd
equal deleted inserted replaced
36:f52a9deddfc0 37:0fbbe329c3a2
       
     1 %{
       
     2 	#include "prototypes.h"
       
     3 %}
       
     4 %token IDENTIFIER CONSTANT STRING_LITERAL SIZEOF
       
     5 %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
       
     6 %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
       
     7 %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
       
     8 %token XOR_ASSIGN OR_ASSIGN TYPE_NAME
       
     9 
       
    10 %token TYPEDEF EXTERN STATIC AUTO REGISTER
       
    11 %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID
       
    12 %token STRUCT UNION ENUM ELIPSIS RANGE
       
    13 
       
    14 %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
       
    15 
       
    16 %start file
       
    17 %%
       
    18 
       
    19 primary_expr
       
    20 	: identifier
       
    21 	| CONSTANT
       
    22 	| STRING_LITERAL
       
    23 	| '(' expr ')'
       
    24 	;
       
    25 
       
    26 postfix_expr
       
    27 	: primary_expr
       
    28 	| postfix_expr '[' expr ']'
       
    29 	| postfix_expr '(' ')'
       
    30 	| postfix_expr '(' argument_expr_list ')'
       
    31 	| postfix_expr '.' identifier
       
    32 	| postfix_expr PTR_OP identifier
       
    33 	| postfix_expr INC_OP
       
    34 	| postfix_expr DEC_OP
       
    35 	;
       
    36 
       
    37 argument_expr_list
       
    38 	: assignment_expr
       
    39 	| argument_expr_list ',' assignment_expr
       
    40 	;
       
    41 
       
    42 unary_expr
       
    43 	: postfix_expr
       
    44 	| INC_OP unary_expr
       
    45 	| DEC_OP unary_expr
       
    46 	| unary_operator cast_expr
       
    47 	| SIZEOF unary_expr
       
    48 	| SIZEOF '(' type_name ')'
       
    49 	;
       
    50 
       
    51 unary_operator
       
    52 	: '&'
       
    53 	| '*'
       
    54 	| '+'
       
    55 	| '-'
       
    56 	| '~'
       
    57 	| '!'
       
    58 	;
       
    59 
       
    60 cast_expr
       
    61 	: unary_expr
       
    62 	| '(' type_name ')' cast_expr
       
    63 	;
       
    64 
       
    65 multiplicative_expr
       
    66 	: cast_expr
       
    67 	| multiplicative_expr '*' cast_expr
       
    68 	| multiplicative_expr '/' cast_expr
       
    69 	| multiplicative_expr '%' cast_expr
       
    70 	;
       
    71 
       
    72 additive_expr
       
    73 	: multiplicative_expr
       
    74 	| additive_expr '+' multiplicative_expr
       
    75 	| additive_expr '-' multiplicative_expr
       
    76 	;
       
    77 
       
    78 shift_expr
       
    79 	: additive_expr
       
    80 	| shift_expr LEFT_OP additive_expr
       
    81 	| shift_expr RIGHT_OP additive_expr
       
    82 	;
       
    83 
       
    84 relational_expr
       
    85 	: shift_expr
       
    86 	| relational_expr '<' shift_expr
       
    87 	| relational_expr '>' shift_expr
       
    88 	| relational_expr LE_OP shift_expr
       
    89 	| relational_expr GE_OP shift_expr
       
    90 	;
       
    91 
       
    92 equality_expr
       
    93 	: relational_expr
       
    94 	| equality_expr EQ_OP relational_expr
       
    95 	| equality_expr NE_OP relational_expr
       
    96 	;
       
    97 
       
    98 and_expr
       
    99 	: equality_expr
       
   100 	| and_expr '&' equality_expr
       
   101 	;
       
   102 
       
   103 exclusive_or_expr
       
   104 	: and_expr
       
   105 	| exclusive_or_expr '^' and_expr
       
   106 	;
       
   107 
       
   108 inclusive_or_expr
       
   109 	: exclusive_or_expr
       
   110 	| inclusive_or_expr '|' exclusive_or_expr
       
   111 	;
       
   112 
       
   113 logical_and_expr
       
   114 	: inclusive_or_expr
       
   115 	| logical_and_expr AND_OP inclusive_or_expr
       
   116 	;
       
   117 
       
   118 logical_or_expr
       
   119 	: logical_and_expr
       
   120 	| logical_or_expr OR_OP logical_and_expr
       
   121 	;
       
   122 
       
   123 conditional_expr
       
   124 	: logical_or_expr
       
   125 	| logical_or_expr '?' logical_or_expr ':' conditional_expr
       
   126 	;
       
   127 
       
   128 assignment_expr
       
   129 	: conditional_expr
       
   130 	| unary_expr assignment_operator assignment_expr
       
   131 	;
       
   132 
       
   133 assignment_operator
       
   134 	: '='
       
   135 	| MUL_ASSIGN
       
   136 	| DIV_ASSIGN
       
   137 	| MOD_ASSIGN
       
   138 	| ADD_ASSIGN
       
   139 	| SUB_ASSIGN
       
   140 	| LEFT_ASSIGN
       
   141 	| RIGHT_ASSIGN
       
   142 	| AND_ASSIGN
       
   143 	| XOR_ASSIGN
       
   144 	| OR_ASSIGN
       
   145 	;
       
   146 
       
   147 expr
       
   148 	: assignment_expr
       
   149 	| expr ',' assignment_expr
       
   150 	;
       
   151 
       
   152 constant_expr
       
   153 	: conditional_expr
       
   154 	;
       
   155 
       
   156 declaration
       
   157 	: declaration_specifiers ';'
       
   158 	| declaration_specifiers init_declarator_list ';'
       
   159 	;
       
   160 
       
   161 declaration_specifiers
       
   162 	: storage_class_specifier
       
   163 	| storage_class_specifier declaration_specifiers
       
   164 	| type_specifier
       
   165 	| type_specifier declaration_specifiers
       
   166 	;
       
   167 
       
   168 init_declarator_list
       
   169 	: init_declarator
       
   170 	| init_declarator_list ',' init_declarator
       
   171 	;
       
   172 
       
   173 init_declarator
       
   174 	: declarator
       
   175 	| declarator '=' initializer
       
   176 	;
       
   177 
       
   178 storage_class_specifier
       
   179 	: TYPEDEF
       
   180 	| EXTERN
       
   181 	| STATIC
       
   182 	| AUTO
       
   183 	| REGISTER
       
   184 	;
       
   185 
       
   186 type_specifier
       
   187 	: CHAR
       
   188 	| SHORT
       
   189 	| INT
       
   190 	| LONG
       
   191 	| SIGNED
       
   192 	| UNSIGNED
       
   193 	| FLOAT
       
   194 	| DOUBLE
       
   195 	| CONST
       
   196 	| VOLATILE
       
   197 	| VOID
       
   198 	| struct_or_union_specifier
       
   199 	| enum_specifier
       
   200 	| TYPE_NAME
       
   201 	;
       
   202 
       
   203 struct_or_union_specifier
       
   204 	: struct_or_union identifier '{' struct_declaration_list '}'
       
   205 	| struct_or_union '{' struct_declaration_list '}'
       
   206 	| struct_or_union identifier
       
   207 	;
       
   208 
       
   209 struct_or_union
       
   210 	: STRUCT
       
   211 	| UNION
       
   212 	;
       
   213 
       
   214 struct_declaration_list
       
   215 	: struct_declaration
       
   216 	| struct_declaration_list struct_declaration
       
   217 	;
       
   218 
       
   219 struct_declaration
       
   220 	: type_specifier_list struct_declarator_list ';'
       
   221 	;
       
   222 
       
   223 struct_declarator_list
       
   224 	: struct_declarator
       
   225 	| struct_declarator_list ',' struct_declarator
       
   226 	;
       
   227 
       
   228 struct_declarator
       
   229 	: declarator
       
   230 	| ':' constant_expr
       
   231 	| declarator ':' constant_expr
       
   232 	;
       
   233 
       
   234 enum_specifier
       
   235 	: ENUM '{' enumerator_list '}'
       
   236 	| ENUM identifier '{' enumerator_list '}'
       
   237 	| ENUM identifier
       
   238 	;
       
   239 
       
   240 enumerator_list
       
   241 	: enumerator
       
   242 	| enumerator_list ',' enumerator
       
   243 	;
       
   244 
       
   245 enumerator
       
   246 	: identifier
       
   247 	| identifier '=' constant_expr
       
   248 	;
       
   249 
       
   250 declarator
       
   251 	: declarator2
       
   252 	| pointer declarator2
       
   253 	;
       
   254 
       
   255 declarator2
       
   256 	: identifier
       
   257 	| '(' declarator ')'
       
   258 	| declarator2 '[' ']'
       
   259 	| declarator2 '[' constant_expr ']'
       
   260 	| declarator2 '(' ')'
       
   261 	| declarator2 '(' parameter_type_list ')'
       
   262 	| declarator2 '(' parameter_identifier_list ')'
       
   263 	;
       
   264 
       
   265 pointer
       
   266 	: '*'
       
   267 	| '*' type_specifier_list
       
   268 	| '*' pointer
       
   269 	| '*' type_specifier_list pointer
       
   270 	;
       
   271 
       
   272 type_specifier_list
       
   273 	: type_specifier
       
   274 	| type_specifier_list type_specifier
       
   275 	;
       
   276 
       
   277 parameter_identifier_list
       
   278 	: identifier_list
       
   279 	| identifier_list ',' ELIPSIS
       
   280 	;
       
   281 
       
   282 identifier_list
       
   283 	: identifier
       
   284 	| identifier_list ',' identifier
       
   285 	;
       
   286 
       
   287 parameter_type_list
       
   288 	: parameter_list
       
   289 	| parameter_list ',' ELIPSIS
       
   290 	;
       
   291 
       
   292 parameter_list
       
   293 	: parameter_declaration
       
   294 	| parameter_list ',' parameter_declaration
       
   295 	;
       
   296 
       
   297 parameter_declaration
       
   298 	: type_specifier_list declarator
       
   299 	| type_name
       
   300 	;
       
   301 
       
   302 type_name
       
   303 	: type_specifier_list
       
   304 	| type_specifier_list abstract_declarator
       
   305 	;
       
   306 
       
   307 abstract_declarator
       
   308 	: pointer
       
   309 	| abstract_declarator2
       
   310 	| pointer abstract_declarator2
       
   311 	;
       
   312 
       
   313 abstract_declarator2
       
   314 	: '(' abstract_declarator ')'
       
   315 	| '[' ']'
       
   316 	| '[' constant_expr ']'
       
   317 	| abstract_declarator2 '[' ']'
       
   318 	| abstract_declarator2 '[' constant_expr ']'
       
   319 	| '(' ')'
       
   320 	| '(' parameter_type_list ')'
       
   321 	| abstract_declarator2 '(' ')'
       
   322 	| abstract_declarator2 '(' parameter_type_list ')'
       
   323 	;
       
   324 
       
   325 initializer
       
   326 	: assignment_expr
       
   327 	| '{' initializer_list '}'
       
   328 	| '{' initializer_list ',' '}'
       
   329 	;
       
   330 
       
   331 initializer_list
       
   332 	: initializer
       
   333 	| initializer_list ',' initializer
       
   334 	;
       
   335 
       
   336 statement
       
   337 	: labeled_statement
       
   338 	| compound_statement
       
   339 	| expression_statement
       
   340 	| selection_statement
       
   341 	| iteration_statement
       
   342 	| jump_statement
       
   343 	;
       
   344 
       
   345 labeled_statement
       
   346 	: identifier ':' statement
       
   347 	| CASE constant_expr ':' statement
       
   348 	| DEFAULT ':' statement
       
   349 	;
       
   350 
       
   351 compound_statement
       
   352 	: '{' '}'
       
   353 	| '{' statement_list '}'
       
   354 	| '{' declaration_list '}'
       
   355 	| '{' declaration_list statement_list '}'
       
   356 	;
       
   357 
       
   358 declaration_list
       
   359 	: declaration
       
   360 	| declaration_list declaration
       
   361 	;
       
   362 
       
   363 statement_list
       
   364 	: statement
       
   365 	| statement_list statement
       
   366 	;
       
   367 
       
   368 expression_statement
       
   369 	: ';'
       
   370 	| expr ';'
       
   371 	;
       
   372 
       
   373 selection_statement
       
   374 	: IF '(' expr ')' statement
       
   375 	| IF '(' expr ')' statement ELSE statement
       
   376 	| SWITCH '(' expr ')' statement
       
   377 	;
       
   378 
       
   379 iteration_statement
       
   380 	: WHILE '(' expr ')' statement
       
   381 	| DO statement WHILE '(' expr ')' ';'
       
   382 	| FOR '(' ';' ';' ')' statement
       
   383 	| FOR '(' ';' ';' expr ')' statement
       
   384 	| FOR '(' ';' expr ';' ')' statement
       
   385 	| FOR '(' ';' expr ';' expr ')' statement
       
   386 	| FOR '(' expr ';' ';' ')' statement
       
   387 	| FOR '(' expr ';' ';' expr ')' statement
       
   388 	| FOR '(' expr ';' expr ';' ')' statement
       
   389 	| FOR '(' expr ';' expr ';' expr ')' statement
       
   390 	;
       
   391 
       
   392 jump_statement
       
   393 	: GOTO identifier ';'
       
   394 	| CONTINUE ';'
       
   395 	| BREAK ';'
       
   396 	| RETURN ';'
       
   397 	| RETURN expr ';'
       
   398 	;
       
   399 
       
   400 file
       
   401 	: external_definition
       
   402 	| file external_definition
       
   403 	;
       
   404 
       
   405 external_definition
       
   406 	: function_definition
       
   407 	| declaration
       
   408 	;
       
   409 
       
   410 function_definition
       
   411 	: declarator function_body
       
   412 	| declaration_specifiers declarator function_body
       
   413 	;
       
   414 
       
   415 function_body
       
   416 	: compound_statement
       
   417 	| declaration_list compound_statement
       
   418 	;
       
   419 
       
   420 identifier
       
   421 	: IDENTIFIER
       
   422 	;
       
   423 %%
       
   424 
       
   425 #include <stdio.h>
       
   426 
       
   427 extern YYSTYPE yytext;
       
   428 extern int column;
       
   429 
       
   430 void yyerror(char *s)
       
   431 {
       
   432 	fflush(stdout);
       
   433 	printf("[%4d]: %s\n", column, yytext);
       
   434 	printf("\n%*s\n%*s\n", column, "^", column, s);
       
   435 }