Beispiel #1
0
def hgvs_parser(description, convert_to_model, grammar_file, start_rule,
                save_png):
    """
    Parse the HGVS description.

    :param description: HGVS description.
    :param grammar_file: Path towards grammar file.
    :param start_rule: Root rule for the grammar.
    :param save_png: Save parse tree as png.
    :param convert_to_model:
    """
    if grammar_file:
        parser = HgvsParser(grammar_path=grammar_file, start_rule=start_rule)
    else:
        parser = HgvsParser()
    try:
        parse_tree = parser.parse(description)
    except ParseError as e:
        print(e)
    else:
        if parse_tree is not None:
            if convert_to_model:
                model = parse_tree_to_model(parse_tree)
                print(json.dumps(model['model'], indent=2))
                if save_png:
                    pydot__tree_to_png(parse_tree, save_png)
            else:
                print('Successfully parsed HGVS description:\n %s' %
                      description)
                if save_png:
                    pydot__tree_to_png(parse_tree, save_png)
                    print('Parse tree image saved to:\n %s ' % save_png)
Beispiel #2
0
    def test():
        tree = EvalExpressions().transform(
            l.parse(
                "output = tf.nn.conv2d(input_op, kernel,(1,dh,dw,1),padding='SAME')"
            ))

        pydot__tree_to_png(tree, "conv2d.png")
Beispiel #3
0
 def visualize(self, png_path=None, show=False):
     from lark.tree import pydot__tree_to_png
     png_path = png_path or "/tmp/cell.png"
     pydot__tree_to_png(self.tree, png_path)
     if show:
         import subprocess
         subprocess.call(['open', png_path])
Beispiel #4
0
def visualize_lark(string: str, parser: Lark, path2filename: Union[str, Path]):
    if type(path2filename) is str:
        path2filename = Path(path2filename).expanduser()
    else:
        path2filename = path2filename.expanduser()
    ast = parser.parse(string)
    tree.pydot__tree_to_png(ast, path2filename)
Beispiel #5
0
def parse(query, full_tree=False, create_png=False):
    ast = parser.parse(query)
    if create_png:
        filename = create_filename(query)
        tree.pydot__tree_to_png(ast, filename)
    if full_tree:
        return ast
    return RomanParser().transform(ast)
Beispiel #6
0
    def get_tree(self, input, destination=None):
        """Generates readable representation of the input
        optionally can save graph into a PNG file"""

        tree = self._get_tree(input)
        out = tree.pretty(indent_str=' ')

        if destination:
            pydot__tree_to_png(tree, destination)

        return out
Beispiel #7
0
def make_png(
    grounding
):  # make a visual png of the AST for a given grounding. requires pydot package.
    try:
        parser = Lark(open('mod/ltl.lark').read(),
                      start='ltl',
                      ambiguity='explicit')
    except FileNotFoundError:
        parser = Lark(open('./ltl.lark').read(),
                      start='ltl',
                      ambiguity='explicit')
    tree.pydot__tree_to_png(parser.parse(grounding), './parse.png')
Beispiel #8
0
def parse_idl_string(idl_string, png_file=None):
    tree = get_ast_from_idl_string(idl_string)
    content = extract_content_from_ast(tree)

    if png_file:
        os.makedirs(os.path.dirname(png_file), exist_ok=True)
        try:
            pydot__tree_to_png(tree, png_file)
        except ImportError:
            pass

    return content
Beispiel #9
0
def parse_idl_string(idl_string, png_file=None):
    tree = get_ast_from_idl_string(idl_string)
    content = extract_content_from_ast(tree)

    if png_file:
        os.makedirs(os.path.dirname(png_file), exist_ok=True)
        try:
            pydot__tree_to_png(tree, png_file)
        except ImportError:
            pass

    return content
Beispiel #10
0
def main(s, out_fn):
    graphviz_setup()
    project_root = os.path.normpath(os.path.join(os.path.dirname(__file__), "../../"))
    fld = os.path.normpath(project_root + "./mappyfile")
    gf = os.path.join(fld, "mapfile.lalr.g")
    grammar_text = open(gf).read()

    g = Lark(grammar_text, parser="lalr", lexer="contextual")
    t = g.parse(s)
    print(t)
    pydot__tree_to_png(t, os.path.join(project_root, "docs/images", out_fn))
    print(t.pretty())
