4

I have a batch file which uses an external program called ASET.exe (http://www.pement.org/sed/bat_env.htm) to set values to variables. This was used as it has some advanced capabilities than the normal SET command. ASET can change the given string to different formats and then assign it to it. It has functions like UPPER(), Fread(), Lower(), left().

c:\test> aset var := left('asdf',2) 

the above statement assigns "as" to Variable "var". But the problem is it will work only in win 98 or before machines. It will not work in win NT or in XP or 2008. So, I decided to write a small program like ASET in c# which supports some small number of functions. I am reading those commands as Command line arguments and parsing them, and then using a for loop and switch case, I am able to achieve the desired result to some extent. But I am not able to parse properly for all types of commands.

For Eg: I cannot parse this properly:

aset var := left(upper(fsdsf),2). 

My question is that What is the correct procedure for parsing commandline arguments? How to distinguish function names, various switches, operators ? Using "switch" is the only solution to call a function based on the input string ?

This is my Grammar file

grammar sra;

options {

  language = Java;
  output = AST;
}

start returns [String res]: expression 
{
$res=$expression.res;
System.out.println("value equals at start: "+$expression.text+$res);
} ;

expression returns [String res]
: Identifier Assignop statement
{$res=$statement.res;
System.out.println("value equals at ecpression: "+$statement.text+" "+$res);}
;

statement returns [String res]
  : function {$res=$function.res;
  System.out.println("value equals at statement: "+$function.text+" "+$res);}

  //|function Plus function
  //|function Plus Identifier
  //|Identifier
  //|Identifier Plus Identifier
  ;


   function returns [String res]
  : e=upper {$res=$e.res;
  System.out.println("value equals at function: "+$e.text+" "+$res);}  
  ;
upper returns [String res]
  : e=Upper '(' b=arguments   ')'
  {
  System.out.println("argum before conver "+$b.text);
  $res= ($b.text).toUpperCase();
  System.out.println("value equals at upper: "+$e.text+" "+"Arguments="+$b.text+" "+$res); }

  ;
arguments  returns [String res]     
                :   e1=Identifier   {$res=$e1.text;}
                |  function  {$res=$function.res;} 
                ;


Upper : 'upper';
Lower : 'lower';

Identifier  :  ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9')*;
Assignop :':=' ;

Lparen  : '(';
Rparen  : ')';
Plus  : '+';
WS  : (' '|'\t'|'\r'|'\n')+{$channel = HIDDEN;};

Now how to achieve functionality like var := upper(upper(fsf)) ?? I am getting output as UPPER(FSF)..

4

2 回答 2

2

您可以根据需要解析命令行参数。您建议的用法不是命令行参数的典型用法,因此对于您想要做的事情没有一个“正确”的过程。

您似乎有自己的语法(我猜可能基于帕斯卡),因此您需要为您的语法找到或编写解析器和评估器。一方面是像ANTLR这样的东西,或者你可以用sprache去某个地方。

也可能值得看看http://commandline.codeplex.com/关于解析和处理命令行参数的一些想法(尽管我怀疑这是否足以满足您的要求)。

于 2012-05-28T16:05:25.097 回答
0

Windows 98 已停止支持 6 年,ASET 也是如此。

你可能会看到“Take Command”(4NT)是否会做你想做的事,但否则我认为带有 FreeDOS 的 VirtualBox 可能是你最好的选择。

于 2012-05-29T09:04:00.050 回答