Skip to content
Browse files

Improved Lisp example

  • Loading branch information...
1 parent 7134e31 commit 2cbf2e92fdb3579015d6ea001ee690ec7a3af050 @pstuifzand committed Mar 23, 2012
Showing with 40 additions and 9 deletions.
  1. +8 −1 Makefile
  2. +11 −7 examples/lisp/lisp.mp
  3. +17 −0 examples/lisp/lisp.pl
  4. +1 −1 examples/lisp/test.lsp
  5. +3 −0 lib/MarpaX/CodeGen/SimpleLex.pm
View
9 Makefile
@@ -4,17 +4,24 @@ gen_marpa.pl: marpa_parser.pl marpa+.mp
perl marpa_parser.pl marpa+.mp > $@
lib/MarpaX/Parser/Marpa.pm: gen_marpa.pl marpa+.mp
- mkdir -p lib/MarpaX/Parser
+ -mkdir -p lib/MarpaX/Parser
perl gen_marpa.pl marpa+.mp MarpaX::CodeGen::SimpleLex > $@
lib/MarpaX/Parser/HTMLGen.pm: examples/htmlgen/htmlgen.mp
+ -mkdir -p lib/MarpaX/Parser
bin/marp $< MarpaX::CodeGen::SimpleLex MarpaX::Parser::HTMLGen > $@
htmlgen: lib/MarpaX/Parser/HTMLGen.pm
perl examples/htmlgen/htmlgen.pl examples/htmlgen/test.htmlgen
+lib/MarpaX/Parser/Lisp.pm: examples/lisp/lisp.mp
+ -mkdir -p lib/MarpaX/Parser
+ bin/marp $< MarpaX::CodeGen::SimpleLex MarpaX::Parser::Lisp > $@
+
+lisp: lib/MarpaX/Parser/Lisp.pm
+
clean:
-rm lib/MarpaX/Parser/Marpa.pm
-rm lib/MarpaX/Parser/HTMLGen.pm
View
18 examples/lisp/lisp.mp
@@ -1,12 +1,16 @@
-
String = /"([^"]+)"/
Number = /(\d+)/
Symbol = /([a-z]+)/
+Space = /[ \r\n\t]+/
-Parser ::= Expression+ {{ shift; return \@_; }}
-Expression ::= $( List $) {{ shift; return $_[1]; }}
-List ::= Expression+ {{ shift; return \@_; }}
-Expression ::= String {{ shift; return { string => $_[0] }; }}
-Expression ::= Number {{ shift; return { number => $_[0] }; }}
-Expression ::= Symbol {{ shift; return { symbol => $_[0] }; }}
+Parser ::= Expression+ {{ shift; return \@_; }}
+Expression ::= WS $( WS Parser WS $) WS {{ shift; return $_[3]; }}
+Expression ::= WS $( WS Expression WS $. WS Expression WS $) WS {{ shift; return [ $_[3], $_[7] ]; }}
+Expression ::= WS $( WS $) WS {{ shift; return 'nil'; }}
+Expression ::= WS String WS {{ shift; return { string => $_[1] }; }}
+Expression ::= WS Number WS {{ shift; return { number => $_[1] }; }}
+Expression ::= WS Symbol WS {{ shift; return $_[1]; }}
+Expression ::= WS $' Expression WS {{ shift; return [ 'quote', $_[2] ]; }}
+WS ::= Space
+WS ::= Null
View
17 examples/lisp/lisp.pl
@@ -0,0 +1,17 @@
+use strict;
+use lib 'lib';
+use Data::Dumper;
+
+use MarpaX::Parser::Lisp;
+use MarpaX::CodeGen::Dumper;
+
+my $infile = $ARGV[0];
+
+open my $fh, '<', $infile or die "Can't open '$infile'";
+
+my $codegen = MarpaX::CodeGen::Dumper->new;
+
+my $parser = MarpaX::Parser::Lisp->new;
+my $parse_tree = $parser->parse($fh);
+$codegen->generate_code($parse_tree);
+
View
2 examples/lisp/test.lsp
@@ -1 +1 @@
-( add 10 10 1 9393 45 )
+3 ((1) . 5) 4
View
3 lib/MarpaX/CodeGen/SimpleLex.pm
@@ -106,6 +106,9 @@ HEADER
}
else {
$_->{char} =~ s/^\$//;
+ if ($_->{char} eq "'") {
+ $_->{char} = "\\'";
+ }
printf(" %-30s => '%s',\n", $_->{lhs}, $_->{char});
}
}

0 comments on commit 2cbf2e9

Please sign in to comment.
Something went wrong with that request. Please try again.