Esempio n. 1
0
 public Parser()
 {
     SymbolTable     = new SymbolList();
     dfa             = new FAStateList();
     charSetTable    = new CharacterSetList();
     ProductionTable = new ProductionList();
     lrStates        = new LRStateList();
     stack           = new Stack <Token>();
     ExpectedSymbols = new SymbolList();
     inputTokens     = new Queue <Token>();
     sysPosition     = new Position();
     CurrentPosition = new Position();
     Grammar         = new GrammarProperties();
     groupStack      = new Stack <Token>();
     groupTable      = new GroupList();
     Restart();
     IsTablesLoaded = false;
     TrimReductions = false;
 }
Esempio n. 2
0
        public bool LoadTables(BinaryReader reader)
        {
            EGTReader egt = new EGTReader();
            bool      success;

            try
            {
                egt.Open(reader);
                Restart();
                success = true;
                while (!egt.IsEndOfFile || !success)
                {
                    egt.GetNextRecord();
                    EGTRecord recType = (EGTRecord)egt.RetrieveByte();

                    switch (recType)
                    {
                    case EGTRecord.DFAState:
                        int  index       = egt.RetrieveInt16();
                        bool accept      = egt.RetrieveBoolean();
                        int  acceptIndex = egt.RetrieveInt16();
                        egt.RetrieveEntry();     // Reserved
                        dfa[index] = !accept ? new FAState() : new FAState(SymbolTable[acceptIndex]);
                        while (!egt.IsReadComplete)
                        {
                            int setIndex = egt.RetrieveInt16();
                            int target   = egt.RetrieveInt16();
                            egt.RetrieveEntry();     // Reserved
                            dfa[index].Edges.Add(new FAEdge(this.charSetTable[setIndex], target));
                        }
                        break;

                    case EGTRecord.InitialStates:
                        dfa.InitialState      = egt.RetrieveInt16();
                        lrStates.InitialState = egt.RetrieveInt16();
                        break;

                    case EGTRecord.LRState:
                        index = egt.RetrieveInt16();
                        egt.RetrieveEntry();
                        lrStates[index] = new LRState();
                        while (!egt.IsReadComplete)
                        {
                            int symIndex = egt.RetrieveInt16();
                            int action   = egt.RetrieveInt16();
                            int target   = egt.RetrieveInt16();
                            egt.RetrieveEntry();
                            lrStates[index].Add(new LRAction(SymbolTable[symIndex], (LRActionType)action, (short)target));
                        }
                        break;

                    case EGTRecord.Production:
                        index = egt.RetrieveInt16();
                        int headIndex = egt.RetrieveInt16();
                        egt.RetrieveEntry();
                        ProductionTable[index] = new Production(SymbolTable[headIndex], (short)index);
                        while (!egt.IsReadComplete)
                        {
                            int symIndex = egt.RetrieveInt16();
                            ProductionTable[index].Handle.Add(SymbolTable[symIndex]);
                        }
                        break;

                    case EGTRecord.Symbol:
                        index = egt.RetrieveInt16();
                        string     name = egt.RetrieveString();
                        SymbolType type = (SymbolType)egt.RetrieveInt16();
                        SymbolTable[index] = new Symbol(name, type, (short)index);
                        break;

                    case EGTRecord.CharRanges:
                        index = egt.RetrieveInt16();
                        egt.RetrieveInt16();     // Codepage
                        int total = egt.RetrieveInt16();
                        egt.RetrieveEntry();     // Reserveds
                        charSetTable[index] = new CharacterSet();
                        while (!egt.IsReadComplete)
                        {
                            charSetTable[index].Add(new CharacterRange((ushort)egt.RetrieveInt16(), (ushort)egt.RetrieveInt16()));
                        }
                        break;

                    case EGTRecord.Group:
                        Group g = new Group();
                        index       = egt.RetrieveInt16();
                        g.Name      = egt.RetrieveString();
                        g.Container = SymbolTable[egt.RetrieveInt16()];
                        g.Start     = SymbolTable[egt.RetrieveInt16()];
                        g.End       = SymbolTable[egt.RetrieveInt16()];
                        g.Advance   = (Group.AdvanceMode)egt.RetrieveInt16();
                        g.Ending    = (Group.EndingMode)egt.RetrieveInt16();
                        egt.RetrieveEntry();

                        int count = egt.RetrieveInt16();
                        for (int i = 0; i < count; i++)
                        {
                            g.Nesting.Add(egt.RetrieveInt16());
                        }

                        g.Container.Group = g;
                        g.Start.Group     = g;
                        g.End.Group       = g;
                        groupTable[index] = g;
                        break;

                    case EGTRecord.Property:
                        index = egt.RetrieveInt16();
                        name  = egt.RetrieveString();    // Just discard
                        Grammar.SetValue(index, egt.RetrieveString());
                        break;

                    case EGTRecord.TableCounts:
                        SymbolTable     = new SymbolList(egt.RetrieveInt16());
                        charSetTable    = new CharacterSetList(egt.RetrieveInt16());
                        ProductionTable = new ProductionList(egt.RetrieveInt16());
                        dfa             = new FAStateList(egt.RetrieveInt16());
                        lrStates        = new LRStateList(egt.RetrieveInt16());
                        groupTable      = new GroupList(egt.RetrieveInt16());
                        break;

                    default:
                        throw new ParserException("File Error. A record of type '" + (char)recType + "' was read. This is not a valid code.");
                    }
                }

                egt.Dispose();
            }
            catch (Exception ex)
            {
                throw new ParserException(ex.Message, ex, nameof(LoadTables));
            }
            IsTablesLoaded = success;
            return(success);
        }