Пример #1
0
        dot.edge(p_name, name)
        for n in node.params:
            see_node(name, n, dot)
        see_node(name, node.body, dot)
        for n in node.localvars:
            see_node(name, n, dot)
    elif type(node) == CompoundStmtNode:
        dot.node(name, str(node.kind))
        dot.edge(p_name, name)
        for n in node.stmts:
            see_node(name, n, dot)
    elif type(node) == IfStmtNode:
        dot.node(name, str(node.kind))
        dot.edge(p_name, name)
        see_node(name, node.cond, dot)
        see_node(name, node.then, dot)
        if not node.els is None:
            see_node(name, node.els, dot)

if __name__ == '__main__':
    path = './t1.c'
    l = Lexer(filename(path), read_file(path))
    l.lex()
    l.see_tokens()

    p = Parser(l.tokens)
    p.parse()

    see_ast(p.ast)

Пример #2
0
import sys
sys.path.append("..")
from lex import Lexer

def filename(path):
    index = str(path).rfind('/') + 1
    name = path[index:]
    return name

def read_file(path):
    with open(path, 'r') as f:
        return f.read()

if __name__ == '__main__':
    path = './t1.c'
    lexer = Lexer(filename(path), read_file(path))
    lexer.lex()
    lexer.see_tokens()