home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d156 / grammars.lha / Grammars / C / gram.y next >
Text File  |  1988-10-02  |  9KB  |  432 lines

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