#!/usr/bin/env python3 # -*- coding: utf-8 -*- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # IMPORTS # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ from __future__ import annotations; from lark import Tree; from typing import List; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # GLOBALE KONSTANTEN # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # METHODEN # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def rekursiv_atoms(fml: Tree) -> List[str]: ## Herausforderung: schreibe diese Funktion! return []; def rekursiv_depth(fml: Tree) -> int: ## Herausforderung: schreibe diese Funktion! return 0; def rekursiv_length(fml: Tree) -> int: ## Herausforderung: schreibe diese Funktion! return 0; def rekursiv_parentheses(fml: Tree) -> int: ## Herausforderung: schreibe diese Funktion! return 0 def rekursiv_eval(fml: Tree, I: List[str]) -> int: teilfml = getTeilformeln(fml); if fml.data in ['atom', 'beliebig']: name = getText(fml); return 1 if (name in I) else 0; elif fml.data == 'wahr': return 1; elif fml.data == 'falsch': return 0; elif fml.data == 'negation': val1 = rekursiv_eval(teilfml[0], I); return 1 - val1; elif fml.data == 'konjunktion': val1 = rekursiv_eval(teilfml[0], I); val2 = rekursiv_eval(teilfml[1], I); return min(val1, val2); elif fml.data == 'disjunktion': val1 = rekursiv_eval(teilfml[0], I); val2 = rekursiv_eval(teilfml[1], I); return max(val1, val2); elif fml.data == 'implikation': val1 = rekursiv_eval(teilfml[0], I); val2 = rekursiv_eval(teilfml[1], I); return 0 if val1 == 1 and val2 == 0 else 1; raise Exception('Evaluation nicht möglich!'); # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # HILFSMETHODEN # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ def getText(fml: Tree) -> str: text = fml.children[0]; if isinstance(text, str): return text; raise Exception('Konnte Textinhalt nicht ablesen!'); def getTeilformeln(fml: Tree) -> List[Tree]: return [ part for part in fml.children if isinstance(part, Tree) and not part.data == 'junktor' ];