示例#1
0
文件: c.py 项目: Riamse/chom-c
def new_c():
    ret = Grammar()
    ret.add_nonterminals({k for k in rules.keys()})
    for right in rules.values():
        for tokens in right:
            ret.add_terminals({tok for tok in tokens if tok and (callable(tok) or (isinstance(tok, str) and tok[0] != "$"))})
    for k, v in rules.items():
        ret.add_rule(k, v)
    return ret
示例#2
0
文件: example.py 项目: Riamse/chom-c
    },
    "$NP": {
        ("$A", "$NP"): Fraction(3, 10),
        ("$N",): Fraction(7, 10),
    },
    "$VP": {
        ("$V", "$NP"): Fraction(3, 4),
        ("$V",): Fraction(1, 4),
    },
    "$N": {
        ('ideas',): .5,
        ('linguists',): .5,
    },
    "$V": {
        ("hate",): .5,
        ("generate",): .5,
    },
    "$A": {
        ("great",): .5,
        ("green",): .5,
    }
}

english = Grammar()
english.add_terminals({'generate', 'hate', 'great', 'green', 'ideas', 'linguists'})
english.add_nonterminals({"$S", '$NP', '$VP', '$N', '$V', '$A'})
for k, v in rules.items():
    english.add_rule(k, v)

print(english.expand("$S"))