Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: when $$s =~ m/\G.../gc is too verbose

by Anonymous Monk
on Feb 02, 2006 at 23:28 UTC ( #527498=note: print w/replies, xml ) Need Help??

in reply to when $$s =~ m/\G.../gc is too verbose

Maybe I don't understand the problem, but if you really hate typing so much, why not generalize the solution instead?

Not that I like to generalize things, because I usually end up un-generalizing them a few months later (stupid shifting requirements!), but it seems easier than playing with symbol table manipulations just to save a few keystrokes to me... am I missing something?

I'm thinking of something roughly along these lines... completely untested and possibly wrong code is below. ;-)

# make a table of regular expression patterns my %table = ( qr/(\d+)/ => 'INT', qr/([A-Z]\w*)/ => 'ID', .... # more tokens here ); my ($parser) = shift; my $s = $parser->YYData->{INPUT}; my @matches; # any matches found by our re go in here foreach my $re ( keys %table ) { # for each regexp, check to see if it matches, and # put all the captured values in @matches if it does @matches = ( $$s =~ m/\G$re/gc ); # return the appropriate token, and captures... return( $table{$re}, @matches) if (@matches); } # end search for a token match # token not found... put error handling here ...


Replies are listed 'Best First'.
Re^2: when $$s =~ m/\G.../gc is too verbose
by stefp (Vicar) on Feb 03, 2006 at 00:00 UTC
    Without going to the extremeties of toke.c (the Perl tokenizer), things are usally more complicated than mere pattern matching. One may have to test whatever flags. Otherwise, indeed one could factorize one way or another.

    -- stefp

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2020-11-29 08:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found