PreviousUpNext

15.4.135  src/app/yacc/lib/lrtable.pkg

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

# Compiled by:
#     src/lib/std/standard.lib

###                   "An education obtained with money
###                    is worse than no education at all."
###
###                                    -- Socrates (circa 470-399BC)



package   lr_table
: (weak)  Lr_Table                                              # Lr_Table      is from   src/app/yacc/lib/base.api
{
    include package   rw_vector;
    include package   list;

    infix my 9  get ;

    Pairlist (X,Y) = EMPTY
                   | PAIR  (X, Y, Pairlist( X, Y ));

    Terminal    =    TERM  Int;
    Nonterminal = NONTERM  Int;
    State       =   STATE  Int;

    Action  = SHIFT   State
            | REDUCE  Int       #  rulenum from grammar 
            | ACCEPT
            | ERROR;

    exception GOTO  (State, Nonterminal);

     Table
        =
        {   states:        Int,
            rules:         Int,
            initial_state:  State,
            action:        Rw_Vector( (Pairlist( Terminal, Action ), Action) ),
            goto:          Rw_Vector( Pairlist( Nonterminal, State ) )
        };

    state_count = \\ ( { states, ... } : Table) => states; end ;
    rule_count  = \\ ( { rules, ... } : Table) => rules; end ;

    describe_actions
       =
       \\ ( { action, ... } : Table)
           =
           \\ (STATE s)
               =
               action[s];

    describe_goto
       =
       \\ ( { goto, ... } : Table)
           =
           \\ (STATE s)
               =
               goto[s];

    fun find_term (TERM term, row, default)
        =
        {   fun find (PAIR (TERM key, data, r))
                    =>
                    if   (key <  term)   find r;
                    elif (key == term)   data;
                    else                 default;
                    fi;

                find EMPTY =>   default;
            end;

            find row;
            };

        fun find_nonterm (NONTERM nt, row)
            =
            find row
            where
                fun find (PAIR (NONTERM key, data, r))
                       =>
                       if   (key <  nt)   find r;
                       elif (key == nt)   THE data;
                       else               NULL;
                       fi;

                    find EMPTY => NULL;
                end;
            end;

        action
            =
            \\ ( { action, ... } : Table)
                =
                \\ (STATE state, term)
                  =
                  {   my (row, default) = action[state];
                      find_term (term, row, default);
                  };

        goto = \\ ( { goto, ... } : Table)
                   =>
                   \\ (a as (STATE state, nonterm))
                       =>
                       case (find_nonterm (nonterm, goto[state]))
                         
                            THE state =>   state;
                            NULL      =>   raise exception (GOTO a);
                       esac;
                   end;
               end;

        initial_state
            =
           \\ ( { initial_state, ... } : Table) =  initial_state;

        make_lr_table
            =
            \\ { actions, gotos, initial_state, state_count, rule_count }
                =
                ( { action=>actions, goto=>gotos,
                    states=>state_count,
                    rules=>rule_count,
                    initial_state
                  }
                  : Table
                );
};


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext