home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_07 / v7n7073a.txt < prev    next >
Text File  |  1989-05-29  |  9KB  |  485 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 program
  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.     | TYPEDEF { in_tdef = TRUE; su = FALSE; } tdef ';' { in_tdef = FALSE; }
  157.     ;
  158.  
  159. tdef
  160.     : type_specifier_list opt_decl
  161.     | declarator
  162.     ;
  163.  
  164. opt_decl
  165.     :
  166.     | declarator
  167.     ;
  168.  
  169. declaration_specifiers
  170.     : storage_class_specifier
  171.     | storage_class_specifier declaration_specifiers
  172.     | type_specifier
  173.     | type_specifier declaration_specifiers
  174.     ;
  175.  
  176. init_declarator_list
  177.     : init_declarator
  178.     | init_declarator_list ',' init_declarator
  179.     ;
  180.  
  181. init_declarator
  182.     : declarator
  183.     | declarator '=' initializer
  184.     ;
  185.  
  186. storage_class_specifier
  187.     :  EXTERN
  188.     | STATIC
  189.     | AUTO
  190.     | REGISTER
  191.     ;
  192.  
  193. type_specifier
  194.     : CHAR
  195.     | SHORT
  196.     | INT
  197.     | LONG
  198.     | SIGNED
  199.     | UNSIGNED
  200.     | FLOAT
  201.     | DOUBLE
  202.     | CONST
  203.     | VOLATILE
  204.     | VOID
  205.     | struct_or_union_specifier  { su = ( su > 0 ? --su : 0); }
  206.     | enum_specifier  { enumflag = FALSE; }
  207.     | TYPE_NAME
  208.     ;
  209.  
  210. struct_or_union_specifier
  211.     : struct_or_union identifier '{' struct_declaration_list '}'
  212.     | struct_or_union '{' struct_declaration_list '}'
  213.     | struct_or_union identifier
  214.     ;
  215.  
  216. struct_or_union
  217.     : STRUCT
  218.         { su += 1; }
  219.     | UNION
  220.         { su += 1; }
  221.     ;
  222.  
  223. struct_declaration_list
  224.     : struct_declaration
  225.     | struct_declaration_list struct_declaration
  226.     ;
  227.  
  228. struct_declaration
  229.     : type_specifier_list struct_declarator_list ';'
  230.     ;
  231.  
  232. struct_declarator_list
  233.     : struct_declarator
  234.     | struct_declarator_list ',' struct_declarator
  235.     ;
  236.  
  237. struct_declarator
  238.     : declarator
  239.     | ':' constant_expr
  240.     | declarator ':' constant_expr
  241.     ;
  242.  
  243. enum_specifier
  244.     : enum '{' enumerator_list '}'
  245.     | enum identifier '{' enumerator_list '}'
  246.     | enum identifier
  247.     ;
  248.  
  249. enum
  250.     : ENUM { enumflag = TRUE; }
  251.     ;
  252.  
  253. enumerator_list
  254.     : enumerator
  255.     | enumerator_list ',' enumerator
  256.     ;
  257.  
  258. enumerator
  259.     : identifier
  260.     | identifier '=' constant_expr
  261.     ;
  262.  
  263. declarator
  264.     : declarator2
  265.     | pointer declarator2
  266.     ;
  267.  
  268. declarator2
  269.     : identifier
  270.     | '(' declarator ')'
  271.     | declarator2 '[' ']'
  272.     | declarator2 '[' constant_expr ']'
  273.     | declarator2 '(' ')' 
  274.         {
  275.         func_decl = TRUE;
  276.         if ( edit_mode )
  277.             {
  278.             write_proto(NOARGS);
  279.             ed_delete();
  280.             }
  281.         }
  282.     | declarator2 '(' parameter_type_list ')'
  283.     | declarator2 '(' parameter_identifier_list
  284.         {
  285.         if ( edit_mode )
  286.             {
  287.             write_proto(WITH_ARGS);
  288.             ed_delete();
  289.             }
  290.         }  ')' 
  291.     ;
  292.  
  293. pointer
  294.     : '*'
  295.     | '*' type_specifier_list
  296.     | '*' pointer
  297.     | '*' type_specifier_list pointer
  298.     ;
  299.  
  300. type_specifier_list
  301.     : type_specifier
  302.     | type_specifier_list type_specifier
  303.     ;
  304.  
  305. parameter_identifier_list
  306.     : identifier_list
  307.     | identifier_list ',' ELIPSIS
  308.     ;
  309.  
  310. identifier_list
  311.     : identifier
  312.     | identifier_list ',' identifier
  313.     ;
  314.  
  315. parameter_type_list
  316.     : parameter_list
  317.     | parameter_list ',' ELIPSIS
  318.     ;
  319.  
  320. parameter_list
  321.     : parameter_declaration
  322.     | parameter_list ',' parameter_declaration
  323.     ;
  324.  
  325. parameter_declaration
  326.     : type_specifier_list declarator
  327.     | type_name
  328.     | REGISTER type_specifier_list declarator
  329.     ;
  330.  
  331. type_name
  332.     : type_specifier_list
  333.     | type_specifier_list abstract_declarator
  334.     ;
  335.  
  336. abstract_declarator
  337.     : pointer
  338.     | abstract_declarator2
  339.     | pointer abstract_declarator2
  340.     ;
  341.  
  342. abstract_declarator2
  343.     : '(' abstract_declarator ')'
  344.     | '[' ']'
  345.     | '[' constant_expr ']'
  346.     | abstract_declarator2 '[' ']'
  347.     | abstract_declarator2 '[' constant_expr ']'
  348.     | '(' ')'
  349.     | '(' parameter_type_list ')'
  350.     | abstract_declarator2 '(' ')'
  351.     | abstract_declarator2 '(' parameter_type_list ')'
  352.     ;
  353.  
  354. initializer
  355.     : assignment_expr
  356.     | '{' initializer_list '}'
  357.     | '{' initializer_list ',' '}'
  358.     ;
  359.  
  360. initializer_list
  361.     : initializer
  362.     | initializer_list ',' initializer
  363.     ;
  364.  
  365. statement
  366.     : labeled_statement
  367.     | compound_statement
  368.     | expression_statement
  369.     | selection_statement
  370.     | iteration_statement
  371.     | jump_statement
  372.     ;
  373.  
  374. labeled_statement
  375.     : identifier ':' statement
  376.     | CASE constant_expr ':' statement
  377.     | DEFAULT ':' statement
  378.     ;
  379.  
  380. compound_statement
  381.     : left_curly '}'
  382.     | left_curly statement_list '}'
  383.     | left_curly declaration_list '}'
  384.     | left_curly declaration_list statement_list '}'
  385.     ;
  386.  
  387. left_curly
  388.     : '{' {proto_flag=FALSE;}
  389.     ;
  390. declaration_list
  391.     : declaration
  392.     | declaration_list declaration
  393.     ;
  394.  
  395. statement_list
  396.     : statement
  397.     | statement_list statement
  398.     ;
  399.  
  400. expression_statement
  401.     : ';'
  402.     | expr ';'
  403.     ;
  404.  
  405. selection_statement
  406.     : IF '(' expr ')' statement
  407.     | IF '(' expr ')' statement ELSE statement
  408.     | SWITCH '(' expr ')' statement
  409.     ;
  410.  
  411. iteration_statement
  412.     : WHILE '(' expr ')' statement
  413.     | DO statement WHILE '(' expr ')' ';'
  414.     | FOR '(' ';' ';' ')' statement
  415.     | FOR '(' ';' ';' expr ')' statement
  416.     | FOR '(' ';' expr ';' ')' statement
  417.     | FOR '(' ';' expr ';' expr ')' statement
  418.     | FOR '(' expr ';' ';' ')' statement
  419.     | FOR '(' expr ';' ';' expr ')' statement
  420.     | FOR '(' expr ';' expr ';' ')' statement
  421.     | FOR '(' expr ';' expr ';' expr ')' statement
  422.     ;
  423.  
  424. jump_statement
  425.     : GOTO identifier ';'
  426.     | CONTINUE ';'
  427.     | BREAK ';'
  428.     | RETURN ';'
  429.     | RETURN expr ';'
  430.     ;
  431.  
  432. program
  433.     : file { ed_flush(); }
  434.     ;
  435.  
  436. file
  437.     : external_definition
  438.     | file external_definition
  439.     ;
  440.  
  441. external_definition
  442.     : function_definition
  443.     | declaration 
  444.         {
  445.         if ( func_decl == TRUE )
  446.         {
  447.         if ( edit_mode )
  448.             ed_delete();
  449.         func_decl = FALSE;
  450.         }
  451.         };
  452.  
  453. function_definition
  454.     : declarator function_body
  455.     | declaration_specifiers declarator function_body
  456.     ;
  457.  
  458. function_body
  459.     : compound_statement
  460.     | declaration_list compound_statement
  461.     ;
  462.  
  463. identifier
  464.     : IDENTIFIER
  465.         {
  466.         if ( in_tdef && su == 0 && !enumflag)
  467.         storesym(yytext,0L);  /* save only typedef names */
  468.         }
  469.     ;
  470. %%
  471.  
  472. #include <stdio.h>
  473. #include "ctocxx.h"
  474.  
  475. extern char *yytext;
  476. extern int proto_flag;
  477. extern int lineno;
  478. int in_tdef = FALSE;    /* flag used to control storing typedef idents in table */
  479. int su = FALSE;        /* TRUE if just after a struct/union keyword */
  480. int enumflag = FALSE;    /* TRUE if in an enum specifier */
  481. int func_decl = FALSE;
  482. int edit_mode = FALSE;  /* TRUE only when in the original source file, not includes */
  483. extern FILE *efd;
  484.  
  485.