Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using NUnit.Framework;
using SimpleLexer;
using SimpleLangParser;
using System.IO;
namespace TestDescentParser
{
[TestFixture]
public class DescentParserTests
{
private bool Parse(string text)
{
TextReader inputReader = new StringReader(text);
Lexer l = new Lexer(inputReader);
Parser p = new Parser(l);
p.Progr();
if (l.LexKind == Tok.EOF)
{
return true;
}
else
{
return false;
}
}
[Test]
public void TestWhile()
{
Assert.IsTrue(Parse(@"begin while 5 do a:=2 end"));
Assert.IsTrue(Parse(@"begin
while 5 do
begin
a:=2
end
end"));
Assert.IsTrue(Parse(@"begin
while 5 do
begin
while 6 do
a:=2;
while 7 do
begin
a:=3;
c:=4
end
end
end"));
}
[Test]
public void TestFor()
{
Assert.IsTrue(Parse(@"begin
for a:=1 to 5 do
begin
b:=b+1
end
end"));
Assert.IsTrue(Parse(@"begin
for a:=1 to 5 do
begin
for i:=1 to 6 do
c:=c-1;
b:=b+1
end
end"));
}
[Test]
public void TestIf()
{
Assert.IsTrue(Parse(@"begin
if 2 then
a:=2
else
b:=2;
if 3 then
if c then
c:=4
else
m:=1
else
v:=8;
if 4 then
if 4 then
if 6 then
m:=0
end"));
}
[Test]
public void TestExpr()
{
Assert.IsTrue(Parse(@"begin
if 2+2*(c-d/3) then
begin
a:=2;
while 2-3+f do c:=c*2
end
else
b:=2-3*(c-d/f*3);
for i:=2-3*(s-d) to (c-3) do
a:=(a-(3-3));
if 3 then
if (c-3) then
c:=4+2
else
m:=1
else
v:=(8+2)
end"));
}
}
}