From e40d27c44d56125e4d12420ec5a15ca98d9bcf45 Mon Sep 17 00:00:00 2001 From: NikitaBogoslovskiy <nbog@sfedu.ru> Date: Sun, 9 Oct 2022 20:58:12 +0300 Subject: [PATCH] do module 4 --- Module2/SimpleLangLexer/SimpleLexer.cs | 17 +++ Module4/SimpleLangParser/SimpleLangParser.cs | 121 +++++++++++++++++++ TestDescentParser/Tests.cs | 6 +- 3 files changed, 141 insertions(+), 3 deletions(-) diff --git a/Module2/SimpleLangLexer/SimpleLexer.cs b/Module2/SimpleLangLexer/SimpleLexer.cs index 985fa98..3940b79 100644 --- a/Module2/SimpleLangLexer/SimpleLexer.cs +++ b/Module2/SimpleLangLexer/SimpleLexer.cs @@ -105,6 +105,13 @@ namespace SimpleLexer keywordsMap["or"] = Tok.OR; keywordsMap["and"] = Tok.AND; keywordsMap["not"] = Tok.NOT; + keywordsMap["while"] = Tok.WHILE; + keywordsMap["for"] = Tok.FOR; + keywordsMap["do"] = Tok.DO; + keywordsMap["to"] = Tok.TO; + keywordsMap["if"] = Tok.IF; + keywordsMap["then"] = Tok.THEN; + keywordsMap["else"] = Tok.ELSE; } public string FinishCurrentLine() @@ -168,6 +175,16 @@ namespace SimpleLexer LexKind = Tok.SEMICOLON; NextCh(); } + else if (currentCh == '(') + { + LexKind = Tok.LEFT_BRACKET; + NextCh(); + } + else if (currentCh == ')') + { + LexKind = Tok.RIGHT_BRACKET; + NextCh(); + } else if (currentCh == ':') { NextCh(); diff --git a/Module4/SimpleLangParser/SimpleLangParser.cs b/Module4/SimpleLangParser/SimpleLangParser.cs index 246db0b..0eac18a 100644 --- a/Module4/SimpleLangParser/SimpleLangParser.cs +++ b/Module4/SimpleLangParser/SimpleLangParser.cs @@ -27,8 +27,52 @@ namespace SimpleLangParser Block(); } + public void M() + { + if (l.LexKind == Tok.ID || l.LexKind == Tok.INUM) + { + l.NextLexem(); + } + else if (l.LexKind == Tok.LEFT_BRACKET) + { + l.NextLexem(); + Expr(); + if (l.LexKind == Tok.RIGHT_BRACKET) + l.NextLexem(); + else + SyntaxError("right bracket expected"); + } + } + + public void B() + { + if (l.LexKind == Tok.MULT || l.LexKind == Tok.DIVISION) + { + l.NextLexem(); + M(); + B(); + } + } + + public void T() + { + M(); + B(); + } + + public void A() + { + if (l.LexKind == Tok.PLUS || l.LexKind == Tok.MINUS) + { + l.NextLexem(); + T(); + A(); + } + } + public void Expr() { + /* if (l.LexKind == Tok.ID || l.LexKind == Tok.INUM) { l.NextLexem(); @@ -37,6 +81,9 @@ namespace SimpleLangParser { SyntaxError("expression expected"); } + */ + T(); + A(); } public void Assign() @@ -81,6 +128,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 +173,65 @@ namespace SimpleLangParser Statement(); } + public void While() + { + l.NextLexem(); // РїСЂРѕРїСѓСЃРє while + Expr(); + if (l.LexKind == Tok.DO) + { + l.NextLexem(); + } + else + { + SyntaxError("do expected"); + } + Statement(); + } + + public void For() + { + l.NextLexem(); // РїСЂРѕРїСѓСЃРє for + Assign(); + if (l.LexKind == Tok.TO) + { + l.NextLexem(); + } + else + { + SyntaxError("to expected"); + } + Expr(); + if (l.LexKind == Tok.DO) + { + l.NextLexem(); + } + else + { + SyntaxError("do expected"); + } + Statement(); + } + + public void If() + { + l.NextLexem(); // РїСЂРѕРїСѓСЃРє if + Expr(); + if (l.LexKind == Tok.THEN) + { + l.NextLexem(); + } + else + { + SyntaxError("then expected"); + } + Statement(); + if (l.LexKind == Tok.ELSE) + { + l.NextLexem(); + Statement(); + } + } + 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