From 6532ce82f7b6b7f09259d9ddd3167a5cee64cbd7 Mon Sep 17 00:00:00 2001 From: unknown <user@mil8a-202a-05.mmcs.sfedu.ru> Date: Mon, 26 Sep 2022 13:23:46 +0300 Subject: [PATCH] lab2 done --- Module2/SimpleLangLexer/SimpleLexer.cs | 159 +++++++++++++++++++++---- TestSimpleLexer/Tests.cs | 12 +- 2 files changed, 144 insertions(+), 27 deletions(-) diff --git a/Module2/SimpleLangLexer/SimpleLexer.cs b/Module2/SimpleLangLexer/SimpleLexer.cs index 7bd0af4..aedad81 100644 --- a/Module2/SimpleLangLexer/SimpleLexer.cs +++ b/Module2/SimpleLangLexer/SimpleLexer.cs @@ -83,7 +83,8 @@ namespace SimpleLexer NextLexem(); // Считать первую лексему, заполнив LexText, LexKind и, возможно, LexValue } - public void Init() { + public void Init() + { } @@ -100,9 +101,14 @@ namespace SimpleLexer keywordsMap["begin"] = Tok.BEGIN; keywordsMap["end"] = Tok.END; keywordsMap["cycle"] = Tok.CYCLE; - } + keywordsMap["div"] = Tok.DIV; + keywordsMap["mod"] = Tok.MOD; + keywordsMap["and"] = Tok.AND; + keywordsMap["or"] = Tok.OR; + keywordsMap["not"] = Tok.NOT; + } - public string FinishCurrentLine() + public string FinishCurrentLine() { return CurrentLineText + inputReader.ReadLine(); } @@ -156,24 +162,135 @@ namespace SimpleLexer LexText = ""; LexRow = row; LexCol = col; - // Тип лексемы определяется по ее первому символу - // Для каждой лексемы строится синтаксическая диаграмма - if (currentCh == ';') - { - NextCh(); - LexKind = Tok.SEMICOLON; - } - else if (currentCh == ':') - { - NextCh(); - if (currentCh != '=') - { - LexError("= was expected"); - } - NextCh(); - LexKind = Tok.ASSIGN; - } - else if (char.IsLetter(currentCh)) + // Тип лексемы определяется по ее первому символу + // Для каждой лексемы строится синтаксическая диаграмма + if (currentCh == ';') + { + NextCh(); + LexKind = Tok.SEMICOLON; + } + else if (currentCh == ':') + { + NextCh(); + if (currentCh == '=') + { + NextCh(); + LexKind = Tok.ASSIGN; + } + else + { + LexKind = Tok.COLON; + } + + } + else if (currentCh == ',') + { + NextCh(); + LexKind = Tok.COMMA; + } + else if (currentCh == '+') + { + NextCh(); + if (currentCh == '=') + { + NextCh(); + LexKind = Tok.PLUSASSIGN; + } + else + { + LexKind = Tok.PLUS; + } + } + else if (currentCh == '-') + { + NextCh(); + if (currentCh == '=') + { + NextCh(); + LexKind = Tok.MINUSASSIGN; + } + else + { + LexKind = Tok.MINUS; + } + } + else if (currentCh == '*') + { + NextCh(); + if (currentCh == '=') + { + NextCh(); + LexKind = Tok.MULTASSIGN; + } + else + { + LexKind = Tok.MULT; + } + } + else if (currentCh == '/') + { + NextCh(); + if (currentCh == '=') + { + NextCh(); + LexKind = Tok.DIVASSIGN; + } + else if (currentCh == '/') + { + while (currentCh != '\0' && currentCh != '\n') + NextCh(); + if (currentCh == '\0') + LexKind = Tok.EOF; + else + NextLexem(); + } + else + { + LexKind = Tok.DIVISION; + } + } + else if (currentCh == '>') + { + NextCh(); + LexKind = Tok.GT; + if (currentCh == '=') + { + NextCh(); + LexKind = Tok.GEQ; + } + } + else if (currentCh == '<') + { + NextCh(); + LexKind = Tok.LT; + if (currentCh == '=') + { + NextCh(); + LexKind = Tok.LEQ; + } + if (currentCh == '>') + { + NextCh(); + LexKind = Tok.NEQ; + } + } + else if (currentCh == '=') + { + NextCh(); + LexKind = Tok.EQ; + } + else if (currentCh == '{') + { + while (currentCh != '}') + { + if (currentCh == '\0') + LexError("Неверное использование комментариев {}."); + NextCh(); + } + NextCh(); + NextLexem(); + } + else if (char.IsLetter(currentCh)) { while (char.IsLetterOrDigit(currentCh)) { diff --git a/TestSimpleLexer/Tests.cs b/TestSimpleLexer/Tests.cs index 1416597..96f9e96 100644 --- a/TestSimpleLexer/Tests.cs +++ b/TestSimpleLexer/Tests.cs @@ -19,7 +19,7 @@ namespace TestSimpleLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestSimpleLexer { public static List< KeyValuePair<Tok, string> > getLexerOutput(Lexer l) @@ -56,7 +56,7 @@ namespace TestSimpleLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestSimpleLexerOps { [Test] @@ -126,7 +126,7 @@ namespace TestSimpleLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestSimpleLexerAssigns { [Test] @@ -164,7 +164,7 @@ namespace TestSimpleLexer public class TestSimpleLexerComparisons { [Test] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public void TestComparisons() { string text = @"><>>=<=+<> > <="; @@ -218,7 +218,7 @@ namespace TestSimpleLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestSimpleLexerLineCmt { [Test] @@ -280,7 +280,7 @@ namespace TestSimpleLexer } [TestFixture] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public class TestSimpleLexerMultLineCmt { [Test] -- GitLab