Beispiel #1
0
        private void HandleSyntaxError(LRActionList actionList, Token read)
        {
            List <Symbol> expectedSymbols = new List <Symbol>();

            for (int i = 0; i < actionList.Count; i++)
            {
                Symbol actionSymbol = actionList[i];
                switch (actionSymbol.Type)
                {
                case SymbolType.Content:
                case SymbolType.End:
                case SymbolType.GroupStart:
                case SymbolType.GroupEnd:
                    expectedSymbols.Add(actionSymbol);
                    break;
                }
            }

            OnSyntaxError(read.EndPosition, read.Data, String.Join(",", expectedSymbols));
        }
        private void HandleSyntaxError(LRActionList actionList, Token read)
        {
            List<Symbol> expectedSymbols = new List<Symbol>();
            for (int i = 0; i < actionList.Count; i++)
            {
                Symbol actionSymbol = actionList[i];
                switch (actionSymbol.Type)
                {
                    case SymbolType.Content:
                    case SymbolType.End:
                    case SymbolType.GroupStart:
                    case SymbolType.GroupEnd:
                        expectedSymbols.Add(actionSymbol);
                        break;
                }
            }

            OnSyntaxError(read.EndPosition, read.Data, String.Join(",", expectedSymbols));
        }
Beispiel #3
0
        public bool Parse(TextReader reader)
        {
            if (egtDataManager == null)
            {
                OnNotLoadedError();
                return(false);
            }

            TokenQueueStack inputTokens = new TokenQueueStack();
            Stack <Token>   tokenStack  = new Stack <Token>();

            tokenStack.Push(new Token());
            Stack <Token>   groupStack      = new Stack <Token>();
            LookAheadBuffer lookAheadBuffer = new LookAheadBuffer(reader);
            ushort          lalrState       = 0;

            while (true)
            {
                if (inputTokens.Count == 0)
                {
                    inputTokens.Push(ProduceToken(groupStack, lookAheadBuffer));
                    OnTokenRead(inputTokens.Peek());
                }
                //Runaway group
                else if (groupStack.Count != 0)
                {
                    OnGroupError();
                    return(false); //Error; abort
                }
                else
                {
                    Token read = inputTokens.Peek();

                    switch (read.Type)
                    {
                    case SymbolType.Noise:
                        inputTokens.Pop();
                        break;

                    case SymbolType.Error:
                        OnLexicalError(read.EndPosition, read.Data);
                        return(false);    //Error; abort

                    default:
                        LRActionList actionList  = egtDataManager.GetLRActionList(lalrState);
                        LRAction     parseAction = actionList[read.Symbol];
                        if (parseAction == null)
                        {
                            HandleSyntaxError(actionList, read);
                            return(false);
                        }

                        switch (parseAction.Type)
                        {
                        case LRActionType.Accept:
                            OnCompleted(tokenStack.Peek().Tag);
                            return(true);

                        case LRActionType.Shift:
                            lalrState  = parseAction.Value;
                            read.State = lalrState;
                            tokenStack.Push(read);
                            //It now exists on the Token-Stack and must be eliminated from the queue.
                            inputTokens.Dequeue();
                            break;

                        case LRActionType.Reduce:
                            if (!DoReduction(tokenStack, lookAheadBuffer, ref lalrState, parseAction))
                            {
                                return(false);
                            }
                            break;
                        }
                        break;
                    }
                }
            }
        }