Beispiel #1
0
def parse_equation(string):
    """ Parses a string into an Expression"""
    tokens = lex(string)
    lhs, rhs = split_equation(tokens)
    lhs = parse_expression(lhs)
    rhs = parse_expression(rhs)

    return Equation(lhs, rhs)
Beispiel #2
0
def evaluate(code):
    code = modules(code)
    return interpret(parse(lex(code)))
    return exp


def gram_else_exp(else_lex):
    global LineIndex
    LineIndex = else_lex[0].line
    exp = ElseExp()
    exp.exp_list = gram_exp_list(else_lex[1:])
    #exp.print(0)
    return exp


def gram_while_exp(while_lex):
    global LineIndex
    LineIndex = while_lex[0].line
    lex = while_lex[1:]
    exp = WhileExp()
    exp.comp_exp = gram_comp_exp(strip_parent(extract_comp_exp(lex)))
    exp.exp_list = gram_exp_list(extract_exp_list(lex))
    #exp.print(0)
    return exp


if __name__ == '__main__':
    #    if_lex = lex("test_grammer_if.c")
    #    gram_if_exp(if_lex)
    else_lex = lex("test_grammer_else.c")
    gram_else_exp(else_lex)
#    while_lex = lex("test_grammer_while.c")
#    gram_while_exp(while_lex)
Beispiel #4
0
from Lexer import lex
from Lexer import tableOfSymb
from Lexer import tableOfId, tableToPrint, tableOfConst, sourceCode, FSuccess

lex()
print('-' * 30)
print('tableOfSymb:{0}'.format(tableOfSymb))
print('-' * 30)

# номер рядка таблиці розбору/лексем/символів ПРОГРАМИ tableOfSymb
numRow = 1
postfixCode = []

# довжина таблиці символів програми
# він же - номер останнього запису
len_tableOfSymb = len(tableOfSymb)
toView = False
print(('len_tableOfSymb', len_tableOfSymb))

# Функція для розбору за правилом
# Program = program StatementList end
# читає таблицю розбору tableOfSymb


def postfixTranslator():
    # чи був успішним лексичний розбір
    if (True, 'Lexer') == FSuccess:
        return parseProgram()


def parseProgram():
Beispiel #5
0
def gen_tree(src_file_path):
    _lex = lex(src_file_path)
    return gram_exp_list(_lex[1:])
Beispiel #6
0

def extract_math_exp_r(lex):
    balance = 0
    count = 0
    for item in lex:
        count += 1
        if item.type == PARENT_L:
            balance += 1
        if item.type == PARENT_R:
            balance -= 1
        if balance == 0:
            return extract_math_exp_l(lex[count + 1:])


def gram_comp_exp(comp_lex):
    global LineIndex
    LineIndex = comp_lex[0].line
    lex = strip_semicolon(comp_lex)
    exp = CompExp()
    exp.opt = extract_opt(lex)
    exp.math_exp_l = gram_math_exp(extract_math_exp_l(lex))
    exp.math_exp_r = gram_math_exp(extract_math_exp_r(lex))
    #exp.print(0)
    return exp


if __name__ == '__main__':
    comp_lex = lex("test_grammer_comp.c")
    gram_comp_exp(comp_lex)
Beispiel #7
0
                else:
                    balance += 1
            if item.type == BRACE_R:
                balance -= 1
            if balance == 0:
                exp_list.append(gram_while_exp(lex[:index]))
                return lex[index:]
    if exp_type == TypeBreakExp:
        break_exp = BreakExp()
        exp_list.append(break_exp)
        return lex[2:]


def extract_each_exp(lex, exp_list):
    if lex:
        extract_each_exp(chop_save_return_remain(exp_list, lex, what_exp(lex)),
                         exp_list)


def gram_exp_list(lex_list):
    lex = strip_brace(lex_list)
    exp = ExpList()
    extract_each_exp(lex, exp.exp_list)
    #exp.print(0)
    return exp


if __name__ == '__main__':
    lex = lex("test_grammer_exp_list.c")
    gram_exp_list(lex)
Beispiel #8
0
from Lexer import lex
from Parser import CheckSyntax, Parser
from Stack_Machine import StackMachine
import sys
str = input('>>> ')
while str != 'exit':
    try:
        tokens = lex(str)
        print(tokens)
        parser = CheckSyntax(tokens)
        lang = parser.lang()
        print(lang)
        for char in lang.rpn:
            print(char[0], end='\t')
        print()
        for i in range(len(lang.rpn)):
            print(i, end='\t')
        print()
    except:
        print('Syntax error')
    try:
        sm = StackMachine(lang.rpn)
        sm.run()
        for var in sm.variables.items():
            print(var)
    except BaseException:
        pass
    str = input('>>> ')
sys.exit(0)
def err_handler(msg):
    print("Error: Line:", LineIndex)
    print(msg)
    exit(1)


def extract_symbol(lex):
    if lex[0].type == SYMBOL:
        return lex[0].name
    err_handler("Error: declare expression should have a symbol!")


def extract_math_exp(lex):
    return lex[2:]


def gram_decl_exp(decl_lex):
    global LineIndex
    LineIndex = decl_lex[0].line
    lex = strip_semicolon(decl_lex)[1:]
    exp = DeclExp()
    exp.symbol = extract_symbol(lex)
    exp.math_exp = gram_math_exp(extract_math_exp(lex))
    #exp.print(0)
    return exp


if __name__ == '__main__':
    decl_lex = lex("test_grammer_decl.c")
    gram_decl_exp(decl_lex)