/// <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 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; }
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; }
private void Reset() { stream = null; structure = null; content = null; dfaStates = null; parserStates = null; symbols = null; rules = null; }
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; }
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; }