#!/usr/bin/env python
"""
:Author Patrik Valkovic
:Created 01.08.2017 07:33
:Licence GNUv3
Part of grammpy
"""
import inspect
from ..Rules import Rule
from ..HashContainer import HashContainer
from ..Nonterminal import Nonterminal
from ..Terminal import Terminal
from ..exceptions import NotNonterminalException, NotRuleException, TerminalDoesNotExistsException, \
NonterminalDoesNotExistsException
class RawGrammar:
def __init__(self,
terminals=None,
nonterminals=None,
rules=None,
start_symbol=None):
terminals = [] if terminals is None else terminals
nonterminals = [] if nonterminals is None else nonterminals
rules = [] if rules is None else rules
self.__terminals = HashContainer()
self.__nonterminals = HashContainer()
self.__rules = HashContainer()
self.__start_symbol = None
self.add_term(terminals)
self.add_nonterm(nonterminals)
self.add_rule(rules)
self.start_set(start_symbol)
# Term part
# TODO add validation of terminals that no rule or nonterminal is passed
def add_term(self, term):
return [Terminal(t, self) for t in self.__terminals.add(term)]
def remove_term(self, term=None):
return [Terminal(t, self) for t in self.__terminals.remove(term)]
def have_term(self, term):
return self.__terminals.have(term)
def get_term(self, term=None):
if term is not None and not HashContainer.is_iterable(term):
item = self.__terminals.get(term)
return Terminal(item, self) if item is not None else None
vals = []
obtain = self.__terminals.get(term)
for t in obtain:
vals.append(Terminal(t, self) if t is not None else None)
return vals
def term(self, term=None):
return self.get_term(term)
def terms(self):
return [Terminal(term, self) for term in self.__terminals.all()]
def terms_count(self):
return self.__terminals.count()
# Non term part
@staticmethod
def _controll_nonterms(nonterms):
nonterms = HashContainer.to_iterable(nonterms)
for nonterm in nonterms:
if not inspect.isclass(nonterm) or not issubclass(nonterm, Nonterminal):
raise NotNonterminalException(nonterm)
return nonterms
def add_nonterm(self, nonterms):
nonterms = RawGrammar._controll_nonterms(nonterms)
return self.__nonterminals.add(nonterms)
def remove_nonterm(self, nonterms=None):
if nonterms is None:
return self.__nonterminals.remove()
nonterms = RawGrammar._controll_nonterms(nonterms)
return self.__nonterminals.remove(nonterms)
def have_nonterm(self, nonterms):
nonterms = RawGrammar._controll_nonterms(nonterms)
return self.__nonterminals.have(nonterms)
def get_nonterm(self, nonterms=None):
if nonterms is None:
return self.__nonterminals.get()
converted = RawGrammar._controll_nonterms(nonterms)
if not HashContainer.is_iterable(nonterms):
return self.__nonterminals.get(converted)[0]
return self.__nonterminals.get(converted)
def nonterm(self, nonterms=None):
return self.get_nonterm(nonterms)
def nonterms(self):
return self.__nonterminals.get()
def nonterms_count(self):
return self.__nonterminals.count()
# Rules part
def _control_rules(self, rules):
rules = HashContainer.to_iterable(rules)
for rule in rules:
if not inspect.isclass(rule) or not issubclass(rule, Rule):
raise NotRuleException(rule)
rule.validate(self)
return rules
def add_rule(self, rules):
rules = self._control_rules(rules)
return self.__rules.add(rules)
def remove_rule(self, rules=None):
if rules is None:
return self.__rules.remove()
rules = self._control_rules(rules)
return self.__rules.remove(rules)
def have_rule(self, rules):
try:
rules = self._control_rules(rules)
return self.__rules.have(rules)
except (TerminalDoesNotExistsException, NonterminalDoesNotExistsException):
return False
def get_rule(self, rules=None):
if rules is None:
return [rule for rule in self.__rules.get() if rule._active]
converted = self._control_rules(rules)
if not HashContainer.is_iterable(rules):
return self.__rules.get(converted)[0]
return self.__rules.get(converted)
def rule(self, rules=None):
return self.get_rule(rules)
def rules(self):
return self.rule()
def rules_count(self):
return len(self.rules())
# StartSymbol
def start_get(self):
return self.__start_symbol
def start_set(self, nonterminal):
if nonterminal is None:
self.__start_symbol = None
return
if not inspect.isclass(nonterminal) or not issubclass(nonterminal, Nonterminal):
raise NotNonterminalException(nonterminal)
if not self.have_nonterm(nonterminal):
raise NonterminalDoesNotExistsException(None, nonterminal, self)
self.__start_symbol = nonterminal
def start_isSet(self):
return self.__start_symbol is not None
def start_is(self, nonterminal):
return self.__start_symbol is nonterminal
挣扎的蓝藻
- 粉丝: 14w+
- 资源: 15万+
最新资源
- 家用干果机行业分析:2023年全球市场规模大约为1105百万美元.docx
- 卡车变电站市场分析:2023年全球市场规模大约为939百万美元.docx
- 抗炎治疗行业分析:全球抗炎治疗市场规模约为1137.4亿美元.docx
- 科研仪器行业市场:2023年全球市场规模大约为70930百万美元.docx
- 客户服务业务流程外包行业分析:全球收入约为587.6亿美元.docx
- 实验报告.pptx - 数据结构实验操作步骤与结果分析报告
- 蓝牙耳机电池行业分析:TWS耳机是最主要的应用领域.docx
- 客户关系管理软件行业分析:全球收入约为8444.7百万美元.docx
- 冷链监测行业市场分析:北美是全球最大的市场,占约26%份额.docx
- 轮胎动平衡市场分析:2023年全球市场规模大约为708百万美元.docx
- 硫酸镍行业分析:2023年全球市场规模大约为9041百万美元.docx
- 密密钥管理行业分析:北美和亚太占全球约75%的全球市场份额.docx
- 民意调查软件市场分析:北美是全球市场的主要地区,占约50%的市场份额.docx
- 企业评估服务市场分析:全球收入约为3425.4百万美元.docx
- 驱蚊产品市场分析:亚太是全球市场的主要地区.docx
- 汽车原始设备制造商工厂内物流行业分析:华东地区是中国最大的消费市场,约占30%.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