Beispiel #11
0
def main(s, out_fn):
    graphviz_setup()
    project_root = os.path.normpath(
        os.path.join(os.path.dirname(__file__), "../../"))
    fld = os.path.normpath(project_root + "./mappyfile")
    gf = os.path.join(fld, "mapfile.lalr.g")
    grammar_text = open(gf).read()

    g = Lark(grammar_text, parser="lalr", lexer="contextual")
    t = g.parse(s)
    print(t)
    pydot__tree_to_png(t, os.path.join(project_root, "docs/images", out_fn))
    print(t.pretty())
Beispiel #12
0
def parse_idl_string(idl_string, png_file=None):
    global parser
    tree = parser.parse(idl_string)

    if png_file:
        try:
            from lark.tree import pydot__tree_to_png
        except ImportError:
            pass
        else:
            os.makedirs(os.path.dirname(png_file), exist_ok=True)
            pydot__tree_to_png(tree, png_file)

    return extract_content_from_ast(tree)
Beispiel #13
0
def f_compile(text, file_name=None, debug=0) -> CodeType:
    if file_name is None:
        try:
            file_name = text.name
        except AttributeError:
            file_name = "<unknown>"
    try:
        text = text.read()
    except AttributeError:
        pass
    tree = f.parse(text)
    if debug > 0:
        from lark.tree import pydot__tree_to_png
        pydot__tree_to_png(tree, 'debug.png')
    tree = FLarkTransformer(FASTTransformer()).transform(tree)
    co = compile(tree, file_name, 'exec', dont_inherit=False)
    return co
Beispiel #14
0
def add_mismatch_offsets(
    netlist_in: Union[Path, str],
    netlist_out: Optional[Union[Path, str]] = None,
    debug: bool = False,
) -> None:
    if isinstance(netlist_in, str):
        netlist_in = Path(netlist_in)

    if netlist_in.suffix in ['.cdl', '.sp', '.spf']:
        parser = Lark(grammar_cdl, parser='lalr')
        scs = False
    elif netlist_in.suffix in ['.scs', '.net']:
        parser = Lark(grammar_scs, parser='lalr')
        scs = True
    else:
        raise ValueError(
            f'Unknown netlist suffix={netlist_in.suffix}. Use ".cdl" or ".scs".'
        )

    lines = read_spectre_cdl_unwrap(netlist_in)

    lines[-1] += '\n'
    tree = parser.parse('\n'.join(lines))

    if debug:
        pydot__tree_to_png(tree, "test0.png")
    obj_list = CktTransformer().transform(tree).children
    obj_list[-1].last = True

    if netlist_out is None:
        netlist_out: Path = netlist_in.with_name(netlist_in.stem + 'out')
    if isinstance(netlist_out, str):
        netlist_out: Path = Path(netlist_out)
    full_netlist = ''
    used_names = []
    offset_map = {}
    for obj in obj_list:
        full_netlist += obj.netlist(used_names, offset_map, scs)
    for key, val in offset_map.items():
        print(f'{val}: 0.0')

    with open_file(netlist_out, 'w') as f:
        f.write(full_netlist)
Beispiel #15
0
def run_earley_parser(sentence, word_sentence, counter, variant, label,
                      directory):
    if '#unknown' in sentence:
        # Unknown token cannot be resolved into valid tree
        return None

    try:
        sentence_wo_floskule = [x for x in sentence if x != "#floskule"]
        parse_tree = PARSER.parse(" ".join(sentence_wo_floskule))
        #        print(sentence)
        #        print(parse_tree.pretty())

        # Map empty tokens to the sentence
        # @note Currently, only single lemma enters sentence, so situation is quite simple
        expanded_sentence = []
        word_counter = 0
        for t in get_tokens_from_tree(parse_tree):
            word = ''
            if t.startswith('#'):
                word = word_sentence[word_counter]
                word_counter += 1
            expanded_sentence.append((word, t))

        print(expanded_sentence)

        larktree.pydot__tree_to_png(
            parse_tree,
            directory + '/sentence-{:03d}-{:02d}.png'.format(counter, variant),
            label=label + "\n" + " ".join(sentence))
        with open(
                directory +
                "/sentence-{:03d}-{:02d}.pretty".format(counter, variant),
                "w") as f:
            f.write(parse_tree.pretty())

    except Exception as e:
        LOGGER.info(e)
        LOGGER.info("Unable to create a tree for <%s>", (" ".join(sentence)))
        return False

    return True
