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); }
/// <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(); } }
private void Reset() { stream = null; structure = null; content = null; dfaStates = null; parserStates = null; symbols = null; rules = null; }
/// <summary> /// Creates a new DFA. /// </summary> /// <param name="states">The states that are part of the DFA.</param> /// <param name="startState">The starting state</param> public DFA(StateCollection states, State startState) { this.states = states; this.startState = startState; this.currentState = startState; }
/// <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(); } }
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; }