示例#1
0
 /// <summary>
 /// Creats a new instance of the <c>LRStateAction</c> class.
 /// </summary>
 /// <param name="index">Index of the LR state action.</param>
 /// <param name="symbol">Symbol associated with the action.</param>
 /// <param name="action">Action type.</param>
 /// <param name="value">Action value.</param>
 public LRStateAction(int index, Symbol symbol, LRAction action, int value)
 {
     m_index  = index;
     m_symbol = symbol;
     m_action = action;
     m_value  = value;
 }
示例#2
0
文件: Grammar.cs 项目: vol16bit/xenko
        /// <summary>
        /// Read LR state information.
        /// </summary>
        private void ReadLRStates()
        {
            int index = ReadInt16Entry();

            ReadEmptyEntry();
            LRStateAction[] stateTable = new LRStateAction[m_entryCount / 4];
            for (int i = 0; i < stateTable.Length; i++)
            {
                Symbol   symbol      = m_symbolTable[ReadInt16Entry()];
                LRAction action      = (LRAction)ReadInt16Entry();
                int      targetIndex = ReadInt16Entry();
                ReadEmptyEntry();
                stateTable[i] = new LRStateAction(i, symbol, action, targetIndex);
            }

            // Create the transition vector
            LRStateAction[] transitionVector = new LRStateAction[m_symbolTable.Length];
            for (int i = 0; i < transitionVector.Length; i++)
            {
                transitionVector[i] = null;
            }
            for (int i = 0; i < stateTable.Length; i++)
            {
                transitionVector[stateTable[i].Symbol.Index] = stateTable[i];
            }

            LRState lrState = new LRState(index, stateTable, transitionVector);

            m_lrStateTable[index] = lrState;
        }
示例#3
0
		/// <summary>
		/// Creats a new instance of the <c>LRStateAction</c> class.
		/// </summary>
		/// <param name="index">Index of the LR state action.</param>
		/// <param name="symbol">Symbol associated with the action.</param>
		/// <param name="action">Action type.</param>
		/// <param name="value">Action value.</param>
		public LRStateAction(Int32 index, Symbol symbol, LRAction action, Int32 value)
		{
			m_Index = index;
			m_Symbol = symbol;
			m_Action = action;
			m_Value = value;
		}
示例#4
0
		/// <summary>
		/// Creats a new instance of the <c>LRStateAction</c> class.
		/// </summary>
		/// <param name="index">Index of the LR state action.</param>
		/// <param name="symbol">Symbol associated with the action.</param>
		/// <param name="action">Action type.</param>
		/// <param name="value">Action value.</param>
		public LRStateAction(int index, Symbol symbol, LRAction action, int value)
		{
			m_index = index;
			m_symbol = symbol;
			m_action = action;
			m_value = value;
		}
示例#5
0
        // Methods
        private LRConflict ActionAdd(SymbolBuild TheSymbol, LRActionType Type, short Value = 0)
        {
            bool       flag2 = false;
            bool       flag  = false;
            LRConflict none  = LRConflict.None;

            for (short i = 0; ((i < base.Count) & !flag2) & !flag; i = (short)(i + 1))
            {
                LRAction action = base[i];
                if (action.Symbol.IsEqualTo(TheSymbol))
                {
                    if ((action.Type() == Type) & (action.Value() == Value))
                    {
                        flag = true;
                    }
                    else
                    {
                        none  = BuildLR.GetConflict(action.Type(), Type);
                        flag2 = true;
                    }
                }
            }
            if (!flag)
            {
                base.Add(new LRAction(TheSymbol, Type, Value));
            }
            return(none);
        }
示例#6
0
        public new LRConflict Add(LRAction Action)
        {
            LRAction    action = Action;
            SymbolBuild symbol = (SymbolBuild)action.Symbol;

            action.Symbol = symbol;
            return(this.ActionAdd(symbol, Action.Type(), Action.Value()));
        }
示例#7
0
        /// <summary>
        /// Loads the automaton as a RNGLR automaton
        /// </summary>
        /// <param name="reader">The input reader</param>
        private void LoadRNGLR(BinaryReader reader)
        {
            RNGLRAutomaton temp = new RNGLRAutomaton(reader);

            for (int i = 0; i != temp.StatesCount; i++)
            {
                automaton.AddState(new Automata.LRState(i));
            }
            for (int i = 0; i != temp.StatesCount; i++)
            {
                Automata.LRState state = automaton.States[i];
                foreach (Symbol terminal in terminals)
                {
                    int count = temp.GetActionsCount(i, terminal.ID);
                    for (int j = 0; j != count; j++)
                    {
                        LRAction action = temp.GetAction(i, terminal.ID, j);
                        switch (action.Code)
                        {
                        case LRActionCode.Accept:
                            state.IsAccept = true;
                            break;

                        case LRActionCode.Shift:
                            state.AddTransition(new Automata.LRTransition(terminal, automaton.States[action.Data]));
                            break;

                        case LRActionCode.Reduce:
                            LRProduction prod = temp.GetProduction(action.Data);
                            state.AddReduction(new Automata.LRReduction(terminal, variables[prod.Head], prod.ReductionLength));
                            break;
                        }
                    }
                }
                foreach (Symbol variable in variables)
                {
                    int count = temp.GetActionsCount(i, variable.ID);
                    for (int j = 0; j != count; j++)
                    {
                        LRAction action = temp.GetAction(i, variable.ID, j);
                        if (action.Code == LRActionCode.Shift)
                        {
                            state.AddTransition(new Automata.LRTransition(variable, automaton.States[action.Data]));
                        }
                    }
                }
            }
        }
