2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 82e16eb0 authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

refactorings, AST-Transformations amended in MLW_compiler.py

parent f5965e58
......@@ -68,7 +68,7 @@ import types
from functools import reduce, partial
__version__ = '0.5.1' + '_dev' + str(os.stat(__file__).st_mtime)
__version__ = '0.5.2' + '_dev' + str(os.stat(__file__).st_mtime)
DEBUG = True
......@@ -1211,7 +1211,7 @@ class CompilerBase:
return compiler(node)
def full_compilation(source, parser_HQ, AST_transformations, compiler):
def full_compilation(source, parserHQ, AST_transformations, compiler):
"""Compiles a source in three stages:
1. Parsing
2. AST-transformation
......@@ -1221,7 +1221,7 @@ def full_compilation(source, parser_HQ, AST_transformations, compiler):
Args:
source (str): the input source for compilation
parser_HQ (ParserHQ): the ParserHeadquarter object
parserHQ (ParserHQ): the ParserHeadquarter object
AST_transformations (dict): a table that assigns AST transformation
functions to parser names (see function ASTTransform)
compiler (object): an instance of a class derived from `CompilerBase`
......@@ -1234,8 +1234,8 @@ def full_compilation(source, parser_HQ, AST_transformations, compiler):
"""
assert isinstance(compiler, CompilerBase)
syntax_tree = parser_HQ.parse(source)
DEBUG_DUMP_SYNTAX_TREE(parser_HQ, syntax_tree, compiler, ext='.cst')
syntax_tree = parserHQ.parse(source)
DEBUG_DUMP_SYNTAX_TREE(parserHQ, syntax_tree, compiler, ext='.cst')
errors = syntax_tree.collect_errors(clear=True)
assert errors or str(syntax_tree) == source
......@@ -1243,7 +1243,7 @@ def full_compilation(source, parser_HQ, AST_transformations, compiler):
# likely that error list gets littered with compile error messages
if not errors:
ASTTransform(syntax_tree, AST_transformations)
DEBUG_DUMP_SYNTAX_TREE(parser_HQ, syntax_tree, compiler, ext='.ast')
DEBUG_DUMP_SYNTAX_TREE(parserHQ, syntax_tree, compiler, ext='.ast')
result = compiler.compile__(syntax_tree)
errors.extend(syntax_tree.collect_errors(clear=True))
else:
......@@ -1421,7 +1421,6 @@ class EBNFCompiler(CompilerBase):
self.variables = set()
self.scanner_tokens = set()
self.definition_names = []
self.curr_name = str(None)
self.recursive = set()
self.root = ""
self.directives = {'whitespace': '\s*',
......@@ -1554,7 +1553,6 @@ class EBNFCompiler(CompilerBase):
node.add_error('A rule with name "%s" has already been defined.' %
rule)
try:
self.curr_name = '"' + rule + '"'
self.rules.add(rule)
defn = self.compile__(node.result[1])
if rule in self.variables:
......@@ -1611,22 +1609,12 @@ class EBNFCompiler(CompilerBase):
', '.join(list(EBNFCompiler.KNOWN_DIRECTIVES))))
return ""
def _current_name(self):
# if self.curr_name in {'', str(None)}:
# name = []
# else:
# name = ["name=" + self.curr_name]
name = [] # turn explicit names declaration off
self.curr_name = str(None)
return name
def non_terminal(self, node, parser_class):
"""Compiles any non-terminal, where `parser_class` indicates the Parser class
name for the particular non-terminal.
"""
name = self._current_name()
arguments = filter(lambda arg: arg,
[self.compile__(r) for r in node.result] + name)
[self.compile__(r) for r in node.result])
return parser_class + '(' + ', '.join(arguments) + ')'
def expression(self, node):
......@@ -1692,10 +1680,10 @@ class EBNFCompiler(CompilerBase):
return 'Token(' + ', '.join([node.result]) + ')'
def regexp(self, node):
name = self._current_name()
rx = node.result
name = []
if rx[:2] == '~/':
name = ['wL=' + WHITESPACE_KEYWORD] + name
name = ['wL=' + WHITESPACE_KEYWORD]
rx = rx[1:]
if rx[-2:] == '/~':
name = ['wR=' + WHITESPACE_KEYWORD] + name
......
......@@ -100,7 +100,7 @@ class MLWGrammar(ParserHeadquarter):
DATEI_ENDE = !/./
NIEMALS = /(?!.)/
"""
source_hash__ = "ae6d6896307766bb8965321636b0ed54"
source_hash__ = "d7afa7bb0037ee25c0cabfa6e5a956c6"
parser_initialization__ = "upon instatiation"
wsp__ = mixin_comment(whitespace=r'\s*', comment=r'#.*(?:\n|$)')
wspL__ = wsp__
......@@ -174,8 +174,10 @@ MLWTransTable = {
"LemmaVarianten":
[partial(remove_tokens, tokens={'VARIANTEN'}), flatten,
partial(remove_tokens, tokens={',', ';'})],
"LVariante, LVZusatz":
"LVariante, LVZusatz, Schreibweise":
[remove_expendables, reduce_single_child],
"SWVariante":
[remove_expendables, partial(remove_tokens, tokens={':'})],
"GrammatikPosition":
[partial(remove_tokens, tokens={'GRAMMATIK', ';'}), flatten],
"GrammatikVarianten":
......@@ -187,10 +189,10 @@ MLWTransTable = {
"Flexion, Verweis":
[remove_expendables, reduce_single_child],
"ArtikelKopf": no_transformation,
"SchreibweisenPosition": no_transformation,
"SchreibweisenPosition":
[partial(remove_tokens, tokens={'SCHREIBWEISE', ':'}),
flatten, partial(remove_tokens, tokens={','})],
"SWTyp": no_transformation,
"SWVariante": no_transformation,
"Schreibweise": no_transformation,
"BedeutungsPosition": no_transformation,
"Bedeutung": no_transformation,
"Bedeutungskategorie": no_transformation,
......
......@@ -72,68 +72,35 @@
</GrammatikPosition>
</LemmaPosition>
<SchreibweisenPosition>
<token__>
SCHREIBWEISE
</token__>
<SWTyp>
<token__>
script.
</token__>
</SWTyp>
<token__>
:
</token__>
<SWVariante>
<Schreibweise>
<token__>
vizreg-
</token__>
vizreg-
</Schreibweise>
<token__>
:
</token__>
<Verweis>
>>beleg_id_3
</Verweis>
</SWVariante>
<ZeroOrMore>
<Sequence>
<token__>
,
</token__>
<SWVariante>
<Schreibweise>
<token__>
festregel(a)
</token__>
</Schreibweise>
<token__>
:
</token__>
<Verweis>
>>beleg_id_4
</Verweis>
</SWVariante>
</Sequence>
<Sequence>
<token__>
,
</token__>
<SWVariante>
<Schreibweise>
<token__>
fezdregl(a)
</token__>
</Schreibweise>
<token__>
:
</token__>
<Verweis>
>>beleg_id5
</Verweis>
</SWVariante>
</Sequence>
</ZeroOrMore>
<SWVariante>
<Schreibweise>
festregel(a)
</Schreibweise>
<Verweis>
>>beleg_id_4
</Verweis>
</SWVariante>
<SWVariante>
<Schreibweise>
fezdregl(a)
</Schreibweise>
<Verweis>
>>beleg_id5
</Verweis>
</SWVariante>
</SchreibweisenPosition>
<BedeutungsPosition>
<Sequence>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment