home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / compcomp / tpyacc / pas.y < prev    next >
Text File  |  1991-06-17  |  13KB  |  594 lines

  1.  
  2. /* PAS.Y: ISO Level 0 Pascal grammar, adapted to TP Yacc 2-28-89 AG
  3.    To compile: yacc pas
  4.                lex paslex
  5.                tpc pas */
  6.  
  7. %{
  8. (*
  9.  *
  10.  * Pascal grammar in Yacc format, based originally on BNF given
  11.  * in "Standard Pascal -- User Reference Manual", by Doug Cooper.
  12.  * This in turn is the BNF given by the ANSI and ISO Pascal standards,
  13.  * and so, is PUBLIC DOMAIN. The grammar is for ISO Level 0 Pascal.
  14.  * The grammar has been massaged somewhat to make it LALR, and added
  15.  * the following extensions.
  16.  *
  17.  * constant expressions
  18.  * otherwise statement in a case
  19.  * productions to correctly match else's with if's
  20.  * beginnings of a separate compilation facility
  21.  *
  22.  *)
  23.  
  24. uses LexLib, YaccLib;
  25.  
  26. var filename : String;
  27.  
  28. procedure yyerror(msg : string);
  29.   begin
  30.     writeln(filename, ': ', yylineno, ': ',
  31.             msg, ' at or before `', yytext, '''.')
  32.   end(*yyerror*);
  33.  
  34. %}
  35.  
  36. /* Note the Pascal keyword tokens are stropped with leading underscore
  37.    (e.g. _AND) in the Turbo Pascal Yacc version, because these identifiers
  38.    will be declared as token numbers in the output file generated by Yacc,
  39.    and hence must not collide with Turbo Pascal keywords. */
  40.  
  41. %token _AND _ARRAY ASSIGNMENT _BEGIN _CASE CHARACTER_STRING COLON COMMA _CONST DIGSEQ
  42. %token _DIV _DO DOT DOTDOT _DOWNTO _ELSE _END EQUAL _EXTERNAL _FILE _FOR _FORWARD _FUNCTION
  43. %token GE _GOTO GT IDENTIFIER _IF _IN _LABEL LBRAC LE LPAREN LT MINUS _MOD _NIL _NOT
  44. %token NOTEQUAL _OF _OR _OTHERWISE _PACKED PLUS _PROCEDURE _PROGRAM RBRAC
  45. %token REALNUMBER _RECORD _REPEAT RPAREN SEMICOLON _SET SLASH STAR STARSTAR _THEN
  46. %token _TO _TYPE _UNTIL UPARROW _VAR _WHILE _WITH
  47.  
  48. %token ILLEGAL
  49.  
  50. %%
  51.  
  52. file : program
  53.     | program error
  54.     { writeln(yylineno, ':Text follows logical end of program.'); }
  55.     | module
  56.     ;
  57.  
  58. program : program_heading semicolon block DOT
  59.     ;
  60.  
  61. program_heading : _PROGRAM identifier
  62.     | _PROGRAM identifier LPAREN identifier_list RPAREN
  63.     ;
  64.  
  65. identifier_list : identifier_list comma identifier
  66.     | identifier
  67.     ;
  68.  
  69. block : label_declaration_part
  70.     constant_definition_part
  71.     type_definition_part
  72.     variable_declaration_part
  73.     procedure_and_function_declaration_part
  74.     statement_part
  75.     ;
  76.  
  77. module : constant_definition_part
  78.     type_definition_part
  79.     variable_declaration_part
  80.     procedure_and_function_declaration_part
  81.     ;
  82.  
  83. label_declaration_part : _LABEL label_list semicolon
  84.     |
  85.     ;
  86.  
  87. label_list : label_list comma label
  88.     | label
  89.     ;
  90.  
  91. label : DIGSEQ
  92.     ;
  93.  
  94. constant_definition_part : _CONST constant_list
  95.     |
  96.     ;
  97.  
  98. constant_list : constant_list constant_definition
  99.     | constant_definition
  100.     ;
  101.  
  102. constant_definition : identifier EQUAL cexpression semicolon
  103.     ;
  104.  
  105. /*constant : cexpression ;        /* good stuff! */
  106.  
  107. cexpression : csimple_expression
  108.     | csimple_expression relop csimple_expression
  109.     ;
  110.  
  111. csimple_expression : cterm
  112.     | csimple_expression addop cterm
  113.     ;
  114.  
  115. cterm : cfactor
  116.     | cterm mulop cfactor
  117.     ;
  118.  
  119. cfactor : sign cfactor
  120.     | cexponentiation
  121.     ;
  122.  
  123. cexponentiation : cprimary
  124.     | cprimary STARSTAR cexponentiation
  125.     ;
  126.  
  127. cprimary : identifier
  128.     | LPAREN cexpression RPAREN
  129.     | unsigned_constant
  130.     | _NOT cprimary
  131.     ;
  132.  
  133. constant : non_string
  134.     | sign non_string
  135.     | CHARACTER_STRING
  136.     ;
  137.  
  138. sign : PLUS
  139.     | MINUS
  140.     ;
  141.  
  142. non_string : DIGSEQ
  143.     | identifier
  144.     | REALNUMBER
  145.     ;
  146.  
  147. type_definition_part : _TYPE type_definition_list
  148.     |
  149.     ;
  150.  
  151. type_definition_list : type_definition_list type_definition
  152.     | type_definition
  153.     ;
  154.  
  155. type_definition : identifier EQUAL type_denoter semicolon
  156.     ;
  157.  
  158. type_denoter : identifier
  159.     | new_type
  160.     ;
  161.  
  162. new_type : new_ordinal_type
  163.     | new_structured_type
  164.     | new_pointer_type
  165.     ;
  166.  
  167. new_ordinal_type : enumerated_type
  168.     | subrange_type
  169.     ;
  170.  
  171. enumerated_type : LPAREN identifier_list RPAREN
  172.     ;
  173.  
  174. subrange_type : constant DOTDOT constant
  175.     ;
  176.  
  177. new_structured_type : structured_type
  178.     | _PACKED structured_type
  179.     ;
  180.  
  181. structured_type : array_type
  182.     | record_type
  183.     | set_type
  184.     | file_type
  185.     ;
  186.  
  187. array_type : _ARRAY LBRAC index_list RBRAC _OF component_type
  188.     ;
  189.  
  190. index_list : index_list comma index_type
  191.     | index_type
  192.     ;
  193.  
  194. index_type : ordinal_type ;
  195.  
  196. ordinal_type : new_ordinal_type
  197.     | identifier
  198.     ;
  199.  
  200. component_type : type_denoter ;
  201.  
  202. record_type : _RECORD record_section_list _END
  203.     | _RECORD record_section_list semicolon variant_part _END
  204.     | _RECORD variant_part _END
  205.     ;
  206.  
  207. record_section_list : record_section_list semicolon record_section
  208.     | record_section
  209.     ;
  210.  
  211. record_section : identifier_list COLON type_denoter
  212.     ;
  213.  
  214. variant_part : _CASE variant_selector _OF variant_list semicolon
  215.     | _CASE variant_selector _OF variant_list
  216.     |
  217.     ;
  218.  
  219. variant_selector : tag_field COLON tag_type
  220.     | tag_type
  221.     ;
  222.  
  223. variant_list : variant_list semicolon variant
  224.     | variant
  225.     ;
  226.  
  227. variant : case_constant_list COLON LPAREN record_section_list RPAREN
  228.     | case_constant_list COLON LPAREN record_section_list semicolon
  229.         variant_part RPAREN
  230.     | case_constant_list COLON LPAREN variant_part RPAREN
  231.     ;
  232.  
  233. case_constant_list : case_constant_list comma case_constant
  234.     | case_constant
  235.     ;
  236.  
  237. case_constant : constant
  238.     | constant DOTDOT constant
  239.     ;
  240.  
  241. tag_field : identifier ;
  242.  
  243. tag_type : identifier ;
  244.  
  245. set_type : _SET _OF base_type
  246.     ;
  247.  
  248. base_type : ordinal_type ;
  249.  
  250. file_type : _FILE _OF component_type
  251.     ;
  252.  
  253. new_pointer_type : UPARROW domain_type
  254.     ;
  255.  
  256. domain_type : identifier ;
  257.  
  258. variable_declaration_part : _VAR variable_declaration_list semicolon
  259.     |
  260.     ;
  261.  
  262. variable_declaration_list :
  263.       variable_declaration_list semicolon variable_declaration
  264.     | variable_declaration
  265.     ;
  266.  
  267. variable_declaration : identifier_list COLON type_denoter
  268.     ;
  269.  
  270. procedure_and_function_declaration_part :
  271.         proc_or_func_declaration_list semicolon
  272.     |
  273.     ;
  274.  
  275. proc_or_func_declaration_list :
  276.       proc_or_func_declaration_list semicolon proc_or_func_declaration
  277.     | proc_or_func_declaration
  278.     ;
  279.  
  280. proc_or_func_declaration : procedure_declaration
  281.     | function_declaration
  282.     ;
  283.  
  284. procedure_declaration : procedure_heading semicolon directive
  285.     | procedure_heading semicolon procedure_block
  286.     ;
  287.  
  288. procedure_heading : procedure_identification
  289.     | procedure_identification formal_parameter_list
  290.     ;
  291.  
  292. directive : _FORWARD
  293.     | _EXTERNAL
  294.     ;
  295.  
  296. formal_parameter_list : LPAREN formal_parameter_section_list RPAREN ;
  297.  
  298. formal_parameter_section_list : formal_parameter_section_list semicolon
  299.  formal_parameter_section
  300.     | formal_parameter_section
  301.     ;
  302.  
  303. formal_parameter_section : value_parameter_specification
  304.     | variable_parameter_specification
  305.     | procedural_parameter_specification
  306.     | functional_parameter_specification
  307.     ;
  308.  
  309. value_parameter_specification : identifier_list COLON identifier
  310.     ;
  311.  
  312. variable_parameter_specification : _VAR identifier_list COLON identifier
  313.     ;
  314.  
  315. procedural_parameter_specification : procedure_heading ;
  316.  
  317. functional_parameter_specification : function_heading ;
  318.  
  319. procedure_identification : _PROCEDURE identifier ;
  320.  
  321. procedure_block : block ;
  322.  
  323. function_declaration : function_heading semicolon directive
  324.     | function_identification semicolon function_block
  325.     | function_heading semicolon function_block
  326.     ;
  327.  
  328. function_heading : _FUNCTION identifier COLON result_type
  329.     | _FUNCTION identifier formal_parameter_list COLON result_type
  330.     ;
  331.  
  332. result_type : identifier ;
  333.  
  334. function_identification : _FUNCTION identifier ;
  335.  
  336. function_block : block ;
  337.  
  338. statement_part : compound_statement ;
  339.  
  340. compound_statement : _BEGIN statement_sequence _END ;
  341.  
  342. statement_sequence : statement_sequence semicolon statement
  343.     | statement
  344.     | error
  345.  { writeln('statement ignored'); yyclearin }
  346.     ;
  347.  
  348. statement : open_statement
  349.     | closed_statement
  350.     ;
  351.  
  352. open_statement : label COLON non_labeled_open_statement
  353.     | non_labeled_open_statement
  354.     ;
  355.  
  356. closed_statement : label COLON non_labeled_closed_statement
  357.     | non_labeled_closed_statement
  358.     ;
  359.  
  360. non_labeled_closed_statement : assignment_statement
  361.     | procedure_statement
  362.     | goto_statement
  363.     | compound_statement
  364.     | case_statement
  365.     | repeat_statement
  366.     | closed_with_statement
  367.     | closed_if_statement
  368.     | closed_while_statement
  369.     | closed_for_statement
  370.     |
  371.     ;
  372.  
  373. non_labeled_open_statement : open_with_statement
  374.     | open_if_statement
  375.     | open_while_statement
  376.     | open_for_statement
  377.     ;
  378.  
  379. repeat_statement : _REPEAT statement_sequence _UNTIL boolean_expression
  380.     ;
  381.  
  382. open_while_statement : _WHILE boolean_expression _DO open_statement
  383.     ;
  384.  
  385. closed_while_statement : _WHILE boolean_expression _DO closed_statement
  386.     ;
  387.  
  388. open_for_statement : _FOR control_variable ASSIGNMENT initial_value direction
  389.             final_value _DO open_statement
  390.     ;
  391.  
  392. closed_for_statement : _FOR control_variable ASSIGNMENT initial_value direction
  393.             final_value _DO closed_statement
  394.     ;
  395.  
  396. open_with_statement : _WITH record_variable_list _DO open_statement
  397.     ;
  398.  
  399. closed_with_statement : _WITH record_variable_list _DO closed_statement
  400.     ;
  401.  
  402. open_if_statement : _IF boolean_expression _THEN statement
  403.     | _IF boolean_expression _THEN closed_statement _ELSE open_statement
  404.     ;
  405.  
  406. closed_if_statement : _IF boolean_expression _THEN closed_statement
  407.             _ELSE closed_statement
  408.     ;
  409.  
  410. assignment_statement : variable_access ASSIGNMENT expression
  411.     ;
  412.  
  413. variable_access : identifier
  414.     | indexed_variable
  415.     | field_designator
  416.     | variable_access UPARROW
  417.     ;
  418.  
  419. indexed_variable : variable_access LBRAC index_expression_list RBRAC
  420.     ;
  421.  
  422. index_expression_list : index_expression_list comma index_expression
  423.     | index_expression
  424.     ;
  425.  
  426. index_expression : expression ;
  427.  
  428. field_designator : variable_access DOT identifier
  429.     ;
  430.  
  431. procedure_statement : identifier params
  432.     | identifier
  433.     ;
  434.  
  435. params : LPAREN actual_parameter_list RPAREN ;
  436.  
  437. actual_parameter_list : actual_parameter_list comma actual_parameter
  438.     | actual_parameter
  439.     ;
  440.  
  441. /*
  442.  * this forces you to check all this to be sure that only write and
  443.  * writeln use the 2nd and 3rd forms, you really can't do it easily in
  444.  * the grammar, especially since write and writeln aren't reserved
  445.  */
  446. actual_parameter : expression
  447.     | expression COLON expression
  448.     | expression COLON expression COLON expression
  449.     ;
  450.  
  451. goto_statement : _GOTO label
  452.     ;
  453.  
  454. case_statement : _CASE case_index _OF case_list_element_list _END
  455.     | _CASE case_index _OF case_list_element_list SEMICOLON _END
  456.     | _CASE case_index _OF case_list_element_list semicolon
  457.             otherwisepart statement _END
  458.     | _CASE case_index _OF case_list_element_list semicolon
  459.             otherwisepart statement SEMICOLON _END
  460.     ;
  461.  
  462. case_index : expression ;
  463.  
  464. case_list_element_list : case_list_element_list semicolon case_list_element
  465.     | case_list_element
  466.     ;
  467.  
  468. case_list_element : case_constant_list COLON statement
  469.     ;
  470.  
  471. otherwisepart :    _OTHERWISE
  472.     | _OTHERWISE COLON
  473.     ;
  474.  
  475. control_variable : identifier ;
  476.  
  477. initial_value : expression ;
  478.  
  479. direction : _TO
  480.     | _DOWNTO
  481.     ;
  482.  
  483. final_value : expression ;
  484.  
  485. record_variable_list : record_variable_list comma variable_access
  486.     | variable_access
  487.     ;
  488.  
  489. boolean_expression : expression ;
  490.  
  491. expression : simple_expression
  492.     | simple_expression relop simple_expression
  493.     | error
  494.     ;
  495.  
  496. simple_expression : term
  497.     | simple_expression addop term
  498.     ;
  499.  
  500. term : factor
  501.     | term mulop factor
  502.     ;
  503.  
  504. factor : sign factor
  505.     | exponentiation
  506.     ;
  507.  
  508. exponentiation : primary
  509.     | primary STARSTAR exponentiation
  510.     ;
  511.  
  512. primary : variable_access
  513.     | unsigned_constant
  514.     | function_designator
  515.     | set_constructor
  516.     | LPAREN expression RPAREN
  517.     | _NOT primary
  518.     ;
  519.  
  520. unsigned_constant : unsigned_number
  521.     | CHARACTER_STRING
  522.     | _NIL
  523.     ;
  524.  
  525. unsigned_number : unsigned_integer | unsigned_real ;
  526.  
  527. unsigned_integer : DIGSEQ
  528.     ;
  529.  
  530. unsigned_real : REALNUMBER
  531.     ;
  532.  
  533. /* functions with no params will be handled by plain identifier */
  534. function_designator : identifier params
  535.     ;
  536.  
  537. set_constructor : LBRAC member_designator_list RBRAC
  538.     | LBRAC RBRAC
  539.     ;
  540.  
  541. member_designator_list : member_designator_list comma member_designator
  542.     | member_designator
  543.     ;
  544.  
  545. member_designator : member_designator DOTDOT expression
  546.     | expression
  547.     ;
  548.  
  549. addop: PLUS
  550.     | MINUS
  551.     | _OR
  552.     ;
  553.  
  554. mulop : STAR
  555.     | SLASH
  556.     | _DIV
  557.     | _MOD
  558.     | _AND
  559.     ;
  560.  
  561. relop : EQUAL
  562.     | NOTEQUAL
  563.     | LT
  564.     | GT
  565.     | LE
  566.     | GE
  567.     | _IN
  568.     ;
  569.  
  570. identifier : IDENTIFIER
  571.     ;
  572.  
  573. semicolon : SEMICOLON
  574.     ;
  575.  
  576. comma : COMMA
  577.     ;
  578.  
  579. %%
  580.  
  581. {$I PASLEX}
  582.  
  583. begin
  584.   filename := paramStr(1);
  585.   if filename='' then
  586.     begin
  587.       write('input file: ');
  588.       readln(filename);
  589.     end;
  590.   assign(yyinput, filename);
  591.   reset(yyinput);
  592.   if yyparse=0 then writeln('successful parse!');
  593. end.
  594.