Skip to content
Snippets Groups Projects
Commit 930bd7b1 authored by lusparon332's avatar lusparon332
Browse files

lab4 done

parent 4d38e7d4
Branches
No related merge requests found
Pipeline #5617 passed with warnings with stages
in 6 minutes and 51 seconds
......@@ -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))
{
......
......@@ -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";
......
......@@ -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
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment