I want to convert
((a v b) ^ c) v e -> c
into
[
'implication'
[
'or',
[
'and',
[
'or',
'a',
'b'
],
'c'
],
'e'
],
'c'
]
How is this possible?
I guess I should start by defining some operators (and operator type corresponds to the operator symbol)
var operators = {
'v' : 'or',
'^' : 'and',
'->': 'implication'
};
and then traverse the string
// string
var infix = '((a v b) ^ c) v e -> c';
// remove spaces, so infix[i]!=" "
infix = infix.replace(/\s+/g, '');
// traverse through string
for (let i=0; i<infix.length; i++) {
// get token
var token = infix[i];
// if token is an operator
if (operators.indexOf(token) !== -1) {
(...)
}
// if token is parenthesis
else if (token === '(') {
(...)
}
(...)
}
but I don't know how to get further than this.
I guess the tree structured array will be done using something like
expression = [operators[token], expression];
so the expression is preserved but on a nested level in the array.