示例#8
0
        public LRConflict ConflictForAction(SymbolBuild TheSymbol, LRActionType Type, short Value)
        {
            bool       flag = false;
            LRConflict none = LRConflict.None;

            for (short i = 0; (i < base.Count) & !flag; i = (short)(i + 1))
            {
                LRAction action = base[i];
                if (action.Symbol.IsEqualTo(TheSymbol))
                {
                    if ((action.Type() == Type) & (action.Value() == Value))
                    {
                        none = LRConflict.None;
                    }
                    else
                    {
                        none = BuildLR.GetConflict(action.Type(), Type);
                    }
                    flag = true;
                }
            }
            return(none);
        }
示例#9
0
文件: EGT.cs 项目: cwellsx/GOLDEngine
 internal Production GetProduction(LRAction ParseAction)
 {
     return m_ProductionTable[ParseAction.Value];
 }
示例#10
0
 public void Add(LRAction action)
 {
     base.Add(action);
 }
示例#11
0
        internal ParseResult ParseLALR(Token NextToken)
        {
            //This function analyzes a token and either:
            //  1. Makes a SINGLE reduction and pushes a complete Reduction object on the m_Stack
            //  2. Accepts the token and shifts
            //  3. Errors and places the expected symbol indexes in the Tokens list
            //The Token is assumed to be valid and WILL be checked
            //If an action is performed that requires controlt to be returned to the user, the function returns true.
            //The Message parameter is then set to the type of action.

            LRAction ParseAction = m_loaded.FindLRAction(m_CurrentLALR, NextToken.Symbol);

            // Work - shift or reduce
            if ((ParseAction != null))
            {
                //'Debug.WriteLine("Action: " & ParseAction.Text)

                switch (ParseAction.Type)
                {
                case LRActionType.Accept:
                    return(ParseResult.Accept);

                case LRActionType.Shift:
                    Push(NextToken, ParseAction.Value);
                    return(ParseResult.Shift);

                case LRActionType.Reduce:
                    //Produce a reduction - remove as many tokens as members in the rule & push a nonterminal token
                    Production Prod = m_loaded.GetProduction(ParseAction);

                    ParseResult Result;
                    Token       Head;
                    //======== Create Reduction
                    if (m_TrimReductions & Prod.ContainsOneNonTerminal())
                    {
                        //The current rule only consists of a single nonterminal and can be trimmed from the
                        //parse tree. Usually we create a new Reduction, assign it to the Data property
                        //of Head and push it on the m_Stack. However, in this case, the Data property of the
                        //Head will be assigned the Data property of the reduced token (i.e. the only one
                        //on the m_Stack).
                        //In this case, to save code, the value popped of the m_Stack is changed into the head.

                        //Build a Reduction
                        Head = m_Stack.Pop().Token;
                        Head.TrimReduction(Prod.Head());

                        Result = ParseResult.ReduceEliminated;
                    }
                    else
                    {
                        int          nTokens = Prod.Handle().Count;
                        List <Token> tokens  = new List <Token>(nTokens);
                        for (int i = Prod.Handle().Count - 1; i >= 0; --i)
                        {
                            TokenState popped = Pop();
                            while (popped.IsExtra)
                            {
                                tokens.Insert(0, popped.Token);
                                popped = Pop();
                            }
                            tokens.Insert(0, popped.Token);
                        }
                        //Say that the reduction's Position is the Position of its first child.
                        Head   = new Reduction(Prod, tokens.ToArray());
                        Result = ParseResult.ReduceNormal;
                    }

                    //========== Goto
                    short GotoState = m_Stack.Peek().GotoState;

                    LRAction found = m_loaded.FindLRAction(GotoState, Prod.Head());
                    if ((found != null) && (found.Type == LRActionType.Goto))
                    {
                        Push(Head, found.Value);
                        return(Result);
                    }
                    else
                    {
                        //========= If action not found here, then we have an Internal Table Error!!!!
                        return(ParseResult.InternalError);
                    }

                default:
                    return(ParseResult.InternalError);
                }
            }
            else
            {
                return(ParseResult.SyntaxError);
            }
        }