Reputation: 809
I am currently designing a GUI that allows the user to define some logic. I don't want it getting too complex, so I am limiting it to a single set of brackets. So, the idea is to check that between the opening and closing brackets there are not any other opening brackets.
eg. IF ( a + b OR **(** b+ c)
would alert with error.
So I decided with the route of:
Here's the code. I think its pretty horrific and I am sure there must be a better way to do this. Some kind of IndexOf maybe.
<select rel="OpenBracket" id="open1">
<option value=""></option>
<option value="(">(</option>
</select>
Some True/Fale here
<select rel="CloseBracket" id="close1">
<option value=""></option>
<option value=")">)</option>
</select>
AND
<br />
<select rel="OpenBracket" id="open2">
<option value=""></option>
<option value="(">(</option>
</select>
Some True/Fale here
<select rel="CloseBracket" id="close2">
<option value=""></option>
<option value=")">)</option>
</select>
<button onclick="javascript:TestingRules();">Check</button>
function GetOpenBrackets() {
var openBracketArray = [];
jQuery('[rel="OpenBracket"]').each(function() {
if (jQuery(this).val() == "(") {
openBracketArray.push(jQuery(this).attr('id'));
} else {
openBracketArray.push(jQuery(this).val());
}
});
return openBracketArray;
}
function GetCloseBrackets() {
var closeBracketArray = [];
jQuery('[rel="CloseBracket"]').each(function() {
if (jQuery(this).val() == "(") {
closeBracketArray.push(jQuery(this).attr('id'));
} else {
closeBracketArray.push(jQuery(this).val());
}
});
return closeBracketArray;
}
function TestingRules() {
var openBrackets = GetOpenBrackets();
var closeBrackets = GetCloseBrackets();
var closeBracketIndex;
var openBracketIndex;
for (openBracketIndex in openBrackets) {
if (openBrackets[openBracketIndex] !== "") {
var foundCloseBracketIndex = -1;
for (closeBracketIndex in closeBrackets) {
if (openBracketIndex <= closeBracketIndex) {
if (closeBrackets[closeBracketIndex] !== "") {
foundCloseBracketIndex = closeBracketIndex;
break;
}
}
}
if (foundCloseBracketIndex > -1) {
var openBracketCheck;
for (openBracketCheck in openBrackets) {
if (openBracketIndex < openBracketCheck && closeBracketIndex >= openBracketCheck) {
if (openBrackets[openBracketCheck] !== "") {
alert('error');
}
}
}
}
}
}
// for testing:
// console.log(OpenBracketArray.length);
}
Upvotes: 0
Views: 124
Reputation: 14363
I believe that you can simply try implementing
a conceptual stack(top=0)
and
push(top=top+1)
whenever you see (
and
[(optional) if top>X , where X is the depth allowed, error out invalid expression length]
pop(top=top-1)
whenever you see )
.
When the expression completes and you still have anything left on stack(top>0)
you can infer that expression is not balanced.
Upvotes: 1
Reputation: 512
Instead of trying to find a pair of brackets, why not just search for two open brackets in a row? When you find the first open bracket, one of two things are possible: Either the content that follows is enclosed with a close bracket (so the user didn't nest them), or you find another open bracket. If you find another open bracket, the user is trying to nest statements. This saves you from having to go back and searching the same space again.
Upvotes: 0
Reputation: 160181
Why not just keep a counter, or count paren stack depth? If you inc for open parens, dec for closing, and the counter goes above 1, you have an error. (If I've understood your requirements correctly.)
Upvotes: 1