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

lab4 done

parent 4d38e7d4
No related merge requests found
Pipeline #5617 passed with warnings with stages
in 6 minutes and 51 seconds
...@@ -106,9 +106,19 @@ namespace SimpleLexer ...@@ -106,9 +106,19 @@ namespace SimpleLexer
keywordsMap["and"] = Tok.AND; keywordsMap["and"] = Tok.AND;
keywordsMap["or"] = Tok.OR; keywordsMap["or"] = Tok.OR;
keywordsMap["not"] = Tok.NOT; 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(); return CurrentLineText + inputReader.ReadLine();
} }
...@@ -279,7 +289,19 @@ namespace SimpleLexer ...@@ -279,7 +289,19 @@ namespace SimpleLexer
NextCh(); NextCh();
LexKind = Tok.EQ; 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 != '}') while (currentCh != '}')
{ {
...@@ -290,7 +312,7 @@ namespace SimpleLexer ...@@ -290,7 +312,7 @@ namespace SimpleLexer
NextCh(); NextCh();
NextLexem(); NextLexem();
} }
else if (char.IsLetter(currentCh)) else if (char.IsLetter(currentCh))
{ {
while (char.IsLetterOrDigit(currentCh)) while (char.IsLetterOrDigit(currentCh))
{ {
......
...@@ -29,14 +29,12 @@ namespace SimpleLangParser ...@@ -29,14 +29,12 @@ namespace SimpleLangParser
public void Expr() public void Expr()
{ {
if (l.LexKind == Tok.ID || l.LexKind == Tok.INUM) 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(); 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 else
{
SyntaxError("expression expected"); SyntaxError("expression expected");
}
} }
public void Assign() public void Assign()
...@@ -46,7 +44,8 @@ namespace SimpleLangParser ...@@ -46,7 +44,8 @@ namespace SimpleLangParser
{ {
l.NextLexem(); l.NextLexem();
} }
else { else
{
SyntaxError(":= expected"); SyntaxError(":= expected");
} }
Expr(); Expr();
...@@ -81,6 +80,21 @@ namespace SimpleLangParser ...@@ -81,6 +80,21 @@ namespace SimpleLangParser
Assign(); Assign();
break; break;
} }
case Tok.WHILE:
{
While();
break;
}
case Tok.FOR:
{
For();
break;
}
case Tok.IF:
{
If();
break;
}
default: default:
{ {
SyntaxError("Operator expected"); SyntaxError("Operator expected");
...@@ -111,6 +125,70 @@ namespace SimpleLangParser ...@@ -111,6 +125,70 @@ namespace SimpleLangParser
Statement(); 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) public void SyntaxError(string message)
{ {
var errorMessage = "Syntax error in line " + l.LexRow.ToString() + ":\n"; var errorMessage = "Syntax error in line " + l.LexRow.ToString() + ":\n";
......
...@@ -26,7 +26,7 @@ namespace TestDescentParser ...@@ -26,7 +26,7 @@ namespace TestDescentParser
} }
[Test] [Test]
[Ignore("This test is disabled")] //[Ignore("This test is disabled")]
public void TestWhile() public void TestWhile()
{ {
Assert.IsTrue(Parse(@"begin while 5 do a:=2 end")); Assert.IsTrue(Parse(@"begin while 5 do a:=2 end"));
...@@ -75,7 +75,7 @@ namespace TestDescentParser ...@@ -75,7 +75,7 @@ namespace TestDescentParser
} }
[Test] [Test]
[Ignore("This test is disabled")] //[Ignore("This test is disabled")]
public void TestIf() public void TestIf()
{ {
Assert.IsTrue(Parse(@"begin Assert.IsTrue(Parse(@"begin
...@@ -101,7 +101,7 @@ namespace TestDescentParser ...@@ -101,7 +101,7 @@ namespace TestDescentParser
} }
[Test] [Test]
[Ignore("This test is disabled")] //[Ignore("This test is disabled")]
public void TestExpr() public void TestExpr()
{ {
Assert.IsTrue(Parse(@"begin 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