Ejemplo n.º 1
0
    class ParseHandler(object):

        def __init__(self):
            self.ast = None
            self.exps = []

        def shift(self, token):
            print(colorful('移近 {0} {1}'.format(token[0], token[1]), "Cyan"))

        def reduce(self, grammar):
            """
            规约动作, grammar为规约用的产身世
            """
            print('利用规则' + colorful('%s -> %s' % (grammar[0], ' '.join(grammar[1])), "Green"))

    # from util import colorful
    lex = Lex()
    lex.keyword = ['lambda', '[', ']', 'let', 'define', 'if', 'cond', 'or', 'and', '(', ')']
    lex.read_lex('regular_lex.txt')
    lex.compile(grammar_type="regular")
    print(colorful('词法编译完成...', 'Yellow'))

    parser = LRParser()
    parser.read_grammar('schepy_grammar.txt')
    parser.compile()
    print(colorful('语法编译完成...', 'Yellow'))
    parser.show_dfa()

    while True:
        parser.parse(lex.lex(raw_input(), ignore=["limit"]), ParseHandler())
Ejemplo n.º 2
0
    readline.parse_and_bind("tab: complete")

import crash_on_ipy

crash_on_ipy.init()

from lex import Lex
from parser import LRParser
from util import colorful
from runtime import ParseHandler, Env

LEX = Lex()
LEX.keyword = ['lambda', '[', ']', 'let', 'define', 'if',
               'cond', 'or', 'and', '(', ')', '$T', '$F']
LEX.read_lex('regular_lex.txt')
LEX.compile(grammar_type="regular")
# lex.read_lex('regex_lex.txt')
# lex.compile()
print(colorful('词法编译完成...', 'Yellow'))
PARSER = LRParser()
PARSER.read_grammar('schepy_grammar.txt')
PARSER.compile()
print(colorful('语法编译完成...', 'Yellow'))
GLOBAL_ENV = Env.std_env()
while True:
    try:
        HANDLER = ParseHandler()
        EXP = raw_input(colorful('schepy => ', 'Cyan'))
        PARSER.parse(LEX.lex(EXP, ignore=["limit"]), HANDLER)
        print(';VALUE: ' +
              colorful(repr(HANDLER.ast.calc_value(env=GLOBAL_ENV)), "Magenta"))