{-
 * this file will be appended to every parser YYgen.fr builds
 *
 -}

derive ArrayElement YYAction;

yyloop (yyvals@(st, _):_) (yytoks@yyt:yyts) =
    case yyaction st yyt of
        YYShift newst
            | yydebug, traceLn (show st ++ ": shift to " ++ show newst ++ " on " ++ yyshow yyt) = undefined
            | otherwise = yyloop ((newst, YYTok yyt):yyvals) yyts
        YYRed red
            | yydebug, traceLn (show st ++ ": reduce by rule " ++ yyrule red) = undefined
            | otherwise = yyloop (yyreduce red yyvals) yytoks
        YYErr -> let fun = elemAt yyrecs st in yyloop yyvals (fun yytoks)
        YYAccept -> yyaccept (yyvals, yytoks)  -- will this ever happen?
;
yyloop (yyvals@(st, _):_) [] =
    case yyeaction st of
        YYAccept -> yyaccept (yyvals, [])
        YYShift newst -> error ("Can't shift on end of file in state " ++ show st)
        YYRed red
            | yydebug, traceLn (show st ++ ": reduce by rule " ++ yyrule red) = undefined
            | otherwise = yyloop (yyreduce red yyvals) []
        YYErr -> yyerror yyEOF ("syntax error on end-of-file") `seq` (yyvals, [])
    ;
yyloop [] yytoks = error "empty stack in yyloop";

yyaction n tok = case itemAt yyacts n of {
    Just fun -> fun tok;
    Nothing  -> YYErr;
};

yyeaction n = case itemAt yyeacts n of  {
    Just this -> this;
    Nothing -> YYErr;
};


yybadprod p yyvs = error ("bad stack for rule " ++ show p ++ ": " ++ showst yyvs);



--- drop tokens until token is valid in this state
yydrop i [] = yyrecover i [];
yydrop i (t:ts) = case yyaction i t of {
        YYErr
            | yydebug, trace (show i ++ ": error recovery drops " ++ yyshow t ++ "\n") = undefined
            | otherwise = yydrop i ts;
        _ -> t:ts;
    };
yyrecover i []
    | yydebug, trace (show i ++ ": error recovery reaches end-of-file\n") = undefined
    | otherwise = [];

yyrecover i (ts@t:_) =
    yyerror (yyline t) ("syntax error on `" ++ yynice t ++ "`")
        `seq` yydrop i ts;

yyexpect n s trep (ts@t:_) =
    yyerror (yyline t) ("expected " ++ s ++ ", found `" ++ yynice t ++ "`")
        `seq` (trep:ts);
yyexpect n s trep [] =
    yyerror yyEOF ("expected " ++ s ++ ", found end of file")
        `seq` [trep];


yyreduce red yyvs = case itemAt yyprods red of
    Just fun -> case fun yyvs of
        (yyv, yyvals@(popst, _):_) ->
            case yygos.itemAt popst of
                Just arr -> case itemAt arr red of
                    go | go > 0, yydebug, trace (show popst
                                    ++ ": after reduction goto "
                                    ++ show go ++ "\n") = undefined
                       | go > 0 = (go, yyv):yyvals
                       | otherwise -> error ("nowhere to go in state "
                                                ++ show popst ++ " after reduction " ++ show red)
                Nothing -> error ("nowhere to go in state "
                                                ++ show popst ++ " after any reduction")
        _ -> error "empty stack after reduce"
    Nothing -> error ("illegal production " ++ show red)
;


yyparse yytoks = case yyloop [(0, YYStart ())] yytoks of {
    ([(_, YYAcc x)], []) -> Just x;
    ([(_, YYAcc x)], (yyt:_)) -> do yyerror yyEOF ("expected end of file, found `" ++ yynice yyt ++ "`") for Just x;
    _ -> Nothing;
};