-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
79 lines (67 loc) · 2.2 KB
/
main.cpp
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
#include "LRParser.hpp"
#include "Grammar.hpp"
#include "Production.hpp"
#include "Symbol.hpp"
#include "LexText.hpp"
#include <iostream>
std::string const TEST_FILEPATH="testLex.txt";
int main(int const argc, char const * const * const argv)
{
Grammar g;
/*
g |= NT("goal") >>= NT("expr");
g |= NT("expr") >>= NT("term") + T("+") + NT("expr") | NT("term");
g |= NT("term") >>= NT("factor") + T("*") + NT("term") | NT("factor");
g |= NT("factor") >>= T("0") | T("1") | T("2") | T("3") | T("4") | T("5") | T("6") | T("7") | T("8") | T("9");
*/
g |= NT("goal") >>= NT("expr");
g |= NT("expr") >>= NT("term") + T("+") + NT("expr");
g |= NT("expr") >>= NT("term");
g |= NT("term") >>= NT("factor") + T("*") + NT("term");
g |= NT("term") >>= NT("factor");
g |= NT("factor") >>= T("id");
/*
g |= NT("E") >>= NT("E") + T("*") + NT("B");
g |= NT("E") >>= NT("E") + T("+") + NT("B");
g |= NT("E") >>= NT("B");
g |= NT("B") >>= T("0");
g |= NT("B") >>= T("1");
*/
//g |= NT("B") >>= T("hello") + T(" ") + T("world") | T("goodbye") + T(" ") + T("world");
/*
g |= NT("start_$") >>= NT("start");
g |= NT("start") >>= NT("start") + NT("expr");
g |= NT("start");
g |= NT("expr") >>= T("NR");
g |= NT("expr") >>= NT("expr") + T("+") + NT("expr");
*/
LRParser p(LRTable::Type::LR, 1, g);
LexText plt(TEST_FILEPATH);
#ifndef NDEBUG
std::cout << "====================----- Parsing -----====================" << std::endl;
#endif
if(p.parse(plt))
{
std::cout << "Parsing success." << std::endl;
}
#ifndef NDEBUG
std::cout << "==================================================" << std::endl;
#endif
#ifndef NDEBUG
std::cout << "====================----- " << TEST_FILEPATH << " -----====================" << std::endl;
LexText lt(TEST_FILEPATH);
Symbol s=END();
do
{
s = lt.pop();
std::cout << s.toString() << " ";
}
while(s != END());
std::cout << std::endl;
std::cout << "==================================================" << std::endl;
std::cout << "====================----- Grammar -----====================" << std::endl;
std::cout << g.toString();
std::cout << "==================================================" << std::endl;
#endif
return 0;
}