Skip to content

Instantly share code, notes, and snippets.

View pstuifzand's full-sized avatar

Peter Stuifzand pstuifzand

View GitHub Profile
@pstuifzand
pstuifzand / counted_rule.pl
Created April 7, 2012 09:28
BNF generator for counted rule X{N}
use 5.14.2;
use List::Util 'sum';
# power a n = a if n == 1
# power a n = power (power a 2) (n/2) if n is even
# power a n = (power (power a 2) (n/2)) * a if n is odd
sub power {
my ($op, $a, $n) = @_;
if ($n == 1) {
@pstuifzand
pstuifzand / testcase1.pl
Created April 7, 2012 11:22
Testcase parser, lexer exhausted
use Marpa::XS;
use MarpaX::Simple::Lexer;
use IO::String;
my $grammar = Marpa::XS::Grammar->new( {
start => 'Parser',
rules => [
[ 'Parser' => [ 'X' ] ],
],
terminals => [qw/X/],
@pstuifzand
pstuifzand / fib.php
Created July 12, 2012 10:18
Fibonacci for web developers
<?php
include "config.php";
$n = $_GET['n'];
if ($n == 1) {
echo "fib($n) == 1";
mysql_query("REPLACE INTO `fib` (`n`, `fib`) VALUES ('1','1')");
}
else if ($n == 2) {
@pstuifzand
pstuifzand / accesslog-parser.pl
Created April 13, 2011 10:33
Accesslog parser in Perl using Parse::RecDescent.
use Data::Dumper;
use Parse::RecDescent;
$Parse::RecDescent::skip = '';
my $grammar = q{
line: ip ws '-' ws user ws datetime ws request ws status ws responsesize
ws referrer ws useragent "\n"
{ $return = {
ip => $item[1],
@pstuifzand
pstuifzand / test.pl
Created January 3, 2013 10:21
test script
#!/usr/bin/env perl
use strict;
use Marpa::R2 2.038000;
use Data::Dumper;
my $grammar = Marpa::R2::Scanless::G->new(
{
action_object => 'My_Actions',
default_action => 'do_first_arg',
@pstuifzand
pstuifzand / test2.pl
Created January 3, 2013 12:13
Another test program. The input string is a comma separated list of multi-digit integers. The output should be a `Dumper`ed array-ref. $VAR1 = [100,3,4]. Without the ":discard" rules I get an error about how it can only parse the first number "100" and stops at the ",".
use strict;
use Marpa::R2 2.038000;
use Data::Dumper;
my $grammar = Marpa::R2::Scanless::G->new(
{
action_object => 'My_Actions',
default_action => 'do_first_arg',
source => \(<<'END_OF_SOURCE'),
@pstuifzand
pstuifzand / MarpaX-JSON.pm
Last active December 10, 2015 14:28
A JSON parser in Marpa.
package MarpaX::JSON;
use strict;
use Marpa::R2 2.039_000;
sub new {
my ($class) = @_;
my $self = bless {}, $class;
$self->{grammar} = Marpa::R2::Scanless::G->new(
@pstuifzand
pstuifzand / Demo-MarpaX-Makefile.pm
Last active December 10, 2015 14:38
Completely incomplete parser for Makefiles
package Demo::MarpaX::Makefile;
use strict;
use Marpa::R2;
sub new {
my ($class) = @_;
my $self = bless {}, $class;
$self->{grammar} = Marpa::R2::Scanless::G->new(
@pstuifzand
pstuifzand / test-string-parse.pl
Created January 6, 2013 11:22
Parsing a JSON string with Marpa.
use strict;
use Marpa::R2;
my $g = Marpa::R2::Scanless::G->new({
action_object => 'main',
default_action => 'do_first_arg',
source => \(<<'END_OF_SOURCE'),
:start ::= string
@pstuifzand
pstuifzand / fixed-script.pl
Last active December 11, 2015 04:58
Fix script for Marpa question
#!/usr/bin/perl
use strict;
use warnings;
use Marpa::R2 2.023008;
use Data::Dumper;
my %type = (
boy => 'noun',
girl => 'noun',