private string GetSyntaxErrorMessage() { StringBuilder errorMsg = new StringBuilder(); errorMsg.AppendFormat("syntax error, unexpected {0}", Parser.TerminalToString(_nextToken)); if (_currentState.Actions.Count < 7) { bool first = true; foreach (int terminal in _currentState.Actions.Keys) { if (first) { errorMsg.Append(", expecting "); } else { errorMsg.Append(", or "); } errorMsg.Append(Parser.TerminalToString(terminal)); first = false; } } return(errorMsg.ToString()); }
public void DumpTables(TextWriter /*!*/ output) { #if DEBUG Dictionary <int, bool> terminals = new Dictionary <int, bool>(); Dictionary <int, bool> nonterminals = new Dictionary <int, bool>(); int termCount = -1; int ntermCount = -1; for (int q = 0; q < _states.Length; q++) { State s = _states[q]; if (s.Actions != null) { foreach (int t in s.Actions.Keys) { if (t > termCount) { termCount = t; } terminals[t] = true; } } if (s.GotoStates != null) { foreach (int t in s.GotoStates.Keys) { if (t > ntermCount) { ntermCount = t; } nonterminals[t] = true; } } } output.WriteLine("States x (Terms + NonTerms) = {0} x ({1} + {2})", _states.Length, termCount, ntermCount); output.Write("State,"); output.Write("Default,"); for (int t = 0; t < termCount; t++) { if (terminals.ContainsKey(t)) { output.Write(Parser.TerminalToString(t)); output.Write(","); } } for (int t = 0; t < ntermCount; t++) { if (nonterminals.ContainsKey(t)) { output.Write(t); // TODO output.Write(","); } } for (int q = 0; q < _states.Length; q++) { State s = _states[q]; output.Write(q); output.Write(","); if (s.Actions == null) { output.Write(ActionToString(s.DefaultAction)); } output.Write(","); for (int t = 0; t < termCount; t++) { if (terminals.ContainsKey(t)) { int action; if (s.Actions != null) { s.Actions.TryGetValue(t, out action); output.Write(ActionToString(action)); } output.Write(","); } } for (int t = 0; t < ntermCount; t++) { if (nonterminals.ContainsKey(t)) { if (s.GotoStates != null) { int state; if (s.GotoStates.TryGetValue(t, out state)) { output.Write(state); } } output.Write(","); } } output.WriteLine(); } #endif }
// < 0 -> non-terminal // > 0 -> terminal internal string SymbolToString(int symbol) { return((symbol < 0) ? NonTerminalToString(-symbol) : Parser.TerminalToString(symbol)); }