/// <summary> /// Método genérico de teste para análise sintática /// </summary> /// <param name="syntax"></param> /// <param name="type"></param> private void SyntaxTestExecution(SyntaxAnalizer syntax, SyntaxAnalizerType type) { /* * Todos esses inputs são corretos * Input: * * LET A = 4 * LET B = 5*6 * 123 LET C = B/A * PRINT 2+3, "Olá" * INPUT D * GOTO 123 * GOSUB (123) * IF (A-B) = C THEN (C-1) * LIST A,B,C */ string[] input = IOUtils.ReadTxtFile(syntaxAnalizerTestFile1); bool validation = false; string feedback = ""; //Validação do arquivo com todas as linhas corretas int index = 0; foreach (string line in input) { string lline = line; syntax.Lexic.File = new string[] { lline }; feedback = ""; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, "Validação \"" + lline + "\"" + index + " deu negativa, feedback: " + feedback); index++; } input = IOUtils.ReadTxtFile(syntaxAnalizerTestFile2); //Validação do arquivo com todas as linhas inválidas index = 0; foreach (string line in input) { string lline = line; syntax.Lexic.File = new string[] { lline }; feedback = ""; validation = syntax.Analize(ref feedback); Assert.IsTrue(!validation, "Validação " + index + " deu positiva, feedback: " + feedback); index++; } feedback = ""; input = IOUtils.ReadTxtFile(syntaxAnalizerTestFile3); syntax.Lexic.File = input; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, "Validação " + index + " deu negativa, feedback: " + feedback); }
private void syntaxAnalyzerToolStripMenuItem_Click(object sender, EventArgs e) { int index = tabControl.SelectedIndex; FileHandlerForm fileHandler = FileHandlerHash[index]; Lexic.File = fileHandler.GetTextLines(); if ((Lexic.File != null) && (Lexic.File.Length > 0)) { if (radioA.Checked) { syntaxAnalizerType = SyntaxAnalizerType.ASCENDENT; } else if (radioRD.Checked) { syntaxAnalizerType = SyntaxAnalizerType.RECURSIVE_DESCENDENT; } else if (radioNRD.Checked) { syntaxAnalizerType = SyntaxAnalizerType.NON_RECURSIVE_DESCENDENT; } //Chama um analisador sintático do tipo setado, por default o tipo é Descendente Recursivo Syntax = new SyntaxAnalizer(Lexic, syntaxAnalizerType); string feedback = ""; syntaxAnalyzeSuccess = Syntax.Analize(ref feedback); string msg = ""; string title = ""; if (syntaxAnalyzeSuccess) { msg = "Sucesso na Análise Sintática!"; title = "Sucesso"; } else { msg = "Erro na Análise Sintática, feedback: \r\n" + feedback; title = "Erro"; } MessageBox.Show(msg, title); } }
public void SyntaxAnalize_NonRecursiveDescendent_Test() { SyntaxAnalizer syntax = new SyntaxAnalizer(lexic, SyntaxAnalizerType.NON_RECURSIVE_DESCENDENT); string grammarIdentifier = Symbol.GrammarIdentifier; Symbol.GrammarIdentifier = "9"; string line; bool validation; string feedback; line = "123 LET I = 12"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line = "GOTO 123"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line = "IF (A-B) = C THEN LET C = C-1"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line = "IF (A-B) = C THEN IF (C-1) = 5 THEN GOTO 123"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line = "12 IF I <= 10 THEN GOTO 5"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line = "REM \"CODIGO EXEMPLO DE STRING\""; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); SyntaxTestExecution(syntax, SyntaxAnalizerType.NON_RECURSIVE_DESCENDENT); Symbol.GrammarIdentifier = grammarIdentifier; }
/// <summary> /// Método de testes simples para o analizador ascendente /// </summary> /// <param name="syntax"></param> /// <param name="line1"></param> /// <param name="validation"></param> /// <param name="feedback"></param> private static void SimpleTests_Ascendent(SyntaxAnalizer syntax, out string line1, out bool validation, out string feedback) { line1 = "PRINT \"oi\""; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT \"oi\", \"oi\""; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT \"oi\", \"oi\", \"oi\""; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT 1"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT 1, 2, 3"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT \"oi\", \"oi\", \"oi\", \"oi\""; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT ( 1 )"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); }
public void SyntaxAnalize_Ascendent_Test() { string grammarIdentifier = Symbol.GrammarIdentifier; Symbol.GrammarIdentifier = "10"; SyntaxAnalizer syntax = new SyntaxAnalizer(lexic, SyntaxAnalizerType.ASCENDENT); string line1; bool validation; string feedback; #region SimpleTests SimpleTests_Ascendent(syntax, out line1, out validation, out feedback); #endregion line1 = "PRINT 1 + 2"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT 1 + 2 * 3"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT 1 + 2 * (3 + 1)"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT 1 + 2 * (3 + (1*2))"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "PRINT 1 + 2 * (3 + (1*))"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(!validation, feedback); line1 = "INPUT A"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "INPUT A, B"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "LET A = 12"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "LET A = (1 + 2)"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "REM \"oi\""; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "CLEAR"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "IF 1 < 2 THEN LET A = 1"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "IF 1 < 2 THEN IF 1 < 2 THEN LET A = 1"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); line1 = "IF 1 < 2 THEN IF 1 < 2 THEN IF 1 < 2 THEN LET A = 1"; validation = false; feedback = ""; syntax.Lexic.File = new string[] { line1 }; validation = syntax.Analize(ref feedback); Assert.IsTrue(validation, feedback); Symbol.GrammarIdentifier = grammarIdentifier; }