2021-05-05 11:07:32 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# IMPORTS
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
from __future__ import annotations;
|
|
|
|
from lark import Lark;
|
|
|
|
|
2021-05-07 08:18:50 +02:00
|
|
|
from aussagenlogik.syntaxbaum import SyntaxBaum;
|
|
|
|
|
2021-05-05 11:07:32 +02:00
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-06 12:44:29 +02:00
|
|
|
# GLOBALE KONSTANTEN
|
2021-05-05 11:07:32 +02:00
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2021-05-10 14:32:52 +02:00
|
|
|
# lexer durch LARK erzeugen
|
2021-05-10 15:55:00 +02:00
|
|
|
with open('grammars/aussagenlogik.lark', 'r') as fp:
|
2021-05-07 06:37:32 +02:00
|
|
|
grammar = ''.join(fp.readlines());
|
2021-05-10 14:32:52 +02:00
|
|
|
lexer = Lark(grammar, start='expr', regex=True);
|
2021-05-05 11:07:32 +02:00
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2021-05-06 15:29:44 +02:00
|
|
|
# METHODE: string -> Syntaxbaum
|
2021-05-05 11:07:32 +02:00
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2021-05-07 08:18:50 +02:00
|
|
|
def stringToSyntaxbaum(expr: str) -> SyntaxBaum:
|
2021-05-05 11:07:32 +02:00
|
|
|
try:
|
2021-05-10 14:32:52 +02:00
|
|
|
return SyntaxBaum(lexer.parse(expr));
|
2021-05-05 11:07:32 +02:00
|
|
|
except:
|
2021-05-07 08:18:50 +02:00
|
|
|
raise Exception('Ausdruck \033[1m{}\033[0m konnte nicht erkannt werden!'.format(expr));
|