-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.cpp
More file actions
153 lines (140 loc) · 4.59 KB
/
Parser.cpp
File metadata and controls
153 lines (140 loc) · 4.59 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "Parser.hpp"
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <stack>
using namespace std;
int letter_count(Lexer *l){
int temp = l->unique_letters.size();
l->unique_letters.clear();
return temp;
//n=temp;
}
void Parser :: to_tokenstring(Token *t)
{
this->token_string.push_back(*t);
}
void Parser :: start(string sentence)
{
const char *content = &sentence[0];
size_t cont_len = strlen(content);
Lexer l = lexer_new(content, cont_len);
Token t;
t = lexer_next(&l); // start push
to_tokenstring(&t);
// Three valid starting characters of a well formed formula
if(t.kind == TOKEN_END){cout << "Invalid: WFF is empty " << endl;well_formed = false;}
else if(t.kind == TOKEN_LEFTPARANTHESES){follow(&l);}
else if (t.kind == TOKEN_LETTER){follow(&l);}
else if (t.kind == TOKEN_NEGATION){follow(&l);}
else{cout << "Invalid: WFF cannot start with '" << t.text[0] << "'" << endl;well_formed=false;}
};
void Parser :: follow(Lexer *l)
{
Token t;
t=lexer_next(l);
while(t.kind != TOKEN_END)
{
this->to_tokenstring(&t);
if(t.kind == TOKEN_EXCEPTION){well_formed = false;}
else if(t.kind == TOKEN_LETTER)
{
//Problem where input "pt" for example will be allowed.
t=lexer_next(l);
switch(t.kind)
{
case TOKEN_END:
break;
case TOKEN_ARROW:
continue;
case TOKEN_BIOCONDITONAL:
continue;
case TOKEN_OR:
continue;
case TOKEN_AND:
continue;
case TOKEN_RIGHTPARANTHESES:
continue;
default:
{
cout << "Invalid: "<< token_kinderizer(t.kind)<< " cannot be here '" <<t.text << "'" << endl;
well_formed = false;
}
}
}
else if(t.kind==TOKEN_NEGATION)
{
t=lexer_next(l);
switch(t.kind)
{
case TOKEN_LEFTPARANTHESES:
continue;
case TOKEN_LETTER:
continue;
case TOKEN_NEGATION:
continue;
default:
cout << "Invalid: "<< token_kinderizer(t.kind)<< " cannot be here '" <<t.text << "'" << endl;
well_formed = false;
}
}
else if(t.kind==TOKEN_LEFTPARANTHESES)
{
t=lexer_next(l);
switch(t.kind)
{
case TOKEN_NEGATION:
continue;
case TOKEN_LETTER:
continue;
case TOKEN_LEFTPARANTHESES:
continue;
default:
cout << "Invalid: "<< token_kinderizer(t.kind)<< " cannot be here '" <<t.text << "'" << endl;
well_formed = false;
}
}
else if(t.kind==TOKEN_RIGHTPARANTHESES)
{
t=lexer_next(l);
switch(t.kind)
{
case TOKEN_END:
break;
case TOKEN_ARROW:
continue;
case TOKEN_BIOCONDITONAL:
continue;
case TOKEN_OR:
continue;
case TOKEN_AND:
continue;
case TOKEN_RIGHTPARANTHESES:
continue;
default:
cout << "Invalid: "<< token_kinderizer(t.kind)<< " cannot be here '" <<t.text << "'" << endl;
well_formed = false;
}
}
else if((t.kind & (TOKEN_AND | TOKEN_OR | TOKEN_ARROW | TOKEN_BIOCONDITONAL))>0)
{
t=lexer_next(l);
switch(t.kind)
{
case TOKEN_NEGATION:
continue;
case TOKEN_LEFTPARANTHESES:
continue;
case TOKEN_LETTER:
continue;
default:
cout << "Invalid: "<< token_kinderizer(t.kind)<< " cannot be here '" <<t.text << "'" << endl;
well_formed = false;
}
}
else{cout << "ERROR IN PARSER.CPP" << endl;well_formed=false;} // Even the most invalid of formula shouldnt get here.
}
if(l->parantheses != 0) {cout << "UNEVEN PARANTHESES COUNT" << endl; well_formed = false;}
l_count = letter_count(l);
}