Beispiel #16
0
 def image_tree(self, which="after"):
     if which == "after":
         pydot__tree_to_png(self.after_tree, "../tree-after.png")
     else:
         if which == "before":
             pydot__tree_to_png(self.after_tree, "../tree-before.png")
         else:
             pydot__tree_to_png(self.after_tree, "../tree-after.png")
Beispiel #17
0
    line = line.rstrip()
    if (len(line) > 0):
        count = 1
        items = line.split()
        word = items[0]
        if (len(items) > 1):
            if (items[0].isdigit()):
                count = items[0]
                word = items[len(items) - 1]
            else:
                count = items[len(items) - 1]
        tot += int(count)
        print("'" + line + "'")
        try:
            out_tree = lrk.parse(word)
            sys.stdout.write("SUCCESS:True " + word + " ")
            print(repr(out_tree).replace("Tree", ""))
            fname = word + "_" + gname + ".png"
            print(fname)
            tree.pydot__tree_to_png(out_tree, fname)
            success += int(count)
        except:
            print("Exception  " + line)
            #lrk.parse(word)

perc_succ = 100.0 * float(success) / float(tot)

print("TOT_WORDS: " + str(tot) + " TOT_SUCCESS: " + str(success) +
      " TOT_FAIL: " + str(tot - success))
print('SUCCESS%: ' + frmt(perc_succ) + "  FAIL%: " + frmt(100.0 - perc_succ))
Beispiel #18
0
    LE: "<="
    GT: ">"
    GE: ">="
    EQ: "=="
    NE: "!="

    ASSIGN: "="
    CHAR: "a".."z" | "A".."Z"

    %import common.SIGNED_INT
    %import common.CNAME -> VAR
    %import common.ESCAPED_STRING -> STR
    %import common.WS
    %import common.CPP_COMMENT

    %ignore WS
    %ignore CPP_COMMENT
'''

# TODO: update grammar to use lalr parser
parser = Lark(language_grammar)
parse = parser.parse

if __name__ == '__main__':
    import sys
    from lark import tree
    program = sys.argv[1]
    program = open(program, 'rt', encoding='utf-8').read()
    program = parse(program)
    tree.pydot__tree_to_png(program, 'program.png')
Beispiel #19
0
def _parse_tree_to_png(tree, file):
    from lark.tree import pydot__tree_to_png
    pydot__tree_to_png(tree, file)
Beispiel #20
0
 def saveTreeImg(self,tree):
     pydot__tree_to_png(tree, "syntaxTree.png")
Beispiel #21
0
    ID : /[_A-Za-z]([A-Za-z0-9_]*)/
    EQUALS : /=/
    TYPE : /int|float|double|boolean/

    %import common.NUMBER
    %import common.WS
    %ignore WS
"""

file = open('test.txt', 'r')

# Creacion del objeto que representa la gramatica
parser = Lark(grammar, start='inst')

i = 1
nameTREE = ''

# Leer el archivo linea por linea
for line in file:
    try:
        result = parser.parse(line.rstrip())
    except Exception:
        print("Error sintactico en linea " + str(i))
    else:
        print(result.pretty())
        nameTREE = 'arbol_' + str(i) + '.png'
        i += 1
        pydot__tree_to_png(result, nameTREE)

file.close()
Beispiel #22
0
"""
Utility to check out the dice tree and generate images of graphs because pretty.

Test syntaxes:

1d20
1+1
4d6kh3
(1)
(1,)
(((1d6)))
4*(3d8kh2+9[fire]+(9d2e2+3[cold])/2)
(1d4, 2+2, 3d6kl1)kh1
((10d6kh5)kl2)kh1
"""

from lark import Lark
from lark.tree import pydot__tree_to_png

with open("d20/grammar.lark") as f:
    grammar = f.read()

parser = Lark(grammar, start='expr', parser='lalr')

while True:
    result = parser.parse(input())
    print(result.pretty())
    print(result)
    pydot__tree_to_png(result, 'tree.png')
Beispiel #23
0
			
			%import common.NUMBER
			'''

