Hello,

Having studied the dragon book and others more than once, took about 10
minutes to write this parser that worked to my surprise the first time.
God bless LISP and LISP-like grammers.

Aziz,,,
###############################################
program
###############################################
#!/perl -w
use Data::Dumper;
use Switch;
my $str = <<EOF;
(("TEXT" "PLAIN" ("format" "flowed") NIL NIL "7BIT" 206 4 NIL NIL
NIL) ("TEXT" "PLAIN" ("name" "Display.txt" "format" "flowed") NIL NIL "8BIT"
16330 412 NIL ("attachment" ("filename" "Display.txt")) NIL) "mixed"
("boundary" "----=_NextPart_000_36cf_58cd_f54") NIL NIL)
EOF

my @stack;
my $arr=[];
push @stack,$arr;

while($str =~ /(\(|\)|"([^"]*)"|NIL)/g){
    switch($1){
        case '(' {
            my $new = [];
            push @{$stack[$#stack]}, $new;
            push @stack, $new;
        }
        case ')' {
            pop @stack;
        }
        case 'NIL' {
            push @{$stack[$#stack]}, [];
        }
        else {
            push @{$stack[$#stack]}, $2;
        }
    }
}

print Dumper($arr);

###############################################
output
###############################################
$VAR1 = [
          [
            [
              'TEXT',
              'PLAIN',
              [
                'format',
                'flowed'
              ],
              [],
              [],
              '7BIT',
              [],
              [],
              []
            ],
            [
              'TEXT',
              'PLAIN',
              [
                'name',
                'Display.txt',
                'format',
                'flowed'
              ],
              [],
              [],
              '8BIT',
              [],
              [
                'attachment',
                [
                  'filename',
                  'Display.txt'
                ]
              ],
              []
            ],
            'mixed',
            [
              'boundary',
              '----=_NextPart_000_36cf_58cd_f54'
            ],
            [],
            []
          ]
        ];



In article <61001A8E5BE9D0119D1E0008C728E0C3227F13@ntusa2>, "Chris Rogers"
<[EMAIL PROTECTED]> wrote:

> Maybe I'm crazy, but I would like to create an array of arrays from a
> single string.  Here's an example of a string: (("TEXT" "PLAIN"
> ("format" "flowed") NIL NIL "7BIT" 206 4 NIL NIL NIL)("TEXT" "PLAIN"
> ("name" "Display.txt" "format" "flowed") NIL NIL "8BIT" 16330 412 NIL
> ("attachment" ("filename" "Display.txt")) NIL) "mixed" ("boundary"
> "----=_NextPart_000_36cf_58cd_f54") NIL NIL) This string will be
> contained a single scalar variable.  I have tried a few different
> things, all of which have failed miserably.  My guess is that spaces
> will need to be changed to commas. Any help would be greatly
> appreciated.
> 
> Thanks,
> Chris

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to