def calculate(expr,strategy="LeftmostOutermost"): print "Verwende als Reduction-Strategie: "+strategy p = Parser(expr) e = p.parse() if e: print e.traverse() # result = e.betaReduce(pause=False,order="LeftmostOutermost") result = e.betaReduce(pause=False,order=strategy) # result = e.betaReduce(pause=False,order="HybridApplicative") print "\nErgebnis: \n"+result.traverse() else: print "Fehler beim Parsen!"
def main(): parser = Parser() parser.load_rules('rules/rules.txt.barosl') tree = parser.parse_file('input/sum.barosl') interm = IntermCodeGen(tree) for code in interm.code: print code print '----' code_gens = [LmcCodeGen, NasmCodeGen, HtmlCodeGen] code_gen = code_gens[1] code = code_gen(interm).get_code() print code.rstrip() print '----'
def set_rule_file(self, fpath): parser = Parser() parser.load_rules(fpath) self.parser = parser
from tokenizer import Tokenizer from parser2 import Parser source_code = '' with open('examples/main.stp') as stp: source_code = stp.read(1024) tokenizer = Tokenizer(source_code, True) parser = Parser(tokenizer) syntax_tree = parser.parse() if parser.current_level != 0: raise Exception('brackets error') print(syntax_tree)
def izbor(): running = 1 parser = Parser() trie = Trie(Element("KOREN", None)) g = None path = str() resSet = Set() flag = True while running == 1: print("(1)->Izbor direktorijuma") print("(2)->Prikaz trenutnog direktorijuma") print("(3)->Promena direktorijuma") #unesi apsolutnu putanju print("(4)->Unos upita") print("(5)->Kompleksna pretraga") print("(0)->Kraj programa") try: user_input = int(input(">>")) except ValueError: print("Unesite broj 0-5 iz ponudjenog menija.\n") continue if user_input == 1: if flag and len(path) != 0: print("Za promenu direktorijuma izaberite opciju 3!\n") continue path = os.path.abspath( input( "Unesite apsolutnu/relativnu putanju do željenog direktorijuma >> " )) g = kreiraj_graf(path, parser, trie) if g == False: print("Niste dobro uneli putanju direktorijuma\n") flag = False else: flag = True elif user_input == 2: if not flag or len(path) == 0: print("Neophodno je izabrati direktorijum!\n") continue print("Trenutni direktorijum:\n{}".format(path)) print("-" * len(path)) elif user_input == 3: if len(path) == 0 or not flag: print( "Trenutno niste pozicionirani ni na jednom direktorijumu!\n" ) continue new_path = os.path.abspath( input("Unesite putanju direktorijuma: ")) if os.path.exists(new_path) and os.path.isdir( new_path) and new_path != path: path = new_path os.chdir(new_path) print("Uspesna promena direktorijuma!") trie = Trie(Element("KOREN", None)) g = kreiraj_graf(path, parser, trie) else: print("Pogrešan unos!") print("Trenutni direktorijum:\n{}".format(path)) print("--" * 20) elif user_input == 4: if len(path) == 0 or not flag: print( "Trenutno niste pozicionirani ni na jednom direktorijumu!\n" ) continue else: unos = input("Unesite željeni upit:\n").lower() lista_reci = unos.split() if "and" in lista_reci: if len(lista_reci) == 3 and lista_reci[1] == "and": if lista_reci[0].lower() != lista_reci[2].lower(): recnik1 = trie.pretraga(lista_reci[0]) recnik2 = trie.pretraga(lista_reci[2]) res_and = resSet.operation_and(recnik1, recnik2) rang(res_and, g) res_sort = heap_sort(res_and) prikaz(res_sort) else: print( "\nUneli ste dve iste reci. Upit nije validan.\n" ) else: print("\nNiste dobro uneli upit.\n") elif "or" in lista_reci: if len(lista_reci) == 3 and lista_reci[1] == "or": if lista_reci[0].lower() != lista_reci[2].lower(): recnik1 = trie.pretraga(lista_reci[0]) recnik2 = trie.pretraga(lista_reci[2]) res_or = resSet.operation_or(recnik1, recnik2) res_rang = rang(res_or, g) res_sort = heap_sort(res_rang) prikaz(res_sort) else: print( "\nUneli ste dve iste reci. Upit nije validan.\n" ) else: print("\nNiste dobro uneli upit.\n") elif "not" in lista_reci: if len(lista_reci) == 3 and lista_reci[1] == "not": if lista_reci[0].lower() != lista_reci[2].lower(): recnik1 = trie.pretraga(lista_reci[0]) recnik2 = trie.pretraga(lista_reci[2]) res_not = resSet.operation_not(recnik1, recnik2) res_rang = rang(res_not, g) res_sort = heap_sort(res_rang) prikaz(res_sort) else: print( "\nUneli ste dve iste reci. Upit nije validan.\n" ) else: print("\nNiste dobro uneli upit.\n") elif len(lista_reci) > 0: indikator = False for rec in lista_reci: if lista_reci.count(rec) > 1: indikator = True break if indikator == False: recnik1 = trie.pretraga(lista_reci[0]) i = 1 while i < len(lista_reci): recnik2 = trie.pretraga(lista_reci[i]) recnik1 = resSet.operation_or(recnik1, recnik2) i += 1 if recnik1 == False: print("\nNema rezultata pretrage\n") else: rang(recnik1, g) res_sort = heap_sort(recnik1) prikaz(res_sort) else: print( "\nNiste dobro uneli upit, reci ne smeju da se ponavljaju.\n" ) else: print("\nNiste uneli reci za pretragu.\n") elif user_input == 5: new_input = str(input("Unesite željeni upit >>")) complex_function(g, trie, new_input) # myparser(newinput) elif user_input == 0: print("Program završen!") return else: print("Unesite broj 0-4 iz ponudjenog menija.")
def writeln(s): s.encode("utf-8") #n1 = Node('L',Node('V','x',None),None) #n1.right=Node('A',Node('V','x',None),Node('V','y',None)) #print n1.traverse() expr = u"λx y.(λa b.x a y b x z t)" expr1 = "(x y)" expr2 = u"(λc t)" expr3 = "<x <t <e (x t)>>>" expr4 = u"(λx (λy (λz ((F x) y))))" expr5 = r"(\x (\y ((F y) x)))" if False: p = Parser(expr3,True) e = p.parse() p2 = Parser(expr2) e2 = p2.parse() if e and e2: print e.traverse() print e.FV() print e2.traverse() e.right.substitute(e2,"x",e) print e.traverse() else: print "Fehler beim Parsen!" print e.FV()
from exceptions2 import SemanticError import Types class SemanticAnaliser: def __init__(self, ast_tree): self.ast_tree = ast_tree self.identifiers = {} def find_assigned_ids(self, ast_tree): for child in ast_tree.childs: if child.token.type == Types.Assign: self.identifiers[child.get_child( 0).token.value] = child.get_child(0).get_child( 0).token.value self.find_assigned_ids(child) def analise(self): self.find_assigned_ids(self.ast_tree) analiser = LexAnaliser('test_files/test.c') tokens = analiser.analise() #for token in tokens: # print(token) parser = Parser(tokens) tree = parser.program() semantic_analiser = SemanticAnaliser(tree) semantic_analiser.analise() for k, v in semantic_analiser.identifiers.items(): print(k, ' ', v)
def print_menu(): print("0. Exit") print("1. Show non terminals") print("2. Show terminals") print("3. Show productions") print("4. Production for a given nonterminal") print("5. First Set ") print("6. Follow set") print("7. Parse PIF") if __name__ == '__main__': grammar = Grammar.read_file("g1.txt") pif = [0, 1] parser = Parser(grammar, pif) parser_output = ParserOutput(parser) while True: print_menu() x = int(input()) if x == 1: print(grammar.get_non_terminals()) elif x == 2: print(grammar.get_terminals()) elif x == 3: print(grammar.get_productions()) elif x == 4: n = str(input("Enter non-terminal: ")) grammar.print_productions_for_given_nonterminal(n) elif x == 5: