Ok.....So I've been toying around with some code to be able to parse multiple combinations of commands and here is my first crack at it..... I would be interesting in anyones thoughts...

#!/usr/bin/perl use warnings; my %action_list = (); my %rules = ( Cmd_SetVariable => qw' ^\s*(\w+)+\s*\=\s*(\w+ +)\s*$ ', Cmd_IfThen => qw' ^\s*if\s*\(\s*(\w+)\s* +(>=?|<=?|==)\s*(\w+)\s*\)\s*\{\s*([\w\s\(\)=]+)\;?\s*\}$ ', Cmd_Chunk => qw' ^\s*\w+\s*$ ', Cmd_MultiChunk => qw' ^\s*(.*?\;){0,}\s*$ + ', ); sub AUTOLOAD { my $func = shift; print "Oops, handler not defined : $func\n"; } sub Cmd_SetVariable { print "Its a Set Variable Chunk\n"; } sub Cmd_IfThen { my ($this, $chunk) = @_; print "Its a If then Chunk\n"; my ($arg1, $op, $arg2, $do) = $chunk =~ /$rules{Cmd_IfThen}/; print "arg1: $arg1 op: $op arg2: $arg2 do: $do\n"; CheckChunk($do); } sub Cmd_MultiChunk { my ($this, $chunk) = @_; print "Its a Multi Chunk\n"; my @chunks = split ';',$chunk; foreach $c ( @chunks ) { CheckChunk($c); } } sub ViewList { print "\nActionList\n"; foreach my $key ( sort keys %action_list ) { my @commands = split ';', $action_list{$key}; print "$key:"; foreach my $v ( @commands ) { print "\t$v\n"; } } print "\n\n"; } sub AddAction { my $arg = shift; my ($action_name, $action) = split ':', $arg; $action_list{$action_name} = $action; } sub CheckChunk { my $command = shift; my $translated; print "\nProcessing : $command\n"; $translated = 0; foreach my $rulename ( keys %rules ) { if ( $command =~ /$rules{$rulename}/ ) { print "Gotcha :$rules{$rulename}\n"; &{$rulename}($rulename, $command); $translated = 1; } } if ( $translated != 1 ) { print "Syntax Error : $command\n"; } } sub CheckActions { foreach my $name ( sort keys %action_list ) { CheckChunk($action_list{$name}); } } AddAction("test: a=4;b =5 ; c = 9;if (b > a) { c = 100 };"); #AddAction("Camel: Camel = 5"); #AddAction("hippo: ilovetoswim"); #AddAction("ifthen1: if ( ABC > 500 ){XYZ = 10}"); #AddAction("ifthen2: if ( ABC < 500 ){sdfsdf}"); #AddAction("ifthen3: if ( ABC = 500 ){sdfsdf}"); #AddAction("ifthen4: if ( ABC == 500 ){sdfsdf}"); #AddAction("ifthen5: if ( ABC << 500 ){sdfsdf}"); #supposed to break #AddAction("ifthen6: if ( ABC >> 500 ){sdfsdf}"); #supposed to break #AddAction("ifthen7: if ( ABC <= 500 ){sdfsdf}"); #AddAction("ifthen8: if ( ABC >= 500 ){sdfsdf}"); ViewList(); CheckActions();
OUTPUT:
ActionList hippo: a=4 b =5 c = 9 if (b > a) { c = 100 } Processing : a=4;b =5 ; c = 9;if (b > a) { c = 100 }; Gotcha :^\s*(.*?\;){0,}\s*$ Its a Multi Chunk Processing : a=4 Gotcha :^\s*(\w+)+\s*\=\s*(\w+)\s*$ Its a Set Variable Chunk Processing : b =5 Gotcha :^\s*(\w+)+\s*\=\s*(\w+)\s*$ Its a Set Variable Chunk Processing : c = 9 Gotcha :^\s*(\w+)+\s*\=\s*(\w+)\s*$ Its a Set Variable Chunk Processing : if (b > a) { c = 100 } Gotcha :^\s*if\s*\(\s*(\w+)\s*(>=?|<=?|==)\s*(\w+)\s*\)\s*\{\s*([\w\s\ +(\)=]+)\;?\s*\}$ Its a If then Chunk arg1: b op: > arg2: a do: c = 100 Processing : c = 100 Gotcha :^\s*(\w+)+\s*\=\s*(\w+)\s*$ Its a Set Variable Chunk
Regards Paul

In reply to Re^4:command input processing by thekestrel
in thread command input processing by thekestrel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.