PreviousUpNext

15.4.142  src/app/yacc/src/grammar.pkg

#  Mythryl-Yacc Parser Generator (c) 1989 Andrew W. Appel, David R. Tarditi 

# Compiled by:
#     src/app/yacc/src/mythryl-yacc.lib

###           "First secure an independent income,
###            then practice virtue."
###
###                         -- Greek saying



package   grammar
: (weak)  Grammar                                               # Grammar       is from   src/app/yacc/src/grammar.api
{
    #  define types term and nonterm using those in lr_table
    #  enum term = TERM of Int 
    #  enum nonterm = NONTERM of Int

    include lr_table;

     Symbol =    TERMINAL  Terminal
                | NONTERMINAL  Nonterminal;

     Grammar = GRAMMAR 
                    { rules:  List {
                             lhs: Nonterminal,
                             rhs: List( Symbol ), 
                             precedence: Null_Or( Int ),
                             rulenum: Int },
                    noshift:  List( Terminal ),
                    eop:  List( Terminal ),
                    terms: Int,
                    nonterms: Int,
                    start:  Nonterminal,
                    precedence:  Terminal -> Null_Or( Int ),
                    term_to_string:  Terminal -> String,
                    nonterm_to_string:  Nonterminal -> String };
};

package   internal_grammar
: (weak)  Internal_Grammar                                      # Internal_Grammar      is from   src/app/yacc/src/internal-grammar.api
{
    package grammar = grammar;
    include grammar;

     Rule = RULE 
                    { lhs: Nonterminal,
                     rhs: List( Symbol ),
                     num: Int,#  internal # Assigned by coreutils 
                     rulenum: Int,
                     precedence: Null_Or( Int ) };

    my eq_term:  (Terminal, Terminal) -> Bool = (==);
    my gt_term:  (Terminal, Terminal) -> Bool = fn (TERM i, TERM j) => i>j; end ;

    my eq_nonterm:  (Nonterminal, Nonterminal) -> Bool
                = (==);

    my gt_nonterm:  (Nonterminal, Nonterminal) -> Bool
                =  fn (NONTERM i, NONTERM j) = i>j;

    my eq_symbol:  (Symbol, Symbol) -> Bool
               =  (==);

    fun gt_symbol (   TERMINAL (TERM i),     TERMINAL  (TERM j)) =>   i > j;
        gt_symbol (NONTERMINAL (NONTERM i), NONTERMINAL (NONTERM j)) =>   i > j;
        gt_symbol (   TERMINAL _,      NONTERMINAL _     ) =>   FALSE;
        gt_symbol (NONTERMINAL _,         TERMINAL _     ) =>   TRUE;
    end;


    package symbol_assoc
        =
        table_g (
            package {
                 Key = Symbol;
                gt = gt_symbol;
            }
        );

    package nonterm_assoc
        =
        table_g (
            package {
                 Key =  Nonterminal;
                gt = gt_nonterm;
            }
        );

    debug = FALSE;

    fun pr_rule (a as symbol_to_string, nonterm_to_string, print)
        =
        {   print_symbol = print o symbol_to_string;

            fun print_rhs (h ! t)
                    =>
                    {   print_symbol h;
                        print " ";
                        print_rhs t;
                    };

                print_rhs NIL
                    =>
                    ();
            end;

            fn (RULE { lhs, rhs, num, rulenum, precedence, ... } )
                =>
                {   (print o nonterm_to_string) lhs;
                    print " : ";
                    print_rhs rhs;

                    if debug

                         print " num = ";
                         print (int::to_string num);
                         print " rulenum = ";
                         print (int::to_string rulenum);
                         print " precedence = ";

                         case precedence
                           
                              NULL  => print " none";
                              THE i => print (int::to_string i);
                         esac;

                         ();
                    fi;
                }; end ;
        };

    fun pr_grammar (a as (symbol_to_string, nonterm_to_string, print))
                  (GRAMMAR { rules, terms, nonterms, start, ... } )
         =
         {   stipulate
                 pr_rule = pr_rule a;
             herein
                 fun print_rule { lhs, rhs, precedence, rulenum }
                     =
                     {   pr_rule (RULE { lhs, rhs, num=>0,
                              rulenum, precedence } );

                         print "\n";
                     };
             end;

             print "grammar = \n";
             list::apply print_rule rules;
             print "\n";

             print (" terms = " + (int::to_string terms) +
                      " nonterms = " + (int::to_string nonterms) +
                      " start = ");

             (print o nonterm_to_string) start;
             ();
         };
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext