Ejemplo n.º 1
0
        static public void parse()
        {
            SourceViewModel.KeepOnlyRead();
            Reset();

            stateStack.Push(0);
            tokenStack.Push(new Token(0, "#", TokenKind.END));

            bool result       = false;
            bool breakThrough = false;

            Lexer.Reset();

            Action    action;
            FixAction fixAction;

            token = Lexer.LexNext();
            while (true)
            {
                if (token != null)
                {
                    if (/*hasErr && token.kind == TokenKind.END || */ breakThrough)
                    {
                        break;
                    }
                    action = ActionTable.action(stateStack.Peek(), token.kind);
                    if (action != null)
                    {
                        switch (action.Kind)
                        {
                        case ActionType.ACCEPT:
                            result = true;
                            break;

                        case ActionType.SHIFT:
                            stateStack.Push(action.value);
                            tokenStack.Push(token);
                            token = Lexer.LexNext();
                            break;

                        case ActionType.REDUCE:
                            Reduce(action.value);
                            break;

                        case ActionType.ERROR:
                            fixAction = ActionTable.getFixAction(stateStack.Peek());
                            if (action.value != -1)
                            {
                                ErrorHandle(token, action.information);
                            }
                            hasErr = true;
                            if (fixAction != null)
                            {
                                switch (fixAction.Kind)
                                {
                                case ActionType.ACCEPT:
                                    throw new NotImplementedException();

                                case ActionType.CHANGE:
                                    stateStack.Push(fixAction.value);
                                    if (fixAction.missToken.HasValue)
                                    {
                                        tokenStack.Push(new Token(token.offset, "", fixAction.missToken.Value));
                                    }
                                    else
                                    {
                                        breakThrough = true;
                                    }
                                    break;

                                case ActionType.ERROR:
                                    breakThrough = true;
                                    break;

                                case ActionType.SHIFT:
                                    token = Lexer.LexNext();
                                    if (token.kind == TokenKind.END)
                                    {
                                        breakThrough = true;
                                    }
                                    break;

                                case ActionType.REDUCE:
                                    Reduce(fixAction.value);
                                    break;
                                }
                            }
                            break;
                        }
                        if (result)
                        {
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            if (result && !hasErr)
            {
                StateViewModel.Display("成功");
                if (processStack.Count > 0)
                {
                    ProcessViewModel.Add(processStack.Peek());
                }
            }
            else
            {
                StateViewModel.Display("失败");
            }

            SourceViewModel.UnkeepOnlyRead();
        }