[英]How to build trie tree to solve this parse algorithm
我正在嘗試使用特里樹來解決這個問題:
Symbol string generator consists of two parts, a set of the start symbol and a set of rules of generation.
For example:
Start symbol: ['S'], Rules of generation: ["S → abc", "S → aA", "A → b", "A → c"]
Then, symbolic string abc can be generated because S → abc.
Symbolic string ab can be generated because S → aA → ab.
Symbolic string abc can be generated because S → aA → ac.
Now, give you a symbolic string generator and a symbolic string, and you need to return True if the symbolic string can be generated, False otherwise
Example
Given generator = ["S -> abcd", "S -> Ad", "A -> ab", "A -> c"], startSymbol = S, symbolString = “abd”, return True.
explanation:
S → Ad → abd
Given generator = ["S → abc", "S → aA", "A → b", "A → c"], startSymbol = S, symbolString = “a”, return False
我發現此問題的關鍵是建立特里樹。 我試圖寫:
def build_trie(values): #value is like ['abc', 'Ad'...]
root = {}
for word in values:
current = root
is_end = False
for c in word:
if 'A' <= c <= 'Z':
vals = m[c] #m is a mapping of {'S': ['abc', 'Ad'], ...}
rs = build_trie(vals)
for k in rs:
if k not in current:
current[k] = rs[k]
else:
# stuck here...
pass
# temp = collections.defaultdict(dict)
# for d in (current[k], rs[k]):
# for k, v in d.items():
# if k in temp and k != '__end__':
# temp[k].update(v)
# else:
# temp[k] = v
# # current[k].update(rs[k])
# current[k] = temp[k]
is_end = True
else:
current = current.setdefault(c, {})
is_end = False
if not is_end:
current['__end__'] = '__end__'
return root
但是卡在其他部分上...還沒有弄清楚如何編寫這棵特里樹。 有什么線索嗎?
您可能要使用python中的多個解析器庫。 我使用了LARK解析器 。 他們給出了各種python解析器的比較。
在大學期間,我在C語言中實現了LALR(1)解析器。我想它的用處會更少。 我發現了一個Python有用的實現在這里 ,如果你想重新寫入整個分析器。 我還沒有測試該代碼的工作原理。
對於給定的語法,我使用LARK編寫了一個驗證器,如下所示。
from lark import Lark
import sys
grammar = """
start: "abcd"
| A "d"
A: "ab"
| "c"
"""
parser = Lark(grammar)
def check_grammer(word):
try:
parser.parse(word)
return True
except Exception as exception:
print exception
return False
word = sys.argv[1]
print check_grammer(word)
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.