예제 #1
0
		private void Reset()
		{
			stream = null;
			structure = null;
			content = null;
			dfaStates = null;
			parserStates = null;
			symbols = null;
			rules = null;
		}
예제 #2
0
파일: Reader.cs 프로젝트: langpavel/LPS-old
 /// <summary>
 /// Reads a CGT and creates all the objects needed to create
 /// a tokenizer and parser at a later time.
 /// </summary>
 /// <param name="stream">The CGT stream.</param>
 private void ReadFile(Stream stream)
 {
     try
     {
         Reset();
         this.stream = stream;
         CalithaBinReader reader = new CalithaBinReader(stream);
         string header = "";
         try
         {
             header = reader.ReadUnicodeString();
             if (! header.StartsWith("GOLD"))
                 throw new CGTStructureException("File header is invalid");
         }
         catch (EndOfStreamException e)
         {
             throw new CGTStructureException("File header is invalid",e);
         }
         RecordCollection records = new RecordCollection();
         while (!(stream.Position == stream.Length))
         {
             records.Add(ReadRecord(reader));
         }
         structure = new CGTStructure(header,records);
         content = new CGTContent(structure);
         dfaStates = CreateDFAStates(content);
         parserStates = CreateParserStates(content);
     }
     finally
     {
         stream.Close();
     }
 }
예제 #3
0
파일: Reader.cs 프로젝트: langpavel/LPS-old
 private SymbolCollection CreateSymbols(CGTContent content)
 {
     SymbolCollection symbols = new SymbolCollection();
     foreach (SymbolRecord symbolRecord in content.SymbolTable)
     {
         Symbol symbol = SymbolFactory.CreateSymbol(symbolRecord);
         symbols.Add(symbol);
     }
     return symbols;
 }
예제 #4
0
파일: Reader.cs 프로젝트: langpavel/LPS-old
        private RuleCollection CreateRules(CGTContent content)
        {
            RuleCollection rules = new RuleCollection();
            foreach (RuleRecord ruleRecord in content.RuleTable)
            {
                SymbolNonterminal lhs = symbols[ruleRecord.Nonterminal] as SymbolNonterminal;
                //todo: exception handling?
                Symbol[] rhs = new Symbol[ruleRecord.Symbols.Count];
                for (int i = 0; i< rhs.Length; i++)
                {
                    rhs[i] = symbols[ruleRecord.Symbols[i]];
                }

                Rule rule = new Rule(ruleRecord.Index,lhs,rhs);
                rules.Add(rule);
            }
            return rules;
        }
예제 #5
0
파일: Reader.cs 프로젝트: langpavel/LPS-old
        private StateCollection CreateParserStates(CGTContent content)
        {
            rules = CreateRules(content);

            StateCollection states = new StateCollection();
            foreach (LALRStateRecord record in content.LALRStateTable)
            {
                State state = new State(record.Index);
                states.Add(state);
            }

            foreach (LALRStateRecord record in content.LALRStateTable)
            {
                State state = states[record.Index];
                foreach (ActionSubRecord subRecord in record.ActionSubRecords)
                {
                    Action action =
                        ActionFactory.CreateAction(subRecord,
                                                   states,
                                                   symbols,
                                                   rules);
                    state.Actions.Add(action);
                }

            }
            return states;
        }
예제 #6
0
파일: Reader.cs 프로젝트: langpavel/LPS-old
        private DFA.StateCollection CreateDFAStates(CGTContent content)
        {
            symbols = CreateSymbols(content);
            DFA.StateCollection states = new DFA.StateCollection();
            foreach (DFAStateRecord stateRecord in content.DFAStateTable)
            {
                DFA.State state;
                if (stateRecord.AcceptState)
                {
                    Symbol symbol = symbols[stateRecord.AcceptIndex];

                    state = new DFA.EndState(stateRecord.Index,(SymbolTerminal)symbol);
                    //todo: type checking (exception?)
                }
                else
                {
                    state = new DFA.State(stateRecord.Index);
                }
                states.Add(state);
            }

            foreach (DFAStateRecord stateRecord in content.DFAStateTable)
            {
                foreach (EdgeSubRecord edgeRecord in stateRecord.EdgeSubRecords)
                {
                    DFA.State source = states[stateRecord.Index];
                    DFA.State target = states[edgeRecord.TargetIndex];
                    CharacterSetRecord charsetRec = content.CharacterSetTable[edgeRecord.CharacterSetIndex];
                    DFA.Transition transition = new DFA.Transition(target,charsetRec.Characters);
                    source.Transitions.Add(transition);
                }
            }
            return states;
        }