Not sure whether I fully understand the question: You have many possible variables. The user can create an expression based on some of these variables, but he is free to use only a subset of them, and you do not know which ones you should declare in the expression parser. Right?
I think there is no simple way to prepare this.
You could start with no variable at all; put the expression into a try-except block and catch the exception for "Unknown identifier" and ask the user to define the missing variable and to specify a value for it. Then start a-new and repeat until no more error appears. But the problem is that there may be other issues in the expression than just unknown variables, and it is not easy to distinguish between them because the unit only exposes two exception types, EExprScanner and EExprParser.
program Project1;
uses
SysUtils, fpexprpars;
var
parser: TFpExpressionparser;
OK: Boolean;
res: TFpExpressionResult;
a: double;
expr: String;
begin
expr := 'a + 1.0';
parser := TFpExpressionParser.Create(nil);
try
repeat
try
OK := true;
parser.Expression := '';
parser.Expression := expr;
except
on E:EExprParser do
begin
WriteLn(E.Message);
Write('Please enter value for missing variable: ');
ReadLn(a);
parser.Identifiers.AddFloatVariable('a', a);
OK := false;
end;
end;
until OK;
res := parser.Evaluate;
WriteLn(expr, ' = ', ArgToFloat(res));
finally
parser.Free;
end;
ReadLn;
end.
I personally would go the safe way and declare all possible variables. A problem may be their initial values; a zero, for example, for variable "A" will be ok when "sqrt(A)" is to be calculated, but be fatal when the expression is "1/A" .