コード例 #1
0
ファイル: test_trans.py プロジェクト: hosford42/GLRParser
def genTrans():
    # used for data generation

    parser = Parser()
    parser.load_grammar(text=TestTrans.grammar)

    parser.compile()

    parser.parse(TestTrans.sent)
    tree = parser.make_tree()
    ttree = parser.trans_tree(tree)
    print(ttree.formatr())
    print(ttree.pformatr())
    print(ttree.str_formatr())
    lst = ttree.list_formatr()
    #print(Tree.convert_tree(lst).replace(" -",""))
    trans_list = [parser.post_processor(trans) for trans in ttree.enum()]

    print(trans_list)
コード例 #2
0
ファイル: test_trans.py プロジェクト: mdolgun/GLRParser
def genTransUnify():
    # used for data generation

    parser = Parser()
    parser.parse_grammar(text=TestTransUnify.grammar)

    parser.compile()

    parser.parse(TestTransUnify.sent)
    tree = parser.make_tree()
    utree = parser.unify_tree(tree)
    print(utree.pformat_ext())
    ttree = parser.trans_tree(utree)
    #print(ttree.format(3))
    #print(ttree.pformat(3))
    #print(ttree.str_format(3))
    #lst = ttree.list_format(3)
    #print(Tree.convert_tree(lst).replace(" -",""))
    trans_list = [parser.post_processor(trans) for trans in ttree.enum()]

    print(trans_list)
コード例 #3
0
%suffix_macro V -> base,aorist,caus,passive
%suffix V -> izle,izler,izlet,izlen
%suffix V -> seyret,seyreder,seyrettir,seyredil
%suffix V -> git,gider,götür,gidil

$V -> $watch : izle | seyret
$V -> $go    : git
"""

try:
    parser = Parser("EN","TR") # initialize parser object

    parser.load_grammar(text=grammar) # load grammar from a text
    sent = "i watch" # sentence to parse

    parser.compile() # constructs parsing tables
    parser.parse(sent) # parse the sentence

    tree = parser.make_tree() # generates parse forest
    ttree = parser.trans_tree(tree) # translate the parse forest

    print(ttree.pformatr()) # pretty-print the translated parse forest

    for trans in ttree.enum(): # enumerate and print all alternative translations in the parse forest
        print(trans) # raw outpu
        print(parser.post_processor(trans)) # postprocessed output
except GrammarError as ge:
    print(ge)
except ParseError as pe:
    print(pe)