From 038eb8a7541bb19dabeb5e952392f6154c686180 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Fri, 7 May 2021 08:18:50 +0200 Subject: [PATCH] =?UTF-8?q?master=20>=20master:=20auslagern=20+=20Entfernu?= =?UTF-8?q?ng=20von=20=C3=BCberfl=C3=BCssigen=20Methoden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- code/aussagenlogik/rekursion.py | 5 +-- code/aussagenlogik/schema.py | 77 +++++--------------------------- code/aussagenlogik/syntaxbaum.py | 69 ++++++++++++++++++++++++++++ code/main.py | 5 +-- 4 files changed, 83 insertions(+), 73 deletions(-) create mode 100644 code/aussagenlogik/syntaxbaum.py diff --git a/code/aussagenlogik/rekursion.py b/code/aussagenlogik/rekursion.py index ecbd5bf..2520d26 100644 --- a/code/aussagenlogik/rekursion.py +++ b/code/aussagenlogik/rekursion.py @@ -19,7 +19,6 @@ from aussagenlogik.schema import isLongConjunction; from aussagenlogik.schema import isDisjunction; from aussagenlogik.schema import isLongDisjunction; from aussagenlogik.schema import isImplication; -from aussagenlogik.schema import getName; from aussagenlogik.schema import SyntaxBaum; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -35,10 +34,10 @@ from aussagenlogik.schema import SyntaxBaum; def rekursiv_eval(fml: SyntaxBaum, I: List[str]) -> int: subfml = fml.children; if isAtom(fml): - name = getName(fml); + name = fml.expr; return 1 if (name in I) else 0; if isBeliebig(fml): - name = getName(fml); + name = fml.expr; return 1 if (name in I) else 0; elif isTrueSymbol(fml): return 1; diff --git a/code/aussagenlogik/schema.py b/code/aussagenlogik/schema.py index 778890b..0e73aa0 100644 --- a/code/aussagenlogik/schema.py +++ b/code/aussagenlogik/schema.py @@ -13,6 +13,8 @@ from lark import Tree; from typing import List; from typing import Union; +from aussagenlogik.syntaxbaum import SyntaxBaum; + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # GLOBALE KONSTANTEN # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -22,57 +24,15 @@ with open('aussagenlogik/grammar.lark', 'r') as fp: grammar = ''.join(fp.readlines()); lexerAussagenlogik = Lark(grammar, start='expr', regex=True); -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# KLASSE: Syntaxbaum -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -class SyntaxBaum(object): - expr: str; - kind: str; - children: List[SyntaxBaum]; - tree: Tree; - - def __init__(self, fml: Tree): - self.kind = fml.data; - if len(fml.children) == 1 and isinstance(fml.children[0], str): - self.expr = fml.children[0]; - self.children = []; - self.tree = Tree(self.kind, fml.children); - else: - self.children = [ SyntaxBaum(child) for child in fml.children if isinstance(child, Tree) and child.data != 'symb' ]; - self.tree = Tree(self.kind, [child.tree for child in self.children]); - signature_parts = []; - i = 0; - for teilfml in fml.children: - if isinstance(teilfml, str): - signature_parts.append(teilfml); - elif teilfml.data == 'symb': - signature_parts.append(getText(teilfml)); - else: - signature_parts.append('{{{}}}'.format(i)); - i += 1; - signature = ' '.join(signature_parts); - self.expr = signature.format(*self.children); - if self.kind in [ 'konj', 'konj_lang', 'disj', 'disj_lang', 'impl' ]: - self.expr = ('(' if self.expr.startswith('(') else '( ') + self.expr; - self.expr += (')' if self.expr.endswith(')') else ' )'); - return; - - def __str__(self): - return self.expr; - - def pretty(self): - return self.tree.pretty(indent_str='- '); - # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # METHODE: string -> Syntaxbaum # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -def stringToSyntaxbaum(u: str) -> SyntaxBaum: +def stringToSyntaxbaum(expr: str) -> SyntaxBaum: try: - return SyntaxBaum(lexerAussagenlogik.parse(u)); + return SyntaxBaum(lexerAussagenlogik.parse(expr)); except: - raise Exception('Ausdruck \033[1m{}\033[0m konnte nicht erkannt werden!'.format(u)); + raise Exception('Ausdruck \033[1m{}\033[0m konnte nicht erkannt werden!'.format(expr)); # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # METHODEN: Erkennung von Formeltypen @@ -81,6 +41,9 @@ def stringToSyntaxbaum(u: str) -> SyntaxBaum: def isAtom(fml: SyntaxBaum) -> bool: return fml.kind == 'atom'; +def isLiteral(fml: SyntaxBaum) -> bool: + return isAtom(fml) or (isNegation(fml) and isAtom(fml.child)); + def isBeliebig(fml: SyntaxBaum) -> bool: return fml.kind == 'beliebig'; @@ -97,33 +60,13 @@ def isConjunction(fml: SyntaxBaum) -> bool: return fml.kind == 'konj'; def isLongConjunction(fml: SyntaxBaum) -> bool: - return fml.kind == 'konj_lang'; + return fml.kind in ['konj', 'konj_lang']; def isDisjunction(fml: SyntaxBaum) -> bool: return fml.kind == 'disj'; def isLongDisjunction(fml: SyntaxBaum) -> bool: - return fml.kind == 'disj_lang'; + return fml.kind == ['disj', 'disj_lang']; def isImplication(fml: SyntaxBaum) -> bool: return fml.kind == 'impl'; - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# METHODEN: Formel -> Textinhalt -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -def getText(fml: Tree) -> str: - text = fml.children[0]; - if isinstance(text, str): - return text; - raise Exception('Konnte Textinhalt nicht ablesen!'); - -def getName(fml: SyntaxBaum) -> str: - return fml.expr; - -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -# METHODEN: Formel -> Textinhalt -# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -def prettifyTree(fml: Union[Tree, SyntaxBaum]) -> str: - return fml.pretty(); diff --git a/code/aussagenlogik/syntaxbaum.py b/code/aussagenlogik/syntaxbaum.py new file mode 100644 index 0000000..c4d199d --- /dev/null +++ b/code/aussagenlogik/syntaxbaum.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# IMPORTS +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from __future__ import annotations; +from lark import Tree; +from typing import Generator; +from typing import List; + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# GLOBALE KONSTANTEN +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# KLASSE: Syntaxbaum +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +class SyntaxBaum(object): + expr: str; + kind: str; + children: List[SyntaxBaum]; + tree: Tree; + + def __init__(self, fml: Tree): + self.kind = fml.data; + if len(fml.children) == 1 and isinstance(fml.children[0], str): + self.expr = fml.children[0]; + self.children = []; + self.tree = Tree(self.kind, fml.children); + else: + self.children = [ SyntaxBaum(child) for child in fml.children if isinstance(child, Tree) and child.data != 'symb' ]; + self.tree = Tree(self.kind, [child.tree for child in self.children]); + signature_parts = []; + i = 0; + for subfml in fml.children: + if isinstance(subfml, str): + signature_parts.append(subfml); + elif subfml.data == 'symb': + symb = str(subfml.children[0]); + signature_parts.append(symb); + else: + signature_parts.append('{{{}}}'.format(i)); + i += 1; + signature = ' '.join(signature_parts); + self.expr = signature.format(*self.children); + if self.kind in [ 'konj', 'konj_lang', 'disj', 'disj_lang', 'impl' ]: + lbrace = '(' if self.expr.startswith('(') else '( '; + rbrace = ')' if self.expr.endswith(')') else ' )'; + self.expr = lbrace + self.expr + rbrace; + return; + + def __str__(self): + return self.expr; + + def __iter__(self) -> Generator[SyntaxBaum, None, None]: + for child in self.children: + yield child; + + @property + def child(self) -> SyntaxBaum: + return self.children[0]; + + def pretty(self): + return self.tree.pretty(indent_str='- '); diff --git a/code/main.py b/code/main.py index f84aa4c..8f4fcbb 100644 --- a/code/main.py +++ b/code/main.py @@ -15,9 +15,8 @@ from typing import List; sys.path.insert(0, os.getcwd()); -from aussagenlogik.schema import prettifyTree; from aussagenlogik.schema import stringToSyntaxbaum; -from aussagenlogik.schema import SyntaxBaum; +from aussagenlogik.syntaxbaum import SyntaxBaum; from aussagenlogik.rekursion import rekursiv_eval; from aussagenlogik.rekursion import rekursiv_atoms; from aussagenlogik.rekursion import rekursiv_depth; @@ -69,7 +68,7 @@ def display_results(expr: str, fml: SyntaxBaum, I: List[str], results: dict): F := \033[92;1m{F}\033[0m: '''.format(F=fml) )); - print(prettifyTree(fml)); + print(fml.pretty()); print(dedent( ''' Für I = [{I}] und F wie oben gilt