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