예제 #1
0
    def setUp(self):
        parser = Parser()

        parser.load_grammar(text=self.grammar)
        parser.compile()
        self.parser = parser
        self.maxDiff = None
예제 #2
0
 def test_rules(self):
     self.maxDiff = None
     for define, exp_rules in zip(self.defines, self.rule_sets):
         parser = Parser()
         parser.load_grammar(text=define + self.grammar)
         with self.subTest(rules=parser.rules, exp_rules=exp_rules):
             self.assertEqual(parser.rules, exp_rules)
예제 #3
0
def genTestMacro():
    for define, exp_rules in zip(TestIfdef.defines, TestIfdef.rule_sets):
        parser = Parser()
        parser.load_grammar(text=define + TestIfdef.grammar)
        print(define)
        print(parser.rules)
        print("==============================")
예제 #4
0
def genTestCost(cls):
    parser = Parser()
    parser.load_grammar(text=cls.grammar, reverse=cls.reverse)
    parser.compile()
    for sent in cls.sents:
        parser.parse(sent)
        tree = parser.make_tree()
        utree = parser.unify_tree(tree)
        ttree = parser.trans_tree(utree)
        print(list(sorted(ttree.enumx())))
예제 #5
0
    def setUp(self):
        parser = Parser()
        grammar = textwrap.dedent(self.grammar)

        parser.load_grammar(text=grammar)
        sent = "c"

        parser.compile()
        parser.parse(sent)

        self.parser = parser
        self.tree = parser.make_tree()
        self.maxDiff = None
예제 #6
0
    def setUp(self):
        parser = Parser("EN", "TR")
        grammar = textwrap.dedent(self.grammar)

        parser.load_grammar(text=grammar)

        parser.compile()
        parser.parse(self.sent)

        self.parser = parser
        self.tree = parser.make_tree()
        self.ttree = parser.trans_tree(self.tree)
        self.maxDiff = None
예제 #7
0
    def setUp(self):
        parser = Parser()
        grammar = textwrap.dedent(self.grammar)

        parser.load_grammar(text=grammar)
        sent = "i saw the man in the house with the telescope"

        parser.compile()
        parser.parse(sent)

        self.parser = parser
        self.tree = parser.make_tree()
        self.maxDiff = None
예제 #8
0
def gen_TestParseMiddleEmpty():
    parser = Parser()
    grammar = textwrap.dedent(TestParseMiddleEmpty.grammar)

    parser.load_grammar(text=grammar)
    sent = "c"

    parser.compile()
    parser.parse(sent)
    tree = parser.make_tree()
    print(tree.format())
    print(tree.str_format())
    print(tree.pformat())
    print(tree.pformat_ext())
예제 #9
0
def gen_TestParseSimple():
    parser = Parser()
    grammar = textwrap.dedent(TestParseSimple.grammar)

    parser.load_grammar(text=grammar)
    sent = "i saw the man in the house with the telescope"

    parser.compile()
    parser.parse(sent)
    tree = parser.make_tree()
    print(tree.format())
    print(tree.str_format())
    print(tree.pformat())
    print(tree.pformat_ext())
예제 #10
0
def gen_TestUnifyBinary():
    parser = Parser()

    parser.load_grammar(text=TestUnifyBinary.grammar)
    parser.compile()
    sents = ["a0 b0", "a0 b1", "a1 b0", "a1 b1"]

    for sent in sents:
        print(sent)
        try:
            parser.parse(sent)
            tree = parser.make_tree()
            tree2 = parser.unify_tree(tree)
            print(tree2.pformat_ext())
        except UnifyError as ue:
            print(ue)
        except ParseError as pe:
            print(pe)
예제 #11
0
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)
예제 #12
0
def gen_TestUnify():
    parser = Parser()

    parser.load_grammar(text=TestUnify.grammar)
    parser.compile()
    sents = [
        "i am watching her", "she is watching me", "these men are watching us",
        "me am watching you", "she is watching i", "two man is watching it",
        "a man watch us", "they watch us", "he watches the men",
        "he watches a men", "i watch him"
    ]
    for sent in sents:
        print(sent)
        try:
            parser.parse(sent)
            tree = parser.make_tree()
            tree2 = parser.unify_tree(tree)
            print(tree2.pformat_ext())
        except UnifyError as ue:
            print(ue)
        except ParseError as pe:
            print(pe)
예제 #13
0
import sys
sys.path.append("..")

from GLRParser import Parser, ParseError, GrammarError, Tree

try:
    parser = Parser()  # initialize parser object

    parser.load_grammar(
        "..\GLRParser\grm\simple_trans.grm")  # load grammar from a file
    sent = "i saw the man in the house with the telescope"  # 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.replace(" -", ""))  # concat suffixes
except GrammarError as ge:
    print(ge)
except ParseError as pe:
    print(pe)
예제 #14
0
def genTestMacro():
    parser = Parser()
    parser.load_grammar(text=TestMacro.grammar)
    for rule in parser.rules:
        print(rule)
예제 #15
0
from GLRParser import Parser, ParseError, GrammarError, Tree

grammar = """
%ifdef token1
    %ifdef token2
        S -> in : out1
    %else
        S -> in : out2
    %endif
%else
    %ifdef token2
        S -> in : out3
    %else
        S -> in : out4
    %endif    
%endif
S -> in : out
"""
defines = [
    "%define token1\n%define token2\n",
    "%define token1\n",
    "%define token2\n",
    "",
]
for define in defines:
    parser = Parser()
    print(define)
    parser.load_grammar(text=define + grammar)
    print(parser.format_rules())
예제 #16
0
VP -> Ved  : Ved  -dH
VP -> Ven  : Ven  -mHş
%macro V -> V,Vs,Ving,Ved,Ven
%form  V -> turn,turns,turning,turned,turned
$V -> $turn          : dön
$V -> $turn Obj      : Obj çevir
$V -> $turn Obj down : Obj reddet
$V -> $turn down Obj : Obj reddet
Subj -> he
Obj  -> it : onu
"""

try:
    parser = Parser() # initialize parser object

    parser.load_grammar(text=grammar) # load grammar from a file
    sent = "he turned it down" # sentence to parse

    print(parser.format_rules())

    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.replace(" -","")) # concat suffixes
except GrammarError as ge:
예제 #17
0
    def setUp(self):
        parser = Parser()

        parser.load_grammar(text=self.grammar, reverse=self.reverse)
        parser.compile()
        self.parser = parser
예제 #18
0
%form V -> watch,watches,watching,watched,watched
%form V -> go,goes,going,went,gone

%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)