mirror of
https://github.com/GnoConsortium/gno.git
synced 2024-10-09 12:57:08 +00:00
31 lines
919 B
Plaintext
31 lines
919 B
Plaintext
|
# sentgen - random sentence generator
|
||
|
# input: grammar file; sequence of nonterminals
|
||
|
# output: a random sentence for each nonterminal
|
||
|
|
||
|
BEGIN { # read rules from grammar file
|
||
|
while (getline < "grammar" > 0)
|
||
|
if ($2 == "->") {
|
||
|
i = ++lhs[$1] # count lhs
|
||
|
rhscnt[$1, i] = NF-2 # how many in rhs
|
||
|
for (j = 3; j <= NF; j++) # record them
|
||
|
rhslist[$1, i, j-2] = $j
|
||
|
} else
|
||
|
print "illegal production: " $0
|
||
|
}
|
||
|
|
||
|
{ if ($1 in lhs) { # nonterminal to expand
|
||
|
gen($1)
|
||
|
printf("\n")
|
||
|
} else
|
||
|
print "unknown nonterminal: " $0
|
||
|
}
|
||
|
|
||
|
function gen(sym, i, j) {
|
||
|
if (sym in lhs) { # a nonterminal
|
||
|
i = int(lhs[sym] * rand()) + 1 # random production
|
||
|
for (j = 1; j <= rhscnt[sym, i]; j++) # expand rhs's
|
||
|
gen(rhslist[sym, i, j])
|
||
|
} else
|
||
|
printf("%s ", sym)
|
||
|
}
|