home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume1 / ansi.c / gram.y next >
Encoding:
Text File  |  1986-11-30  |  7.2 KB  |  434 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.     | primary_expr '[' expr ']'
  22.     | primary_expr '(' ')'
  23.     | primary_expr '(' argument_expr_list ')'
  24.     | primary_expr '.' identifier
  25.     | primary_expr PTR_OP identifier
  26.     ;
  27.  
  28. argument_expr_list
  29.     : assignment_expr
  30.     | argument_expr_list ',' assignment_expr
  31.     ;
  32.  
  33. postfix_expr
  34.     : primary_expr
  35.     | primary_expr INC_OP
  36.     | primary_expr DEC_OP
  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.     : ssc_specifier
  160.     | ssc_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. ssc_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_declaration_list ')'
  259.     ;
  260.  
  261. pointer
  262.     : '*'
  263.     | '*' type_specifier_list
  264.     | '*' pointer
  265.     | '*' type_specifier_list pointer
  266.     ;
  267.  
  268. type_specifier_list
  269.     : type_specifier
  270.     | type_specifier_list type_specifier
  271.     ;
  272.  
  273. parameter_declaration_list
  274.     : identifier_list
  275.     | identifier_list ',' ELIPSIS
  276.     | parameter_types
  277.     ;
  278.  
  279. identifier_list
  280.     : identifier
  281.     | identifier_list ',' identifier
  282.     ;
  283.  
  284. parameter_types
  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_types ')'
  318.     | abstract_declarator2 '(' ')'
  319.     | abstract_declarator2 '(' parameter_types ')'
  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.     | CASE constant_expr RANGE constant_expr ':' statement
  346.     | DEFAULT ':' statement
  347.     ;
  348.  
  349. compound_statement
  350.     : '{' '}'
  351.     | '{' statement_list '}'
  352.     | '{' declaration_list '}'
  353.     | '{' declaration_list statement_list '}'
  354.     ;
  355.  
  356. declaration_list
  357.     : declaration
  358.     | declaration_list declaration
  359.     ;
  360.  
  361. statement_list
  362.     : statement
  363.     | statement_list statement
  364.     ;
  365.  
  366. expression_statement
  367.     : ';'
  368.     | expr ';'
  369.     ;
  370.  
  371. selection_statement
  372.     : IF '(' expr ')' statement
  373.     | IF '(' expr ')' statement ELSE statement
  374.     | SWITCH '(' expr ')' statement
  375.     ;
  376.  
  377. iteration_statement
  378.     : WHILE '(' expr ')' statement
  379.     | DO statement WHILE '(' expr ')' ';'
  380.     | FOR '(' ';' ';' ')' statement
  381.     | FOR '(' ';' ';' expr ')' statement
  382.     | FOR '(' ';' expr ';' ')' statement
  383.     | FOR '(' ';' expr ';' expr ')' statement
  384.     | FOR '(' expr ';' ';' ')' statement
  385.     | FOR '(' expr ';' ';' expr ')' statement
  386.     | FOR '(' expr ';' expr ';' ')' statement
  387.     | FOR '(' expr ';' expr ';' expr ')' statement
  388.     ;
  389.  
  390. jump_statement
  391.     : GOTO identifier ';'
  392.     | CONTINUE ';'
  393.     | BREAK ';'
  394.     | RETURN ';'
  395.     | RETURN expr ';'
  396.     ;
  397.  
  398. file
  399.     : external_definition
  400.     | file external_definition
  401.     ;
  402.  
  403. external_definition
  404.     : function_definition
  405.     | declaration
  406.     ;
  407.  
  408. function_definition
  409.     : declarator function_body
  410.     | declaration_specifiers declarator function_body
  411.     ;
  412.  
  413. function_body
  414.     : compound_statement
  415.     | declaration_list compound_statement
  416.     ;
  417.  
  418. identifier
  419.     : IDENTIFIER
  420.     ;
  421. %%
  422.  
  423. #include <stdio.h>
  424.  
  425. extern char *yytext;
  426. extern int column;
  427.  
  428. yyerror(s)
  429. char *s;
  430. {
  431.     fflush(stdout);
  432.     printf("\n%*s\n%*s\n", column, "^", column, s);
  433. }
  434.