#!/usr/bin/perl -ws $|++; use Parse::RecDescent; # $::RD_TRACE = 1; my $start = "START"; # start symbol (my $parser = Parse::RecDescent->new(<<'END_OF_GRAMMAR')) or die "bad!"; ## return hashref ## { ident => { ## is => [ ## [weight => item, item, item, ...], ## [weight => item, item, item, ...], ... ## ], ## defined => { line-number => times } ## used => { line-number => times } ## }, ... ## } ## item is " literal" or ident ## ident is C-symbol or number (internal for nested rules) { my %grammar; my $internal = 0; } grammar: rule(s) /\Z/ { \%grammar; } ## rule returns identifier (not used) rule: identifier ":" defn ';' { push @{$grammar{$item[1]}{is}}, @{$item[3]}; $grammar{$item[1]}{defined}{$itempos[1]{line}{to}}++; $item[1]; } | <error> ## defn returns listref of choices defn: <leftop: choice "|" choice> ## choice returns a listref of [weight => @items] choice: unweightedchoice { [ 1 => @{$item[1]} ] } | /\d+(\.\d+)?/ /\@/ unweightedchoice { [ $item[1] => @{$item[3]} ] } ## unweightedchoice returns a listref of @items unweightedchoice: item(s) item: quoted_string | identifier ...!/:/ { $grammar{$item[1]}{used}{$itempos[1]{line}{to}}++; $item[1]; # non-leading space flags an identifier } | "(" defn ")" { # parens for recursion, gensym an internal ++$internal; push @{$grammar{$internal}{is}}, @{$item[2]}; $internal; } | <error> quoted_string: /"/ <skip:""> quoted_char(s?) /"/ { " " . join "", @{$item[3]} # leading space flags a string } ## this should be expanded, but it works for this grammar :) quoted_char: /[^\\"]+/ | /\\n/ { "\n" } | /\\"/ { "\"" } identifier: /[A-Za-z_]\w*/ END_OF_GRAMMAR my @data = <DATA>; for (@data) { s/^\s*#.*//; } (my $parsed = $parser->grammar(join '', @data)) or die "bad parse"; for my $id (sort keys %$parsed) { next if $id =~ /^\d+$/; # skip internals my $id_ref = $parsed->{$id}; unless (exists $id_ref->{defined}) { print "$id used in @{[sort keys %{$id_ref->{used}}]} but not defined - FATAL\n"; } unless (exists $id_ref->{used} or $id eq $start) { print "$id defined in @{[sort keys %{$id_ref->{defined}}]} but not used - WARNING\n"; } } use Data::Dumper; print Dumper($parsed); show($start); sub show { my $defn = shift; die "missing defn for $defn" unless exists $parsed->{$defn}; my @choices = @{$parsed->{$defn}{is}}; my $weight = 0; my @keeper = (); while (@choices) { my ($thisweight, @thisitem) = @{pop @choices}; $thisweight = 0 if $thisweight < 0; # no funny stuff $weight += $thisweight; @keeper = @thisitem if rand($weight) < $thisweight; } for (@keeper) { ## should be a list of ids or defns die "huh $_ in $defn" if ref $defn; if (/^ (.*)/s) { print $1; } elsif (/^(\w+)$/) { show($1); } else { die "Can't show $_ in $defn\n"; } } } __END__ START: stanza "\n---\n" stanza "\n---\n" stanza; stanza: stanza " " exclaim " " stanza2 | stanza2; stanza2: sentence " " comparison " " question | sentence " " comparison | comparison " " comparison " " exclaim | address " " question " " question " " sentence; sentence: sentence "\n" sentence2 | sentence2; sentence2: "The " adjectiveNotHep " " personNotHep " " verbRelating " the " adjectiveHep " " personHep "." | "The " personHep " " verbRelating " the " adjectiveNotHep ", " adjectiveNotHep " " personNotHep "."; question: question " " question2 | question2; question2: ques_start " " adjectiveHep " " personNotHep "?" | ques_start " " adjectiveNotHep " " personHep "?"; comparison: comparison " " comparison2 | comparison2; comparison2: "One says '" compNotHep "' while the other says '" compHep "'." | "One thinks '" compNotHep "' while the other thinks '" compHep "'." | "They shout '" compNotHep "!' And we shout '" compHep "'." | "It's " compNotHep " versus " compHep "!" ; personNotHep: "capitalist" | "silk purse man" | "square" | "banker" | "Merchant King" | "pinstripe suit" ; personHep: "cat" | "beat soul" | "wordsmith" | "hep cat" | "free man" | "street poet" | "skin beater" | "reed man" ; adjectiveNotHep: "soul-sucking" | "commercial" | "cash-counting" | "bloody-handed" | "four-cornered" | "uncool" | "love-snuffing"; adjectiveHep: "love-drunk" | "cool, cool" | "happening" | "tuned-in" | "street wise" | "wise and learned"; verbRelating: "begrudges" | "fears" | "distresses" | "dodges" | "dislikes" | "evades" | "curses" | "belittles" | "avoids" | "battles"; compNotHep: "recreation" | "isolation" | "tranportation" | "sacred nation" | "complication" | "subordination"; compHep: "fornication" | "instigation" | "interpretation" | "elevation" | "animation" | "inebriation" | "true relation"; ques_start: 2 @ (5 @ "Could there ever" | 7 @ "How could there") " be a" | "Can you picture a" ; address: "Catch this:" | "Listen, cats," | "Dig it:" | "I lay this on you:"; exclaim: "Heavy, man."| "Heavy." | "Yow!" | "Snap 'em for me." | "Dig it.";
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
demo.c | File | 126 B | 0644 |
|
demo.pl | File | 371 B | 0644 |
|
demo_Cgrammar.pl | File | 10.08 KB | 0644 |
|
demo_Cgrammar_v2.pl | File | 7.08 KB | 0644 |
|
demo_LaTeXish.pl | File | 1.63 KB | 0644 |
|
demo_LaTeXish_autoact.pl | File | 1.72 KB | 0644 |
|
demo_NL2SQL.pl | File | 2.27 KB | 0644 |
|
demo_OOautoparsetree.pl | File | 1.98 KB | 0644 |
|
demo_OOparsetree.pl | File | 1.21 KB | 0644 |
|
demo_PerlCSV.pl | File | 413 B | 0644 |
|
demo_another_Cgrammar.pl | File | 21.08 KB | 0644 |
|
demo_arithmetic.pl | File | 758 B | 0644 |
|
demo_autorule.pl | File | 309 B | 0644 |
|
demo_autoscoresep.pl | File | 587 B | 0644 |
|
demo_autostub.pl | File | 271 B | 0644 |
|
demo_bad.pl | File | 816 B | 0644 |
|
demo_buildcalc.pl | File | 1.3 KB | 0644 |
|
demo_calc.pl | File | 1.82 KB | 0644 |
|
demo_codeblock.pl | File | 287 B | 0644 |
|
demo_cpp.pl | File | 1.23 KB | 0644 |
|
demo_decomment.pl | File | 2.55 KB | 0644 |
|
demo_decomment_nonlocal.pl | File | 2.44 KB | 0644 |
|
demo_delete.pl | File | 901 B | 0644 |
|
demo_derived.pl | File | 891 B | 0644 |
|
demo_dot.pl | File | 4.79 KB | 0644 |
|
demo_embedding.pl | File | 977 B | 0644 |
|
demo_errors.pl | File | 965 B | 0644 |
|
demo_eval.pl | File | 1.15 KB | 0644 |
|
demo_implicit.pl | File | 568 B | 0644 |
|
demo_itemhash.pl | File | 1.64 KB | 0644 |
|
demo_language.pl | File | 2.57 KB | 0644 |
|
demo_leftassoc.pl | File | 1.06 KB | 0644 |
|
demo_leftop.pl | File | 957 B | 0644 |
|
demo_lexer.pl | File | 577 B | 0644 |
|
demo_lisplike.pl | File | 1.49 KB | 0644 |
|
demo_logic.pl | File | 542 B | 0644 |
|
demo_matchrule.pl | File | 772 B | 0644 |
|
demo_matchrule2.pl | File | 540 B | 0644 |
|
demo_mccoy.pl | File | 516 B | 0644 |
|
demo_metaRD.pm | File | 3.02 KB | 0644 |
|
demo_methods.pl | File | 785 B | 0644 |
|
demo_operator.pl | File | 835 B | 0644 |
|
demo_opreps.pl | File | 1.01 KB | 0644 |
|
demo_parsetree.pl | File | 661 B | 0644 |
|
demo_perlparsing.pl | File | 956 B | 0644 |
|
demo_piecewise.pl | File | 3.03 KB | 0644 |
|
demo_precalc.pl | File | 743 B | 0644 |
|
demo_quicklist.pl | File | 1.5 KB | 0644 |
|
demo_randomsentence.pl | File | 2.89 KB | 0644 |
|
demo_recipe.pl | File | 2.09 KB | 0644 |
|
demo_restructure_easy.pl | File | 1.21 KB | 0644 |
|
demo_restructure_painful.pl | File | 1.97 KB | 0644 |
|
demo_scoredsep.pl | File | 606 B | 0644 |
|
demo_selfmod.pl | File | 953 B | 0644 |
|
demo_separators.pl | File | 779 B | 0644 |
|
demo_simple.pl | File | 1.94 KB | 0644 |
|
demo_simpleXML.pl | File | 1.87 KB | 0644 |
|
demo_simplequery.pl | File | 831 B | 0644 |
|
demo_skipcomment.pl | File | 609 B | 0644 |
|
demo_street.pl | File | 502 B | 0644 |
|
demo_template.pl | File | 688 B | 0644 |
|
demo_textgen.pl | File | 5 KB | 0644 |
|
demo_tokens.pl | File | 552 B | 0644 |
|
demo_undumper.pl | File | 23.17 KB | 0644 |
|
demo_whoson.pl | File | 2.61 KB | 0644 |
|