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)..