Jangale V-S SPEL-TIT wrote:

> 
>>Hi Experts,
>>
>>I have a small problem. Maybe it is silly one .
>>
>>I am writing a program which is reading an ini file and doing arithmatic 
>>calculations
>>specified in the ini file. The ini file looks like this
>>
>>-----------------------------------------------------------
>>A = (B + C) / D
>>RESULT = DATAFROMFILE - DATACAL
>>
>>------------------------------------------------------------
>>
>>The equations in the ini file are user defined and user can define any 
>>arithmatic equation in the file !
>>The values on right hand side are not known at the start of program and are 
>>known during the execution
>>from several query operations . The intention of the program is to evaluate 
>>the expressions in the ini file
>>and display the resultant values.
>>
>>One way of doing this could have been  
>>
>>Change ini file to perl program like
>>
>>$A = ($B + $C)/$D  
>>
>>then analyse the variables on right hand side , get their values and then 
>>again read the ini file in the program
>>with do function like
>>
>>do "C:\\temp\\test,ini";
>>
>>With this there is a danger of user inserting any execuatable perl command in 
>>the ini file which can do any unintentional
>>operation like
>>
>>system("delete c:\\*.*");
>>
>>
>>Another possibility is to keep the ini file as it is (specified initially 
>>without $ sign).
>>Read the ini file, add prefix of $ sign before variable name , write the 
>>modified expressions
>>in another ini file and then read this ini file with do statement
>>
>>do "C:\\temp\\modtest.ini";
>>
>>Somehow I am not comfortable with both alternatives. What I need is 
>>
>>1) Read ini file 
>>
>>      A = (B + C) / D
>>      RESULT = DATAFROMFILE - DATACAL
>>
>>   Store the arithmatic expressions on right hand side in perl variables 
>> after adding prefix of $, say   $LEFT{A} = ($B + $C) / $D  etc

You could create a hash for the vrbls and associate the name with a hash element
(undefined until you determine the value of the vrbl).

my %vrbls;

$vrbls{A} = undef;
$vrbls{B} = undef;
$vrbls{C} = undef;
$vrbls{D} = undef;
$vrbls{RESULT} = undef;
$vrbls{DATAFROMFILE} = undef;
$vrbls{DATACAL} = undef;

Now all you need to do is evaluate the expressions.  Of course you'll need
to split each line and decide if you have a vrbl or an operation to perform.
Another hash would help with all the operators in it and a coderef to the
apporpriate subroutine.  There may be a Perl lex/yacc type module to help you
write an LALR grammar in Perl (or possibly write your own converter from C
YACC code to Perl).  Try searching CPAN for a LALR parser in Perl.
Parse::Yapp, Parse::YALALR or Parse::RPN may be useful.

>>2) Evalute the variables on right hand side ..

That's where the grammar would come in - you could verify the grammar and
actually execute it as you parse the file.

>>3) After values of all variables on right hand side are known, evaluate 
>>complete expression with a function (this function will actually calculate 
>>the 
>>    the resultant value of Varible on right hand side eg.
>>
>>    $A  = Eval { $LEFT{A}  }          # Just for example 

You don't need that step - step 2 would have done that already.
You'll have to decide if you want to use some sort of RPN or stack
based grammar or maybe just a left to right line parse with the
ability to apply operator precedence.  Parse::Yapp looks like a good
place to start.

-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Admin mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to