home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / iso_pascal < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  14.3 KB

  1. From: seismo!gatech!emory!arnold (Arnold D. Robbins {EUCC})
  2. Subject: Yacc and Lex for ISO Level 0 Pascal
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 4, Issue 61
  7. Submitted by: seismo!gatech!emory!arnold (Arnold D. Robbins {EUCC})
  8.  
  9.  
  10. echo extracting README
  11. cat << EOF > README
  12. The two files in this archive represent a scanner and parser for ISO Level 0
  13. Pascal, plus some simple extensions. They are in the public domain. Anyone
  14. can do whatever he wants to with them.
  15.  
  16. They successfully pass the 4.2 BSD Yacc and Lex. Other than that, no promises
  17. are made, and no warranties tendered. You use this code at your own risk,
  18. and all that other stuff.
  19.  
  20. Enjoy,
  21.  
  22. Arnold Robbins
  23. EOF
  24. echo extracting gram.y
  25. cat << EOF > gram.y
  26. %{
  27. /*
  28.  * grammar.y
  29.  *
  30.  * Pascal grammar in Yacc format, based originally on BNF given
  31.  * in "Standard Pascal -- User Reference Manual", by Doug Cooper.
  32.  * This in turn is the BNF given by the ANSI and ISO Pascal standards,
  33.  * and so, is PUBLIC DOMAIN. The grammar is for ISO Level 0 Pascal.
  34.  * The grammar has been massaged somewhat to make it LALR, and added
  35.  * the following extensions.
  36.  *
  37.  * constant expressions
  38.  * otherwise statement in a case
  39.  * productions to correctly match else's with if's
  40.  * beginnings of a separate compilation facility
  41.  */
  42.  
  43. %}
  44.  
  45. %token AND ARRAY ASSIGNMENT CASE CHARACTER_STRING COLON COMMA CONST DIGSEQ
  46. %token DIV DO DOT DOTDOT DOWNTO ELSE END EQUAL EXTERNAL FOR FORWARD FUNCTION
  47. %token GE GOTO GT IDENTIFIER IF IN LABEL LBRAC LE LPAREN LT MINUS MOD NIL NOT
  48. %token NOTEQUAL OF OR OTHERWISE PACKED PBEGIN PFILE PLUS PROCEDURE PROGRAM RBRAC
  49. %token REALNUMBER RECORD REPEAT RPAREN SEMICOLON SET SLASH STAR STARSTAR THEN
  50. %token TO TYPE UNTIL UPARROW VAR WHILE WITH
  51.  
  52. %%
  53. file : program
  54.     | module
  55.     ;
  56.  
  57. program : program_heading semicolon block DOT
  58.     ;
  59.  
  60. program_heading : PROGRAM identifier
  61.     | PROGRAM identifier LPAREN identifier_list RPAREN
  62.     ;
  63.  
  64. identifier_list : identifier_list comma identifier
  65.     | identifier
  66.     ;
  67.  
  68. block : label_declaration_part
  69.     constant_definition_part
  70.     type_definition_part
  71.     variable_declaration_part
  72.     procedure_and_function_declaration_part
  73.     statement_part
  74.     ;
  75.  
  76. module : constant_definition_part
  77.     type_definition_part
  78.     variable_declaration_part
  79.     procedure_and_function_declaration_part
  80.     ;
  81.  
  82. label_declaration_part : LABEL label_list semicolon
  83.     |
  84.     ;
  85.  
  86. label_list : label_list comma label
  87.     | label
  88.     ;
  89.  
  90. label : DIGSEQ
  91.     ;
  92.  
  93. constant_definition_part : CONST constant_list
  94.     |
  95.     ;
  96.  
  97. constant_list : constant_list constant_definition
  98.     | constant_definition
  99.     ;
  100.  
  101. constant_definition : identifier EQUAL cexpression semicolon
  102.     ;
  103.  
  104. /*constant : cexpression ;        /* good stuff! */
  105.  
  106. cexpression : csimple_expression
  107.     | csimple_expression relop csimple_expression
  108.     ;
  109.  
  110. csimple_expression : cterm
  111.     | csimple_expression addop cterm
  112.     ;
  113.  
  114. cterm : cfactor
  115.     | cterm mulop cfactor
  116.     ;
  117.  
  118. cfactor : sign cfactor
  119.     | cexponentiation
  120.     ;
  121.  
  122. cexponentiation : cprimary
  123.     | cprimary STARSTAR cexponentiation
  124.     ;
  125.  
  126. cprimary : identifier
  127.     | LPAREN cexpression RPAREN
  128.     | unsigned_constant
  129.     | NOT cprimary
  130.     ;
  131.  
  132. constant : non_string
  133.     | sign non_string
  134.     | CHARACTER_STRING
  135.     ;
  136.  
  137. sign : PLUS
  138.     | MINUS
  139.     ;
  140.  
  141. non_string : DIGSEQ
  142.     | identifier
  143.     | REALNUMBER
  144.     ;
  145.  
  146. type_definition_part : TYPE type_definition_list
  147.     |
  148.     ;
  149.  
  150. type_definition_list : type_definition_list type_definition
  151.     | type_definition
  152.     ;
  153.  
  154. type_definition : identifier EQUAL type_denoter semicolon
  155.     ;
  156.  
  157. type_denoter : identifier
  158.     | new_type
  159.     ;
  160.  
  161. new_type : new_ordinal_type
  162.     | new_structured_type
  163.     | new_pointer_type
  164.     ;
  165.  
  166. new_ordinal_type : enumerated_type
  167.     | subrange_type
  168.     ;
  169.  
  170. enumerated_type : LPAREN identifier_list RPAREN
  171.     ;
  172.  
  173. subrange_type : constant DOTDOT constant
  174.     ;
  175.  
  176. new_structured_type : structured_type
  177.     | PACKED structured_type
  178.     ;
  179.  
  180. structured_type : array_type
  181.     | record_type
  182.     | set_type
  183.     | file_type
  184.     ;
  185.  
  186. array_type : ARRAY LBRAC index_list RBRAC OF component_type
  187.     ;
  188.  
  189. index_list : index_list comma index_type
  190.     | index_type
  191.     ;
  192.  
  193. index_type : ordinal_type ;
  194.  
  195. ordinal_type : new_ordinal_type
  196.     | identifier
  197.     ;
  198.  
  199. component_type : type_denoter ;
  200.  
  201. record_type : RECORD record_section_list END
  202.     | RECORD record_section_list semicolon variant_part END
  203.     | RECORD variant_part END
  204.     ;
  205.  
  206. record_section_list : record_section_list semicolon record_section
  207.     | record_section
  208.     ;
  209.  
  210. record_section : identifier_list COLON type_denoter
  211.     ;
  212.  
  213. variant_part : CASE variant_selector OF variant_list semicolon
  214.     | CASE variant_selector OF variant_list
  215.     |
  216.     ;
  217.  
  218. variant_selector : tag_field COLON tag_type
  219.     | tag_type
  220.     ;
  221.  
  222. variant_list : variant_list semicolon variant
  223.     | variant
  224.     ;
  225.  
  226. variant : case_constant_list COLON LPAREN record_section_list RPAREN
  227.     | case_constant_list COLON LPAREN record_section_list semicolon
  228.         variant_part RPAREN
  229.     | case_constant_list COLON LPAREN variant_part RPAREN
  230.     ;
  231.  
  232. case_constant_list : case_constant_list comma case_constant
  233.     | case_constant
  234.     ;
  235.  
  236. case_constant : constant
  237.     | constant DOTDOT constant
  238.     ;
  239.  
  240. tag_field : identifier ;
  241.  
  242. tag_type : identifier ;
  243.  
  244. set_type : SET OF base_type
  245.     ;
  246.  
  247. base_type : ordinal_type ;
  248.  
  249. file_type : PFILE OF component_type
  250.     ;
  251.  
  252. new_pointer_type : UPARROW domain_type
  253.     ;
  254.  
  255. domain_type : identifier ;
  256.  
  257. variable_declaration_part : VAR variable_declaration_list semicolon
  258.     |
  259.     ;
  260.  
  261. variable_declaration_list :
  262.       variable_declaration_list semicolon variable_declaration
  263.     | variable_declaration
  264.     ;
  265.  
  266. variable_declaration : identifier_list COLON type_denoter
  267.     ;
  268.  
  269. procedure_and_function_declaration_part :
  270.         proc_or_func_declaration_list semicolon
  271.     |
  272.     ;
  273.  
  274. proc_or_func_declaration_list :
  275.       proc_or_func_declaration_list semicolon proc_or_func_declaration
  276.     | proc_or_func_declaration
  277.     ;
  278.  
  279. proc_or_func_declaration : procedure_declaration
  280.     | function_declaration
  281.     ;
  282.  
  283. procedure_declaration : procedure_heading semicolon directive
  284.     | procedure_heading semicolon procedure_block
  285.     ;
  286.  
  287. procedure_heading : procedure_identification
  288.     | procedure_identification formal_parameter_list
  289.     ;
  290.  
  291. directive : FORWARD
  292.     | EXTERNAL
  293.     ;
  294.  
  295. formal_parameter_list : LPAREN formal_parameter_section_list RPAREN ;
  296.  
  297. formal_parameter_section_list : formal_parameter_section_list semicolon formal_parameter_section
  298.     | formal_parameter_section
  299.     ;
  300.  
  301. formal_parameter_section : value_parameter_specification
  302.     | variable_parameter_specification
  303.     | procedural_parameter_specification
  304.     | functional_parameter_specification
  305.     ;
  306.  
  307. value_parameter_specification : identifier_list COLON identifier
  308.     ;
  309.  
  310. variable_parameter_specification : VAR identifier_list COLON identifier
  311.     ;
  312.  
  313. procedural_parameter_specification : procedure_heading ;
  314.  
  315. functional_parameter_specification : function_heading ;
  316.  
  317. procedure_identification : PROCEDURE identifier ;
  318.  
  319. procedure_block : block ;
  320.  
  321. function_declaration : function_heading semicolon directive
  322.     | function_identification semicolon function_block
  323.     | function_heading semicolon function_block
  324.     ;
  325.  
  326. function_heading : FUNCTION identifier COLON result_type
  327.     | FUNCTION identifier formal_parameter_list COLON result_type
  328.     ;
  329.  
  330. result_type : identifier ;
  331.  
  332. function_identification : FUNCTION identifier ;
  333.  
  334. function_block : block ;
  335.  
  336. statement_part : compound_statement ;
  337.  
  338. compound_statement : PBEGIN statement_sequence END ;
  339.  
  340. statement_sequence : statement_sequence semicolon statement
  341.     | statement
  342.     ;
  343.  
  344. statement : open_statement
  345.     | closed_statement
  346.     ;
  347.  
  348. open_statement : label COLON non_labeled_open_statement
  349.     | non_labeled_open_statement
  350.     ;
  351.  
  352. closed_statement : label COLON non_labeled_closed_statement
  353.     | non_labeled_closed_statement
  354.     ;
  355.  
  356. non_labeled_closed_statement : assignment_statement
  357.     | procedure_statement
  358.     | goto_statement
  359.     | compound_statement
  360.     | case_statement
  361.     | repeat_statement
  362.     | closed_with_statement
  363.     | closed_if_statement
  364.     | closed_while_statement
  365.     | closed_for_statement
  366.     |
  367.     ;
  368.  
  369. non_labeled_open_statement : open_with_statement
  370.     | open_if_statement
  371.     | open_while_statement
  372.     | open_for_statement
  373.     ;
  374.  
  375. repeat_statement : REPEAT statement_sequence UNTIL boolean_expression
  376.     ;
  377.  
  378. open_while_statement : WHILE boolean_expression DO open_statement
  379.     ;
  380.  
  381. closed_while_statement : WHILE boolean_expression DO closed_statement
  382.     ;
  383.  
  384. open_for_statement : FOR control_variable ASSIGNMENT initial_value direction
  385.             final_value DO open_statement
  386.     ;
  387.  
  388. closed_for_statement : FOR control_variable ASSIGNMENT initial_value direction
  389.             final_value DO closed_statement
  390.     ;
  391.  
  392. open_with_statement : WITH record_variable_list DO open_statement
  393.     ;
  394.  
  395. closed_with_statement : WITH record_variable_list DO closed_statement
  396.     ;
  397.  
  398. open_if_statement : IF boolean_expression THEN statement
  399.     | IF boolean_expression THEN closed_statement ELSE open_statement
  400.     ;
  401.  
  402. closed_if_statement : IF boolean_expression THEN closed_statement
  403.             ELSE closed_statement
  404.     ;
  405.  
  406. assignment_statement : variable_access ASSIGNMENT expression
  407.     ;
  408.  
  409. variable_access : identifier
  410.     | indexed_variable
  411.     | field_designator
  412.     | variable_access UPARROW
  413.     ;
  414.  
  415. indexed_variable : variable_access LBRAC index_expression_list RBRAC
  416.     ;
  417.  
  418. index_expression_list : index_expression_list comma index_expression
  419.     | index_expression
  420.     ;
  421.  
  422. index_expression : expression ;
  423.  
  424. field_designator : variable_access DOT identifier
  425.     ;
  426.  
  427. procedure_statement : identifier params
  428.     | identifier
  429.     ;
  430.  
  431. params : LPAREN actual_parameter_list RPAREN ;
  432.  
  433. actual_parameter_list : actual_parameter_list comma actual_parameter
  434.     | actual_parameter
  435.     ;
  436.  
  437. /*
  438.  * this forces you to check all this to be sure that only write and
  439.  * writeln use the 2nd and 3rd forms, you really can't do it easily in
  440.  * the grammar, especially since write and writeln aren't reserved
  441.  */
  442. actual_parameter : expression
  443.     | expression COLON expression
  444.     | expression COLON expression COLON expression
  445.     ;
  446.  
  447. goto_statement : GOTO label
  448.     ;
  449.  
  450. case_statement : CASE case_index OF case_list_element_list END
  451.     | CASE case_index OF case_list_element_list SEMICOLON END
  452.     | CASE case_index OF case_list_element_list semicolon
  453.             otherwisepart statement END
  454.     | CASE case_index OF case_list_element_list semicolon
  455.             otherwisepart statement SEMICOLON END
  456.     ;
  457.  
  458. case_index : expression ;
  459.  
  460. case_list_element_list : case_list_element_list semicolon case_list_element
  461.     | case_list_element
  462.     ;
  463.  
  464. case_list_element : case_constant_list COLON statement
  465.     ;
  466.  
  467. otherwisepart :    OTHERWISE
  468.     | OTHERWISE COLON
  469.     ;
  470.  
  471. control_variable : identifier ;
  472.  
  473. initial_value : expression ;
  474.  
  475. direction : TO
  476.     | DOWNTO
  477.     ;
  478.  
  479. final_value : expression ;
  480.  
  481. record_variable_list : record_variable_list comma variable_access
  482.     | variable_access
  483.     ;
  484.  
  485. boolean_expression : expression ;
  486.  
  487. expression : simple_expression
  488.     | simple_expression relop simple_expression
  489.     ;
  490.  
  491. simple_expression : term
  492.     | simple_expression addop term
  493.     ;
  494.  
  495. term : factor
  496.     | term mulop factor
  497.     ;
  498.  
  499. factor : sign factor
  500.     | exponentiation
  501.     ;
  502.     
  503. exponentiation : primary
  504.     | primary STARSTAR exponentiation
  505.     ;
  506.  
  507. primary : variable_access
  508.     | unsigned_constant
  509.     | function_designator
  510.     | set_constructor
  511.     | LPAREN expression RPAREN
  512.     | NOT primary
  513.     ;
  514.  
  515. unsigned_constant : unsigned_number
  516.     | CHARACTER_STRING
  517.     | NIL
  518.     ;
  519.  
  520. unsigned_number : unsigned_integer | unsigned_real ;
  521.  
  522. unsigned_integer : DIGSEQ
  523.     ;
  524.  
  525. unsigned_real : REALNUMBER
  526.     ;
  527.  
  528. /* functions with no params will be handled by plain identifier */
  529. function_designator : identifier params
  530.     ;
  531.  
  532. set_constructor : LBRAC member_designator_list RBRAC
  533.     | LBRAC RBRAC
  534.     ;
  535.  
  536. member_designator_list : member_designator_list comma member_designator
  537.     | member_designator
  538.     ;
  539.  
  540. member_designator : member_designator DOTDOT expression
  541.     | expression
  542.     ;
  543.  
  544. addop: PLUS
  545.     | MINUS
  546.     | OR
  547.     ;
  548.  
  549. mulop : STAR
  550.     | SLASH
  551.     | DIV
  552.     | MOD
  553.     | AND
  554.     ;
  555.  
  556. relop : EQUAL
  557.     | NOTEQUAL
  558.     | LT
  559.     | GT
  560.     | LE
  561.     | GE
  562.     | IN
  563.     ;
  564.  
  565. identifier : IDENTIFIER
  566.     ;
  567.  
  568. semicolon : SEMICOLON
  569.     ;
  570.  
  571. comma : COMMA
  572.     ;
  573.  
  574. %%
  575.  
  576. #include <stdio.h>
  577.  
  578. extern int line_no;
  579. extern char yytext[];
  580.  
  581. yyerror(s)
  582. char *s;
  583. {
  584.     fprintf(stderr, "%s: at or before '%s', line %d\n", s, yytext, line_no);
  585. }
  586. EOF
  587. echo extracting scan.l
  588. cat << EOF > scan.l
  589. %{
  590. /*
  591.  * scan.l
  592.  *
  593.  * lex input file for pascal scanner
  594.  *
  595.  * extensions: to ways to spell "external" and "->" ok for "^".
  596.  */
  597.  
  598. #include <stdio.h>
  599. #include "y.tab.h"
  600. int line_no = 1;
  601.  
  602. %}
  603.  
  604. A    [aA]
  605. B    [bB]
  606. C    [cC]
  607. D    [dD]
  608. E    [eE]
  609. F    [fF]
  610. G    [gG]
  611. H    [hH]
  612. I    [iI]
  613. J    [jJ]
  614. K    [kK]
  615. L    [lL]
  616. M    [mM]
  617. N    [nN]
  618. O    [oO]
  619. P    [pP]
  620. Q    [qQ]
  621. R    [rR]
  622. S    [sS]
  623. T    [tT]
  624. U    [uU]
  625. V    [vV]
  626. W    [wW]
  627. X    [xX]
  628. Y    [yY]
  629. Z    [zZ]
  630. NQUOTE    [^']
  631.  
  632. %%
  633.  
  634. {A}{N}{D}            return(AND);
  635. {A}{R}{R}{A}{Y}            return(ARRAY);
  636. {C}{A}{S}{E}            return(CASE);
  637. {C}{O}{N}{S}{T}            return(CONST);
  638. {D}{I}{V}            return(DIV);
  639. {D}{O}                return(DO);
  640. {D}{O}{W}{N}{T}{O}        return(DOWNTO);
  641. {E}{L}{S}{E}            return(ELSE);
  642. {E}{N}{D}            return(END);
  643. {E}{X}{T}{E}{R}{N}    |
  644. {E}{X}{T}{E}{R}{N}{A}{L}    return(EXTERNAL);
  645. {F}{O}{R}            return(FOR);
  646. {F}{O}{R}{W}{A}{R}{D}        return(FORWARD);
  647. {F}{U}{N}{C}{T}{I}{O}{N}    return(FUNCTION);
  648. {G}{O}{T}{O}            return(GOTO);
  649. {I}{F}                return(IF);
  650. {I}{N}                return(IN);
  651. {L}{A}{B}{E}{L}            return(LABEL);
  652. {M}{O}{D}            return(MOD);
  653. {N}{I}{L}            return(NIL);
  654. {N}{O}{T}            return(NOT);
  655. {O}{F}                return(OF);
  656. {O}{R}                return(OR);
  657. {O}{T}{H}{E}{R}{W}{I}{S}{E}    return(OTHERWISE);
  658. {P}{A}{C}{K}{E}{D}        return(PACKED);
  659. {B}{E}{G}{I}{N}            return(PBEGIN);
  660. {F}{I}{L}{E}            return(PFILE);
  661. {P}{R}{O}{C}{E}{D}{U}{R}{E}    return(PROCEDURE);
  662. {P}{R}{O}{G}{R}{A}{M}        return(PROGRAM);
  663. {R}{E}{C}{O}{R}{D}        return(RECORD);
  664. {R}{E}{P}{E}{A}{T}        return(REPEAT);
  665. {S}{E}{T}            return(SET);
  666. {T}{H}{E}{N}            return(THEN);
  667. {T}{O}                return(TO);
  668. {T}{Y}{P}{E}            return(TYPE);
  669. {U}{N}{T}{I}{L}            return(UNTIL);
  670. {V}{A}{R}            return(VAR);
  671. {W}{H}{I}{L}{E}            return(WHILE);
  672. {W}{I}{T}{H}            return(WITH);
  673. [a-zA-Z]([a-zA-Z0-9])+        return(IDENTIFIER);
  674.  
  675. ":="                return(ASSIGNMENT);
  676. '({NQUOTE}|'')+'        return(CHARACTER_STRING);
  677. ":"                return(COLON);
  678. ","                return(COMMA);
  679. [0-9]+                return(DIGSEQ);
  680. "."                return(DOT);
  681. ".."                return(DOTDOT);
  682. "="                return(EQUAL);
  683. ">="                return(GE);
  684. ">"                return(GT);
  685. "["                return(LBRAC);
  686. "<="                return(LE);
  687. "("                return(LPAREN);
  688. "<"                return(LT);
  689. "-"                return(MINUS);
  690. "<>"                return(NOTEQUAL);
  691. "+"                return(PLUS);
  692. "]"                return(RBRAC);
  693. [0-9]+"."[0-9]+            return(REALNUMBER);
  694. ")"                return(RPAREN);
  695. ";"                return(SEMICOLON);
  696. "/"                return(SLASH);
  697. "*"                return(STAR);
  698. "**"                return(STARSTAR);
  699. "->"            |
  700. "^"                return(UPARROW);
  701.  
  702. "(*"            |
  703. "{"                { register int c;
  704.                     while ((c = input()))
  705.                     {
  706.                         if (c == '}')
  707.                             break;
  708.                         else if (c == '*')
  709.                         {
  710.                             if ((c = input()) == ')')
  711.                                 break;
  712.                             else
  713.                                 unput (c);
  714.                         }
  715.                         else if (c == '\n')
  716.                             line_no++;
  717.                         else if (c == 0)
  718.                             commenteof();
  719.                     }
  720.                 }
  721.  
  722. [ \t\f]                ;
  723.  
  724. \n                line_no++;
  725.  
  726. .                { fprintf (stderr,
  727.                 "'%c' (0%o): illegal charcter at line %d\n",
  728.                     yytext[0], yytext[0], line_no);
  729.                 }
  730.  
  731. %%
  732.  
  733. commenteof()
  734. {
  735.     fprintf (stderr, "unexpected EOF inside comment at line %d\n",
  736.         line_no);
  737.     exit (1);
  738. }
  739.  
  740. yywrap ()
  741. {
  742.     return (1);
  743. }
  744. EOF
  745.  
  746.