Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Parsing CCL (Common Command Language) commands

by mugwumpjism (Hermit)
on Oct 17, 2002 at 11:46 UTC ( #205989=note: print w/replies, xml ) Need Help??


in reply to Parsing CCL (Common Command Language) commands

Left logical grammars such as you have listed are very mechanically converted to `recursive descent' parsers.

You might want to check out Parse::RecDescent. You should be able to go straight from your grammar to a working parser.

Otherwise, make yourself a next_token(), an eat_token() function, and to make things easier, a save_position() and restore_position() pair to allow you to easily put already eaten tokens back onto the front of the stream (not necessary with some grammars, see books on recursive descent and LL grammars for more) and write code like this:

sub expect_Elements { my $stream = shift; my $expression; $p = save_position($stream); if (next_token() eq "(") { eat_token($stream); $expression = [ expect_CCL_Find($stream) ]; # It might make more sense to die() here if (eat_token() ne ")") { $expression = undef; } } else { if ($expression = expect_Set($stream)) { } elsif ($expression = expect_Terms($stream)) { } elsif ($expression = expect_Qualifiers($stream)) { if ($rel = expect_Relation($stream)) { if (next_token($stream) eq "(") { eat_token(); $expression = [ expect_CCL_Find($stream) ]; (eat_token() eq ")") or $expression = undef; } elsif ( my $terms = expect_Terms($stream) ) { $expression = [ $rel, $expression, $terms ]; } else { $expression = undef; } } elsif (next_token($stream) eq "=") { eat_token(); $string1 = expect_string($stream); $op = next_token($stream); $string2 = expect_string($stream); if (!defined $string1 or !defined $string2 or !defined $op or $op ne "-") { $expression = undef; } else { $expression = [ "=", $expression, $string1, "-", $ +string2 ]; } } else { $expression = undef; } } else { $expression = undef; } } if (defined $expression) { return $expression; } else { restore_position($stream, $pointer); return undef; } }

I think it should be fairly obvious why you'd want a module (or flex/yacc) to write this for you for larger grammers, but every programmer should write at least one recursive descent parser :-).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://205989]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2023-12-08 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (35 votes). Check out past polls.

    Notices?