Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# evaluating brackets

Posted on 2001-07-25
Medium Priority
495 Views
Does anyone have code or an algorithm to evaluate brackets for macthematical expressions. I can get the conditions, but am heaving trouble working out there relationship to each other

E.g,

IF (((X=Y) AND (A=B)) OR (C=D)) THEN blah blah blah

Can't quite get my head around it! I've gone "bracket blind" i've been staring at it for so long.

I've marked the question as hard coz I can't find anyone who has done this. I guess if you've done it before it'll pretty simple.

Cheers,

Mark.
0
Question by:sparkido
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 27

Expert Comment

ID: 6316807
0

Author Comment

ID: 6316985
Thanks for thin link, but the code seems to be geared towards evaluating mathematical equations. I guess I can now refine my question ...

I don't actually want to evaluate the expression, as the conditions themselves aren't "real" mathematical conditions, and themselves need to be resolved. I really want to extract the conditions from the brackets, and determine the relationship between them. I'm more interested in the structure of the expression, rather than evaluating it automatically.

I'll keep looking though...
0

LVL 9

Expert Comment

ID: 6317109
Hi sparkido,

I'm not sure, but may be my unit will be usefull for you.

-----
Igor.

unit O2_parse;
//-----------------------------------------------------------------------------------------------------------------------------
//
//   Copyright (c) Igor Tugay , 2001
//
//-----------------------------------------------------------------------------------------------------------------------------
//
//   Unit has class that allow to parse recursive text construction
//   Sample:
//          struc=(
//             column=(width=100, color=(R=255, G=255, B=240), caption = "(c)"),
//             column=(width=120, color=(255, 255, 240), caption = "(c)")
//          )
//
//   Result:
//   O2Expr.Value = "struct"
//       List[0].Value = column
//          List[0].Value = width
//          List[0].Data = 100
//          List[1].Value = color
//              List[0].Value = R
//              List[0].Data = 255
//              ...
//          List[2].Value = "caption"
//          List[2].Data = "(C)"
//       List[1].Value = column
//       .....
//
//-----------------------------------------------------------------------------------------------------------------------------
interface

uses
windows, classes, sysutils, contnrs;

type
TO2Expr = class(TObjectList)
private
FValue: String;
FData: String;
FList: TO2Expr; // if FList is nil then FData has Data
// otherwise FList has list of enclosed data
function GetItem(Index: Integer): TO2Expr;
public
constructor Create;
destructor Destroy; override;

// I should be 1 for top level expression
//  ...Parse('some data = (v=10)', 1)
function Parse(const S: String; I: Integer): Integer;
function GetByValue(const S: String): TO2Expr;

// X - number of spaces to show current level indent
procedure Show(L: TStrings; X: Integer);

property Items[Index: Integer]: TO2Expr read GetItem; default;
end;

implementation

//-----------------------------------------------------------------------------------------------------------------------------

function GetTextConst(const S: String; var I: Integer): String;
var L: Integer;
begin
Result := '';
inc(I);
L := Length(S);
while (I <= L) and (S[I] <> '"') do
begin
Result := Result + S[I];
inc(I);
end;
Result := '"'+Result + '"';
inc(I);
end;

//-----------------------------------------------------------------------------------------------------------------------------

procedure SkipCtlChar(const S:String; var I: Integer);
var L: Integer;
begin
L := Length(S);
while (I<=L) and (S[I] <= ' ') do inc(I);
end;

//----------------- TO2Expr ---------------------------------------------------------------------------------------------------

constructor TO2Expr.Create;
begin
inherited;
end;

//-----------------------------------------------------------------------------------------------------------------------------

destructor TO2Expr.Destroy;
begin
List.Free;
inherited;
end;

//-----------------------------------------------------------------------------------------------------------------------------

function TO2Expr.GetItem(Index: Integer): TO2Expr;
begin
Result := TO2Expr(Inherited Items[Index]);
end;

//-----------------------------------------------------------------------------------------------------------------------------

function TO2Expr.GetByValue(const S: String): TO2Expr;
var
I: Integer;
begin
Result := nil;
for I := 0 to Count-1 do
if CompareText(Trim(S), Trim(Items[I].Value)) = 0 then
begin
Result := Items[I];
Break;
end;
end;

//-----------------------------------------------------------------------------------------------------------------------------

function TO2Expr.Parse(const S: String; I: Integer): Integer;
var
L: Integer;
T: TO2Expr;
begin
L := Length(S);
while (I <= L) and (S[I] <> ')') do
begin
T := TO2Expr.Create;

SkipCtlChar(S, I);
while (I<=L) and (S[I] <> '=') and (S[I] <> ',') and (S[I] <> ')') do
begin
T.FValue := T.FValue+S[I];
inc(I);
end;
T.FValue := Trim(T.FValue);

if (I <= L) and (S[I] <> ',') and (S[I] <> ')') then
begin
inc(I);
SkipCtlChar(S, I);
if (I <= L) and (S[I] = '(') then
begin
inc(I);
T.FList := TO2Expr.Create;
I := T.List.Parse(S, I);
end else
while (I<=L) and (S[I] <> ',') and (S[I] <> ')') do
begin
if S[I] = '"' then
T.FData := T.FData+GetTextConst(S, I) else
begin
T.FData := T.FData+S[I];
inc(I);
end;
end;

SkipCtlChar(S, I);
end;

while (I<=L) and (S[I] = ',') do Inc(I);

T.FData := Trim(T.FData);
end;
if (I<=L) and (S[I] = ')') then inc(I);

Result := I;
end;

//-----------------------------------------------------------------------------------------------------------------------------

procedure TO2Expr.Show(L: TStrings; X: Integer);
var
S: String;
I: Integer;
T: TO2Expr;
begin
S := '';
for I := 1 to X-1 do S:=S+' ';
for I := 0 to Count-1 do
begin
T := TO2Expr(Items[I]);
if T.List = nil then
else begin
T.List.Show(L, X+3);
end;
end;
end;

end.
0

LVL 2

Accepted Solution

FrodoBeggins earned 800 total points
ID: 6317821
Basicaly I would put the expresion in a tree. The tree gives you the posibility of using parent-chld(s) relations just as the brackets. The result is on the root of the tree, and you should start evaluating it with the deepest leaves.
Of course that could be done brutely, but with a structure like a tree is so... nice ;-). Afterwards you can combine expresions, evaluate them partialy and so on. Maybe I'll try to write one such parser, if noone else gives you correct answer.

Rgds,
Frodo
0

Author Comment

ID: 6326063
Cheers for that. Took me a while to work this out, but the theory runs that :

Call a function create a new treenode.
Get all characters from the string until you hit an open bracket.
Now recusively call the function and create a new treenode and continue processing.

When you get a close bracket you add the contents to the node.
If your brackets match up, this will eventually unwind itself back to root.
I think it's important to point out that evaluating brackets should be a two step process. Gather the data, then process it. Probably not wise (if not impossible) to do both at the same time.

You start at the deepest node and evaluate each node working back towards root. Evaluating all nodes at each level, and a parent node combines the results of it's children. This means that when you get to root, the answer of the condition in root is the answer for the entire bracket.

Thanks very much for your help.

Mark.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
###### Suggested Courses
Course of the Month9 days, 3 hours left to enroll