grammar2 = '''
			start: NUMBER "+" NUMBER | NUMBER  "-" NUMBER  | WORD
			WORD: " "*
			
			%import common.NUMBER
			'''
'''
SPACE: " "*
				STRING: [STRING]| WORD
'''
grammar_print = '''
			start: "print" SPACE STRING
			STRING: QUOTES /[A-Z a-z 0-9 //w]*/ QUOTES
			QUOTES: "\'"
			%import common.WORD
			SPACE: " "+
'''

parser = l(grammar_print)

inp = input("INP: ")
tree = parser.parse(inp)
print(tree)

from lark.tree import pydot__tree_to_png  # Just a neat utility function
pydot__tree_to_png(tree, "examples/fruitflies.png")
Beispiel #24
0
def save_diagram(tree, filename, show=False):
    pydot__tree_to_png(tree, filename)
    if show:
        return Image(filename)
Beispiel #25
0
def make_png(data):
    tree.pydot__tree_to_png(data, "out.png")
Beispiel #26
0
 def make_png(self, sentence, filename):
     tree.pydot__tree_to_png(sentence, filename)
Beispiel #27
0
def make_png(filename):
    tree.pydot__tree_to_png(parser.parse(sentence), filename)
Beispiel #28
0
def make_png(parsed_tree, filename):
    pydot__tree_to_png(parsed_tree, filename)
Beispiel #29
0
 def plot_tree(self, filename: str = 'tree'):
     pydot__tree_to_png(self.tree, filename + ".png")
Beispiel #30
0
import os
from lark import Lark

from lark.tree import pydot__tree_to_png

GVIZ_PATH = r"C:\Program Files (x86)\Graphviz2.38\bin"


def graphviz_setup():
    os.environ['PATH'] = GVIZ_PATH + ';' + os.environ['PATH']


graphviz_setup()

fld = os.path.normpath(
    os.path.join(os.path.dirname(__file__), "../.././mappyfile"))
gf = os.path.join(fld, "mapfile.g")
grammar_text = open(gf).read()

g = Lark(grammar_text, parser='earley', lexer='standard')
t = g.parse("MAP NAME 'Test' END")
print(t)
pydot__tree_to_png(t, r"C:\temp\tree.png")
print(t.pretty())
Beispiel #31
0
 def make_ast_image(self, filename) -> None:
     '''Cria uma imagem da AST gerada'''
     if (self._tree == None) :
         raise Exception("Parser tree is not exist.")
     tree.pydot__tree_to_png(self._tree, filename+".png")
Beispiel #32
0
pngs_folder = os.path.join(str(current_dir), 'ast_pngs/')

def create_filename(query):
    f = query.replace(":", "_colon_")
    f = f.replace("=", "_equal_")
    f = f.replace(">", "_gt_")
    f = f.replace("/", "_slash_")
    f = f.replace("|", "_pipe_")
    f = f.replace("?", "_questionmark_")
    f = f.replace("#", "_sharp_")
    f = f.replace("+", "_plus_")
    f = f.replace("(", "_parenthesisl_")
    f = f.replace(")", "_parenthesisr_")
    f = f.replace("[", "_bracketl_")
    f = f.replace("]", "_bracketr_")
    return pngs_folder + f + ".png"

def parse(query, full_tree=False, create_png=False):
    ast = parser.parse(query)
    if create_png:
        filename = create_filename(query)
        tree.pydot__tree_to_png(ast, filename)
    if full_tree:
        return ast
    return RomanParser().transform(ast)

if __name__ == '__main__':
    ast = parse(sys.argv[1], full_tree=True)
    print(RomanParser().transform(ast))
    tree.pydot__tree_to_png(ast, 'ast_pngs/harmalysis_roman.png')