/// <summary> /// Loads the specified DFA automaton /// </summary> /// <param name="automaton">An automaton</param> private void LoadDFA(Automaton automaton) { dfa = new Automata.DFA(); for (int i = 0; i != automaton.StatesCount; i++) { dfa.CreateState(); } for (int i = 0; i != automaton.StatesCount; i++) { Automata.DFAState current = dfa.States[i]; AutomatonState stateData = automaton.GetState(i); // retrieve the matched terminals for (int j = 0; j != stateData.TerminalsCount; j++) { MatchedTerminal mt = stateData.GetTerminal(j); Terminal terminal = terminals[mt.Index]; current.AddItem(terminal); if (mt.Context != 0) { terminals[mt.Index].Context = mt.Context; } } // retrieve the transitions for (int j = 0; j != 256; j++) { int next = stateData.GetCachedTransition(j); char c = Convert.ToChar(j); if (next != Automaton.DEAD_STATE) { current.AddTransition(new CharSpan(c, c), dfa.States[next]); } } for (int j = 0; j != stateData.BulkTransitionsCount; j++) { AutomatonTransition transition = stateData.GetBulkTransition(j); current.AddTransition(new CharSpan(Convert.ToChar(transition.Start), Convert.ToChar(transition.End)), dfa.States[transition.Target]); } current.RepackTransitions(); } }
/// <summary> /// Builds the lexer's data /// </summary> /// <param name="reporter">The reporter to use</param> /// <returns><c>true</c> if the operation succeed</returns> public bool BuildLexerData(Reporter reporter) { reporter.Info("Preparing " + grammar.Name + " lexer's data ..."); // build the lexer's dfa dfa = grammar.BuildDFA(); // check well-formedness foreach (Automata.FinalItem item in dfa.Entry.Items) { reporter.Error(string.Format("Terminal {0} can be an empty string, this is forbidden", item)); } if (dfa.Entry.IsFinal) { return(false); } // build the expected terminals if (!BuildExpected()) { return(false); } // build the separator return(BuildSeparator(reporter)); }
/// <summary> /// Initializes this generator /// </summary> /// <param name="dfa">The dfa to serialize</param> /// <param name="expected">The terminals produced by the DFA</param> public LexerDataGenerator(Automata.DFA dfa, ROList <Grammars.Terminal> expected) { this.dfa = dfa; terminals = expected; }