From 930bd7b1e4212751902e92f6473c35ba8746563b Mon Sep 17 00:00:00 2001 From: lusparon332 <lusparon222@gmail.com> Date: Mon, 10 Oct 2022 20:51:55 +0300 Subject: [PATCH] lab4 done --- Module2/SimpleLangLexer/SimpleLexer.cs | 30 ++++++- Module4/SimpleLangParser/SimpleLangParser.cs | 92 ++++++++++++++++++-- TestDescentParser/Tests.cs | 6 +- 3 files changed, 114 insertions(+), 14 deletions(-) diff --git a/Module2/SimpleLangLexer/SimpleLexer.cs b/Module2/SimpleLangLexer/SimpleLexer.cs index aedad81..6116e89 100644 --- a/Module2/SimpleLangLexer/SimpleLexer.cs +++ b/Module2/SimpleLangLexer/SimpleLexer.cs @@ -106,9 +106,19 @@ namespace SimpleLexer keywordsMap["and"] = Tok.AND; keywordsMap["or"] = Tok.OR; keywordsMap["not"] = Tok.NOT; - } + // РґРѕР±. RecursiveDescnetParser + keywordsMap["while"] = Tok.WHILE; + keywordsMap["do"] = Tok.DO; + keywordsMap["for"] = Tok.FOR; + keywordsMap["to"] = Tok.TO; + keywordsMap["if"] = Tok.IF; + keywordsMap["then"] = Tok.THEN; + keywordsMap["else"] = Tok.ELSE; + //keywordsMap["("] = Tok.LEFT_BRACKET; + //keywordsMap[")"] = Tok.RIGHT_BRACKET; + } - public string FinishCurrentLine() + public string FinishCurrentLine() { return CurrentLineText + inputReader.ReadLine(); } @@ -279,7 +289,19 @@ namespace SimpleLexer NextCh(); LexKind = Tok.EQ; } - else if (currentCh == '{') + // РґРѕР±. DescentParser + else if (currentCh == '(') + { + NextCh(); + LexKind = Tok.LEFT_BRACKET; + } + else if (currentCh == ')') + { + NextCh(); + LexKind = Tok.RIGHT_BRACKET; + } + // ----- + else if (currentCh == '{') { while (currentCh != '}') { @@ -290,7 +312,7 @@ namespace SimpleLexer NextCh(); NextLexem(); } - else if (char.IsLetter(currentCh)) + else if (char.IsLetter(currentCh)) { while (char.IsLetterOrDigit(currentCh)) { diff --git a/Module4/SimpleLangParser/SimpleLangParser.cs b/Module4/SimpleLangParser/SimpleLangParser.cs index 246db0b..9cb9109 100644 --- a/Module4/SimpleLangParser/SimpleLangParser.cs +++ b/Module4/SimpleLangParser/SimpleLangParser.cs @@ -29,14 +29,12 @@ namespace SimpleLangParser public void Expr() { - if (l.LexKind == Tok.ID || l.LexKind == Tok.INUM) - { - l.NextLexem(); - } + if (l.LexKind == Tok.ID || l.LexKind == Tok.INUM || l.LexKind == Tok.PLUS || l.LexKind == Tok.MINUS || l.LexKind == Tok.MULT || l.LexKind == Tok.DIVISION || l.LexKind == Tok.RIGHT_BRACKET || l.LexKind == Tok.LEFT_BRACKET) + while (l.LexKind == Tok.ID || l.LexKind == Tok.INUM || l.LexKind == Tok.PLUS || l.LexKind == Tok.MINUS || l.LexKind == Tok.MULT || l.LexKind == Tok.DIVISION || l.LexKind == Tok.RIGHT_BRACKET || l.LexKind == Tok.LEFT_BRACKET) + l.NextLexem(); + //if (l.LexKind != Tok.ID && l.LexKind != Tok.INUM && l.LexKind != Tok.PLUS && l.LexKind != Tok.MINUS && l.LexKind != Tok.MULT && l.LexKind != Tok.DIVISION && l.LexKind != Tok.RIGHT_BRACKET && l.LexKind != Tok.LEFT_BRACKET) else - { SyntaxError("expression expected"); - } } public void Assign() @@ -46,7 +44,8 @@ namespace SimpleLangParser { l.NextLexem(); } - else { + else + { SyntaxError(":= expected"); } Expr(); @@ -81,6 +80,21 @@ namespace SimpleLangParser Assign(); break; } + case Tok.WHILE: + { + While(); + break; + } + case Tok.FOR: + { + For(); + break; + } + case Tok.IF: + { + If(); + break; + } default: { SyntaxError("Operator expected"); @@ -111,6 +125,70 @@ namespace SimpleLangParser Statement(); } + // РґРѕР±. WHILE + public void While() + { + l.NextLexem(); + Expr(); + + if (l.LexKind == Tok.DO) + { + l.NextLexem(); + Statement(); + } + else SyntaxError("DO expected"); + } + + // РґРѕР±. FOR + public void For() + { + l.NextLexem(); + if (l.LexKind == Tok.ID) + { + l.NextLexem(); + if (l.LexKind == Tok.ASSIGN) + { + l.NextLexem(); + Expr(); + if (l.LexKind == Tok.TO) + { + l.NextLexem(); + Expr(); + if (l.LexKind == Tok.DO) + { + l.NextLexem(); + Statement(); + } + else SyntaxError("DO expected"); + } + else SyntaxError("TO expected"); + } + else SyntaxError("ASSIGN expected"); + } + else SyntaxError("ID expected"); + } + + // РґРѕР±. IF + public void If() + { + l.NextLexem(); + Expr(); + + if (l.LexKind == Tok.THEN) + { + l.NextLexem(); + Statement(); + + if (l.LexKind == Tok.ELSE) + { + l.NextLexem(); + Statement(); + } + } + else SyntaxError("THEN expected"); + } + + public void SyntaxError(string message) { var errorMessage = "Syntax error in line " + l.LexRow.ToString() + ":\n"; diff --git a/TestDescentParser/Tests.cs b/TestDescentParser/Tests.cs index 2d3de16..e523ebd 100644 --- a/TestDescentParser/Tests.cs +++ b/TestDescentParser/Tests.cs @@ -26,7 +26,7 @@ namespace TestDescentParser } [Test] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public void TestWhile() { Assert.IsTrue(Parse(@"begin while 5 do a:=2 end")); @@ -75,7 +75,7 @@ namespace TestDescentParser } [Test] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public void TestIf() { Assert.IsTrue(Parse(@"begin @@ -101,7 +101,7 @@ namespace TestDescentParser } [Test] - [Ignore("This test is disabled")] + //[Ignore("This test is disabled")] public void TestExpr() { Assert.IsTrue(Parse(@"begin -- GitLab