home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / lang / grammars.lzh / GRAMMARS / PASCAL / GRAM.Y next >
Encoding:
Lex Description  |  1988-10-01  |  12.5 KB  |  560 lines

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