public static ScannerErg GetWord(string Data, int spos, char[] Endsigns, char[] DelSigns) { char sign; char Change = '\\'; ScannerErg scnErg = new ScannerErg(); scnErg.setTermSignNr(-1); int endpos = Data.Length; String Word = ""; bool NoEndNext = false; while (spos < endpos) { spos = DelNoSigns(Data, spos, DelSigns); sign = Data.ToCharArray()[spos]; int nr = DoesContain(sign, Endsigns); if (NoEndNext == true) { NoEndNext = false; } else if (sign == Change) { NoEndNext = true; } if (nr >= 0) { if (Endsigns[nr] == Change) { NoEndNext = false; } if (NoEndNext == false) { scnErg.setTermSignNr(nr); spos++; break; } } Word += sign; spos++; } scnErg.setSpos(spos); scnErg.setWord(Word); return(scnErg); }
private bool ParseBNF(string BNF) { bool bTerminal = false; bool bDelNoSigns = true; int automstate = 0; int len = BNF.Length; int spos = 0; char sign; int failureState = 100; char[] DelSigns = new char[] { ' ', '\n', '\r', '\t' }; BNFRule actRule = null; while (spos < len) { if (bDelNoSigns) { spos = ParseHelper.DelNoSigns(BNF, spos, DelSigns); } if (spos < len) { sign = BNF.ToCharArray()[spos]; switch (automstate) { case 0: { actRule = null; if (sign == '#') { spos = ParseHelper.DelComment(BNF, spos); } else { automstate = 1; } break; } case 1: { char[] EndSigns = new char[] { '-' }; ScannerErg scnErg = ParseHelper.GetWord(BNF, spos, EndSigns, DelSigns); if (scnErg.getTermSignNr() == 0) { actRule = new BNFRule(m_Rules.Count); actRule.SetStart(scnErg.getWord()); m_Rules.Add(actRule); automstate = 2; } else { automstate = failureState; } spos = scnErg.getSpos(); break; } case 2: { if (spos < len && BNF.ToCharArray()[spos] == '>') { automstate = 3; spos++; } else { automstate = failureState; } break; } case 3: { char[] EndSigns = new char[] { ',', '\'', '|', '.', '$' }; ScannerErg scnErg = ParseHelper.GetWord(BNF, spos, EndSigns, DelSigns); if (scnErg.getTermSignNr() == 0) { actRule.SetNextToken(scnErg.getWord(), bTerminal); automstate = 3; bTerminal = false; } else if (scnErg.getTermSignNr() == 1) { bTerminal = true; bDelNoSigns = false; automstate = 4; } else if (scnErg.getTermSignNr() == 2) { if (scnErg.getWord().Length != 0) { actRule.SetNextToken(scnErg.getWord(), bTerminal); } else { actRule.SetNextToken(scnErg.getWord(), true); } string StartSign = actRule.GetStart().GetToken(); actRule = new BNFRule(m_Rules.Count); actRule.SetStart(StartSign); m_Rules.Add(actRule); automstate = 3; bTerminal = false; } else if (scnErg.getTermSignNr() == 3) { if (scnErg.getWord().Length != 0) { actRule.SetNextToken(scnErg.getWord(), bTerminal); } else { actRule.SetNextToken(scnErg.getWord(), true); } automstate = 0; bTerminal = false; } else if (scnErg.getTermSignNr() == 4) { automstate = 3; bTerminal = true; } else { automstate = failureState; } spos = scnErg.getSpos(); break; } case 4: { char[] EndSigns = new char[] { '\'' }; ScannerErg scnErg = ParseHelper.GetWord(BNF, spos, EndSigns, null); if (scnErg.getTermSignNr() == 0) { actRule.SetNextToken(scnErg.getWord(), bTerminal); automstate = 5; bTerminal = false; } else { automstate = failureState; } bDelNoSigns = true; spos = scnErg.getSpos(); break; } case 5: { char[] EndSigns = new char[] { ',', '|', '.' }; ScannerErg scnErg = ParseHelper.GetWord(BNF, spos, EndSigns, DelSigns); if (scnErg.getTermSignNr() == 0) { automstate = 3; bTerminal = false; } else if (scnErg.getTermSignNr() == 1) { string StartSign = actRule.GetStart().GetToken(); actRule = new BNFRule(m_Rules.Count); actRule.SetStart(StartSign); m_Rules.Add(actRule); automstate = 3; bTerminal = false; } else if (scnErg.getTermSignNr() == 2) { automstate = 0; bTerminal = false; } else { automstate = failureState; } spos = scnErg.getSpos(); break; } default: { return(false); } } } } return(true); }