diff options
| author | Adam Russell <russella3@wit.edu> | 2021-05-01 21:06:17 -0400 |
|---|---|---|
| committer | Adam Russell <russella3@wit.edu> | 2021-05-01 21:06:17 -0400 |
| commit | db43fb9a74900289600d3eaaf180f60b0aeafccc (patch) | |
| tree | c27ef7bd0796b831a3bc6e3a5f64845473f7e8a5 | |
| parent | 84d0351389dbe22085cd173fe0049713f8c53496 (diff) | |
| download | perlweeklychallenge-club-db43fb9a74900289600d3eaaf180f60b0aeafccc.tar.gz perlweeklychallenge-club-db43fb9a74900289600d3eaaf180f60b0aeafccc.tar.bz2 perlweeklychallenge-club-db43fb9a74900289600d3eaaf180f60b0aeafccc.zip | |
initial commit of solutions
| -rw-r--r-- | challenge-110/adam-russell/perl/PhoneNumberParser.pm | 218 | ||||
| -rw-r--r-- | challenge-110/adam-russell/perl/PhoneNumberParser.yp | 46 | ||||
| -rw-r--r-- | challenge-110/adam-russell/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-110/adam-russell/prolog/ch-1.p | 17 |
4 files changed, 306 insertions, 0 deletions
diff --git a/challenge-110/adam-russell/perl/PhoneNumberParser.pm b/challenge-110/adam-russell/perl/PhoneNumberParser.pm new file mode 100644 index 0000000000..66a94a632c --- /dev/null +++ b/challenge-110/adam-russell/perl/PhoneNumberParser.pm @@ -0,0 +1,218 @@ +#################################################################### +# +# This file was generated using Parse::Yapp version 1.21. +# +# Don't edit this file, use source file instead. +# +# ANY CHANGE MADE HERE WILL BE LOST ! +# +#################################################################### +package PhoneNumberParser; +use vars qw ( @ISA ); +use strict; + +@ISA= qw ( Parse::Yapp::Driver ); +use Parse::Yapp::Driver; + + + +sub new { + my($class)=shift; + ref($class) + and $class=ref($class); + + my($self)=$class->SUPER::new( yyversion => '1.21', + yystates => +[ + {#State 0 + ACTIONS => { + 'OPEN' => 2, + 'DIGIT' => 5, + 'PLUS' => 1 + }, + GOTOS => { + 'prefix' => 4, + 'phone_number' => 3 + } + }, + {#State 1 + ACTIONS => { + 'DIGIT' => 6 + } + }, + {#State 2 + ACTIONS => { + 'DIGIT' => 7 + } + }, + {#State 3 + ACTIONS => { + '' => 8 + } + }, + {#State 4 + ACTIONS => { + 'SPACE' => 9 + } + }, + {#State 5 + ACTIONS => { + 'DIGIT' => 10 + } + }, + {#State 6 + ACTIONS => { + 'DIGIT' => 11 + } + }, + {#State 7 + ACTIONS => { + 'DIGIT' => 12 + } + }, + {#State 8 + DEFAULT => 0 + }, + {#State 9 + ACTIONS => { + 'DIGIT' => 13 + }, + GOTOS => { + 'area_exchange_subscriber' => 14 + } + }, + {#State 10 + ACTIONS => { + 'DIGIT' => 15 + } + }, + {#State 11 + DEFAULT => -4 + }, + {#State 12 + ACTIONS => { + 'CLOSE' => 16 + } + }, + {#State 13 + ACTIONS => { + 'DIGIT' => 17 + } + }, + {#State 14 + DEFAULT => -1 + }, + {#State 15 + ACTIONS => { + 'DIGIT' => 18 + } + }, + {#State 16 + DEFAULT => -3 + }, + {#State 17 + ACTIONS => { + 'DIGIT' => 19 + } + }, + {#State 18 + DEFAULT => -2 + }, + {#State 19 + ACTIONS => { + 'DIGIT' => 20 + } + }, + {#State 20 + ACTIONS => { + 'DIGIT' => 21 + } + }, + {#State 21 + ACTIONS => { + 'DIGIT' => 22 + } + }, + {#State 22 + ACTIONS => { + 'DIGIT' => 23 + } + }, + {#State 23 + ACTIONS => { + 'DIGIT' => 24 + } + }, + {#State 24 + ACTIONS => { + 'DIGIT' => 25 + } + }, + {#State 25 + ACTIONS => { + 'DIGIT' => 26 + } + }, + {#State 26 + DEFAULT => -5 + } +], + yyrules => +[ + [#Rule 0 + '$start', 2, undef + ], + [#Rule 1 + 'phone_number', 3, undef + ], + [#Rule 2 + 'prefix', 4, undef + ], + [#Rule 3 + 'prefix', 4, undef + ], + [#Rule 4 + 'prefix', 3, undef + ], + [#Rule 5 + 'area_exchange_subscriber', 10, undef + ] +], + @_); + bless($self,$class); +} + +#line 15 "perl\PhoneNumberParser.yp" + + +sub lexer{ + my($parser) = @_; + $parser->YYData->{INPUT} or return('', undef); + ## + # send tokens to parser + ## + for($parser->YYData->{INPUT}){ + s/^(\s)// and return ("SPACE", $1); + s/^(\d)// and return ("DIGIT", $1); + s/^(\()// and return ("OPEN", $1); + s/^(\))// and return ("CLOSE", $1); + s/^(\+)// and return ("PLUS", $1); + } +} + +sub error{ + exists $_[0]->YYData->{ERRMSG} + and do{ + print $_[0]->YYData->{ERRMSG}; + return; + }; + print "syntax error\n"; +} + +sub parse{ + my($self, $input) = @_; + $self->YYData->{INPUT} = $input; + my $result = $self->YYParse(yylex => \&lexer, yyerror => \&error); + return $result; +} +1; diff --git a/challenge-110/adam-russell/perl/PhoneNumberParser.yp b/challenge-110/adam-russell/perl/PhoneNumberParser.yp new file mode 100644 index 0000000000..3f85a6a55b --- /dev/null +++ b/challenge-110/adam-russell/perl/PhoneNumberParser.yp @@ -0,0 +1,46 @@ +%token SPACE DIGIT OPEN CLOSE PLUS +%% + +phone_number: prefix SPACE area_exchange_subscriber +; + +prefix: DIGIT DIGIT DIGIT DIGIT + | OPEN DIGIT DIGIT CLOSE + | PLUS DIGIT DIGIT +; + +area_exchange_subscriber: DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT DIGIT +; + +%% + +sub lexer{ + my($parser) = @_; + $parser->YYData->{INPUT} or return('', undef); + ## + # send tokens to parser + ## + for($parser->YYData->{INPUT}){ + s/^(\s)// and return ("SPACE", $1); + s/^(\d)// and return ("DIGIT", $1); + s/^(\()// and return ("OPEN", $1); + s/^(\))// and return ("CLOSE", $1); + s/^(\+)// and return ("PLUS", $1); + } +} + +sub error{ + exists $_[0]->YYData->{ERRMSG} + and do{ + print $_[0]->YYData->{ERRMSG}; + return; + }; + print "syntax error\n"; +} + +sub parse{ + my($self, $input) = @_; + $self->YYData->{INPUT} = $input; + my $result = $self->YYParse(yylex => \&lexer, yyerror => \&error); + return $result; +}
\ No newline at end of file diff --git a/challenge-110/adam-russell/perl/ch-1.pl b/challenge-110/adam-russell/perl/ch-1.pl index e69de29bb2..22b891b7a4 100644 --- a/challenge-110/adam-russell/perl/ch-1.pl +++ b/challenge-110/adam-russell/perl/ch-1.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +## +# Write a script to display all valid phone numbers in a given text file. +## +use Capture::Tiny q/capture_stdout/; +use PhoneNumberParser; + +MAIN:{ + my $parser = new PhoneNumberParser(); + while(my $line = <DATA>){ + $line =~ s/^\s+|\s+$//g; + my $syntax_error = capture_stdout { + $parser->parse($line); + }; + print("$line\n") if !$syntax_error; + } +} + +__DATA__ +0044 1148820341 + +44 1148820341 + 44-11-4882-0341 +(44) 1148820341 + 00 1148820341
\ No newline at end of file diff --git a/challenge-110/adam-russell/prolog/ch-1.p b/challenge-110/adam-russell/prolog/ch-1.p index e69de29bb2..ecac5444f0 100644 --- a/challenge-110/adam-russell/prolog/ch-1.p +++ b/challenge-110/adam-russell/prolog/ch-1.p @@ -0,0 +1,17 @@ +/* + Write a script to display all valid phone numbers in a given text file. +*/ +%:-initialization(main). + +phone_number --> prefx, space, area_exchange_subscriber. +prefx --> ['('], digit, digit, [')']. +prefx --> ['+'], digit, digit. +prefx --> digit, digit, digit, digit. +space --> [' ']. +area_exchange_subscriber --> digit, digit, digit, digit, digit, digit, digit, digit, digit, digit. +digit --> [0]; [1]; [2]; [3]; [4]; [5]; [6]; [7]; [8]; [9]. + + + +main:- + halt.
\ No newline at end of file |
