in reply to Re: Re: Re: Many birds, single cage...
in thread Many birds, single cage...

Fellow mr mischief
I've never ever used Parse::RecDescent before. It worth the effort? The documentation is pretty big, seems that its a big and complex module. Except by some more regular expressions spread through the code, I'm basically using this as my "parser" (sorry about the portuguese comments, I can translate them on demand):

###################################################################### +##### package Netfax::Grammar; ###################################################################### +##### # $Id: Grammar.pm,v 1.3 2003/12/09 18:35:28 lcampos Exp $ use strict; use warnings; our( $VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS ); BEGIN{ use Exporter; $VERSION = do{my@r=(q$Revision: 1.3 $=~/\d+/g);sprintf"%d."."%02d"x$ +#r,@r}; use base qw( Exporter ); } @EXPORT_OK = qw( $command_line $subject_spec $telephone_list $option $option_spec $from_spec $domainname_spec $username_spec ); ###################################################################### +##### # Gramatica para capturar linha de comando (RFC822 "Subject:") my $time_spec = qr{ # Hora: dois digitos codificando numeros de 00 a 23. (?:[01]\d|2[0-3]) # Dois pontos, separador de hora/min, opcional. :? # Minutos: dois digitos, codificando minutos de 00 a 59 [0-5]\d}x; my $day_spec = qr{ # Dias de 01 a 09 0[1-9] | # Dias de 10 a 29 [12][0-9] | # Dias 30 e 31. 3[01] }x; my $month_spec = qr{ # Meses de 01 a 09 0[1-9] | # Meses 10, 11 e 12 1[012] }x; my $year_spec = qr{ # Seculo vinte, apenas 20 # Decadas: de 03 a 99. (?:0[3-9]|[1-9]\d) }x; my $date_sep = qr{ # Separadores validos sao '.' e '-'. (?:\.|-) }x; my $date_spec = qr{ # Dia, de 01 a 31. $day_spec # Separador de data, opcional. $date_sep? # Mes, de 01 a 12. $month_spec # Separador de data, opcional. $date_sep? # Ano, de 2003 a 2099. $year_spec }x; our $option = qr{ # Todas as opcoes comecam com '/' / (?: # IMO ou IM [Ii][Mm][Oo]? | # NSIM ou SIM [Nn]?[Ss][Ii][Mm] | # D (data) [Dd]$date_spec | # H (hora) [Hh]$time_spec ) }x; # Captura o valor passado como argumento em algumas opcoes. our $option_spec = qr{ # Todas as opcoes comecam com '/' / (?: # IMO ou IM ([Ii][Mm][Oo])? | # NSIM ou SIM ([Nn]?[Ss][Ii][Mm]) | # D (data) [Dd]($date_spec) | # H (hora) [Hh]($time_spec) ) }x; my $option_list = qr{ # lista de opcoes: (?: # uma opcao $option # seguida de zero ou mais espacos \s* # tantas vezes quantas se puder encontrar. )* }x; my $phone = qr{ (?: # Caracteres '-', '+', '(', ')', '.', '0-9', # 'a-z', 'A-Z', '_' [-+\(\)\.\w] # ou | # todo espaco em branco (' ') nao seguido de virgula \ (?!,) # obrigatoriamente uma vez, e # tantas mais quantas voce conseguir. )+ }x; my $phone_list = qr{ $phone(?:(?:\s*,\s*)$phone)* }x; # Linha de comando: captura a lista de telefones # e a lista de opcoes separadamente. our $command_line = qr{($phone_list)\s*($option_list)}x; our $subject_spec = qr{^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]:\s*$command_line} +x; # Fim das Regras ###################################################################### +##### ###################################################################### +##### # Regras para capturar telefones de uma lista de telefones our $telephone_list = qr{($phone)(?:\s*,\s*)?}x; # Fim das Regras ###################################################################### +##### ###################################################################### +##### # Regras para capturar o RFC822 "From:" de um email our $username_spec = qr{ # Username [A-Za-z0-9_.-]+ }x; our $domainname_spec = qr;(?:$username_spec\.)+[A-Za-z]{2,4};x; our $from_spec = qr{ $username_spec # "at" @ $domainname_spec }x; # Fim das Regras ###################################################################### +##### 1;__END__

I would love to read what the fellow monks think about this code, and if someone have any nice way to improve it. Thank you all for the answers and considerations, and may the gods bless you.


"In few words, translating PerlMonks documentation and best articles to other languages is like building a bridge to join other Perl communities into PerlMonks family. This makes the family bigger, the knowledge greather, the parties better and the life easier." -- monsieur_champs

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: Many birds, single cage...
by mr_mischief (Monsignor) on Dec 10, 2003 at 14:51 UTC
    If your program, with the chances suggested before, works for all cases and will never or very seldom need to be changed, then it's probably not worth pulling out a big powerful tool like a parser generator module.

    If it's likely to change much, even a little at a time, then eventually you'd have saved more and more work in the end by moving to using one. It's all a matter of cost versus benefits.

    If everything works and never needs to be changed, then the cost of rewriting the code to use another tool is wasted effort with no real up-front benefit. If someone needs to maintain it, then the costs add up over time either way and the benefits of having a cleaner implementation become more pronounced.

    BTW, I've had enough French and Spanish and your comments are written clearly enough that I get a good understanding of them, although I wouldn't be able to rewrite them en Portugues. Yet another example of why clear comments are helpful -- non-native readers have a massively better chance of making sense of them.



    Christopher E. Stith

      As a Londoner Bad Guy would say: "in pieces".

      • My program don't works for all the cases. I don't know all the cases yet. This depends on the sales folks, they're allways thinking on something new. Chances are that this would never end.
      • I'm just moving out from a substr() version that is a maintenance nightmare. Maybe should I wait one or two months before trying to learn something new to improve the program again?
      • I agree with you when you say "If everything works and never needs to be changed(...)"
      • So the comments wasn't a total waste at all, even considering that I will rewrite all this code soon or later. Thank you very much for the effort of trying to understand them.

      "In few words, translating PerlMonks documentation and best articles to other languages is like building a bridge to join other Perl communities into PerlMonks family. This makes the family bigger, the knowledge greather, the parties better and the life easier." -- monsieur_champs