โมดูล Parsley เป็นโมดูลในภาษาไพทอนที่สามารถแยกวิเคราะห์และจัดรูปแบบได้โดยอาศัยอัลกอริทึม PEG (PEG algorithm) และได้นำ OMeta ( object-oriented pattern-matching language) ที่พัฒนาโดย Alessandro Warth มาใช้งาน อ่านรายละเอียดของ OMeta ได้ที่ http://tinlizzie.org/ometa/
รองรับทั้ง Python 2 และ Python 3
ใช้ MIT License
สามารถสั่งติดตั้งได้โดยใช้คำสั่ง pip:
pip install parsley
ตัวอย่างการใช้งาน
[python]
from parsley import makeGrammar
exampleGrammar = """
ones = '1' '1' -> 1
twos = '2' '2' -> 2
stuff = (ones | twos)+
"""
Example = makeGrammar(exampleGrammar, {})
g = Example("11221111")
result = g.stuff()
print(result)[/python]
ผลลัพธ์
[python][1, 2, 1, 1][/python]
ตัวอย่างการลองสร้างภาษาง่าย ๆ
[python]
from parsley import makeGrammar
Grammar = """
ones = 'echo ' <anything+>:s -> "print("+s+")"
"""
run = makeGrammar(Grammar, {})
g = run("echo 'hello'")
result = g.ones()
print(result)
print(eval(result))[/python]
ผลลัพธ์
[python]
print('hello')
hello
None[/python]
ลองนำไปประยุกต์ใช้งานกันดูนะครับ
อ่านเอกสารการใช้งานได้ที่ https://parsley.readthedocs.org/en/latest/index.html
ติดตามบทความต่อไปนะครับ
ขอบคุณครับ
0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)