{ "info": { "author": "Patrik Valkovic", "author_email": "patrik.valkovic@hotmail.cz", "bugtrack_url": null, "classifiers": [], "description": "# grammpy-transforms [![Build Status](https://travis-ci.org/PatrikValkovic/grammpy-transforms.svg?branch=master)](https://travis-ci.org/PatrikValkovic/grammpy-transforms) [![Coverage Status](https://coveralls.io/repos/github/PatrikValkovic/grammpy-transforms/badge.svg?branch=master)](https://coveralls.io/github/PatrikValkovic/grammpy-transforms?branch=master)\n\n-----\n\n## THIS PACKAGE IS DEPRECATED, USE `grammpy` LIBRARY INSTEAD.\n\n-------\nVersion: 1.2.5\n\nPackage for transforming grammpy grammars. \n\nCurrently only small subset of operations are implement.\n\nSubset of methods for each type of grammar will be stored in separate object (currently only ContextFree is supported).\n\nFor each method, you can decide to modify or copy the grammar. Default behaviour is to copy grammar before each modification.\nYou can disable it for every method by passing `transform_grammar=True` as parameter.\n\n\n## Regular grammars\n\nNot implemented.\n\n## Context-Free grammars\n\nOnly methods allowing to transform grammar into Chomsky normal form.\n\n#### Removing of useless symbols\n\nIncluding removing of unreachable symbols and symbols, thats not generate.\n\n```python\nfrom grammpy_transforms import ContextFree\n\nnew_g = ContextFree.remove_unreachable_symbols(g)\nnew_g = ContextFree.remove_nongenerating_nonterminals(g)\nnew_g = ContextFree.remove_useless_symbols(g)\nContextFree.is_grammar_generating(g)\n```\n\n#### Epsilon rules elimination\n\nMethod create new rules that will replace rules with nonterminal rewritable to epsilon rule.\nYou can also only search for nonterminals, that are in rule with epsilon on the right side.\n\n```python\nfrom grammpy import *\nfrom grammpy_transforms import ContextFree\n\nclass OldRules(Rule):\n rules = [([A], [B, C]), ([B], [EPS])]\n\nContextFree.find_nonterminals_rewritable_to_epsilon(g) # list of nonterminals\nnew_g = ContextFree.remove_rules_with_epsilon(g)\n\nclass NewRules(Rule):\n rules = [([A], [B, C]), ([A], [C])]\n \nassert new_g.have_rule(NewRules) is True\n```\n\nLibrary creating own type of rule, so you can backtrack the changes.\nThe type is `ContextFree.EpsilonRemovedRule`.\n\n```python\nclass CreatedRule(Rule):\n rule = ([A], [C])\n\ncreated = new_g.get_rule(CreatedRule)\nassert created.from_rule.rule == ([A], [B, C])\nassert created.replace_index == 0\nassert issubclass(created, ContextFree.EpsilonRemovedRule)\n```\n\n#### Removing of unit rules\n\nAs with epsilon rules removing, method for removing unit rules create new ones as so as removing invalid ones.\nYou can transform the grammar or just find reachable symbols.\n\n```python\nfrom grammpy import *\nfrom grammpy_transforms import ContextFree\n\nclass OldRules(Rule):\n rules = [([A], [B]), ([B], [C]), ([B], [0]), ([C], [1])]\n\nreach = ContextFree.find_nonterminals_reachable_by_unit_rules(g) # instance of ContextFree.UnitSymbolRechablingResults\nassert reach.reach(A, B) is True\nassert reach.reachables(A) == [A, B, C]\npath = reach.path_rules(B, C) # list of unit rules\nassert path[0].rule == ([B], [C])\n\nnew_g = ContextFree.remove_unit_rules(g)\n\nclass NewRules(Rule):\n rules = [([A], [0]), ([A], [1]), ([B], [0]), ([C], [1])]\n \nassert new_g.have_rule(NewRules) is True\n```\n\nMethod creates own type of rule (`ContextFree.ReducedUnitRule`) and you can backtrack the tranformations.\n\n```python\nclass Ato1Rule(Rule): \n rule=([A], [1])\n\ncreated = new_g.get_rule(Ato1Rule)\nassert created.by_rules[0].rule == ([A], [B])\nassert created.by_rules[1].rule == ([B], [C])\nassert created.end_rule.rule == ([C], [1])\n```\n\n#### Transformation to Chomsky normal form\n\nMethod transfer grammar into Chomsky normal form.\n\nThis operations create a lot of own types to allow easy backtracking of transformations.\n\nBase classes are `ContextFree.ChomskyNonterminal` and `ContextFree.ChomskyRule`, that are base classes for other.\n\nAs nonterminals method use `ContextFree.ChomskyTermNonterminal` that represent nonterminal rewritable to terminal (A->a). Nonterminal have property `for_term`, where it stores terminal (as Terminal class).\nSecond class is `ContextFree.ChomskyGroupNonterminal`, that represent group of symbols (for example in rule A->BCD will this nonterminal represent CD). This nonterminal have property `group`, where it stores list of symbols, that represent.\n\nFor rules method create list of classes, where each class have different meaning:\n- `ContextFree.ChomskySplitRule`: Represent rule, that was split to contain only two symbols. In property `from_rule` is stored original rule.\n- `ContextFree.ChomskyRestRule`: Represent right part after splitting of rule. As previous, in `from_rule` property is stored original rule. \nWhen splitting, ChomskySplitRule and ChomskyRestRule represent original whole rule: `A->BCDE ==> A->BX and X->CDE`.\n- `ContextFree.ChomskyTerminalReplaceRule`: This class is used in situations, where rule contains nonterminal with terminal. Rule is transformed into state, where terminal is replace with nonterminal rewritable to that terminal.\nClass have `from_rule` property that stores original rule and `replace_index` property, that indicate which terminal were replace.\n- `ContextFree.ChomskyTermRule`: It is class for rule, that directly rewrite nonterminal to terminal.\n\n### Inverse operations\n\nEliminating of epsilon rules, removing of unit rules and transforming into Chomsky normal form have their inverse operations.\nThey are implemented on `InverseContextFree` class.\n\nThat functions needs just root nonterminal of parsed tree. They then traverse the parse tree and replace rules created by transformations by their original equivalent.\n\nThe operations needs to be perform in opposite order, that transformations occurs.\n\n### Split rules\n\nBecause Grammar class split rules, which represents more than two rules,\nthere is need for algorithm, that replace splitted rule with the original one.\nThis algorithm is implemented on `InverseCommon` class as `splitted_rules` static method.\n\nThis call must be call as the last one. Also, you dont need to call this, if all of your rules have just single rule defined.\n\nAlgorithm is for now implemented only for Context-Free grammars.\n\nIn the following release of grammpy library, splitted rules should behave same as their original counterparts.\nThis method will than reflect it and may have empty implementation in the future.\n\n## Helpers\n\nLibrary provide from version 1.2.0 classes, that helps with parsed tree manipulation and traversing.\n\nClass `Manipulation` can replace specific rule, nonterminal or terminal with different one.\nThe new element will be added into parsed tree and correctly connected to rest of the elements.\n\n```python\nfrom grammpy_transforms import Manipulation\n# ...\nparsed = cyk(...)\nManipulation.replaceNode(parsed, MyNewNonterminal())\nManipulation.replaceRule(parsed.to_rule, MyNewRule())\n# or use type deductions\nManipulation.replace(parsed, MyNewNonterminal())\nManipulation.replace(parsed.to_rule, MyNewRule())\n```\n\nSecond class is `Traversing`. It contains static methods for post order and pre order traversing.\nMethods traverse throught nonterminals, terminals and even the rules. If you want to traverse just nonterminals, use the `filter` buildin function.\n\n```python\nfrom grammpy_transforms import Traversing\n# ...\nTraversing.postOrder(parsed)\nTraversing.preOrder(parsed)\n```\n\nYou can create your own traversing path by calling `traverse` static method.\nMethod accept root of the parsed tree and function accepting current traversing node and callback.\nPassed method must call callback with every node you want to traverse.\n\n```python\nfrom grammpy_transforms import Traversing\n# ...\ndef post_order_traversing(elem, callback):\n if isinstance(elem, Nonterminal):\n for ch in elem.to_rule.to_symbols:\n yield callback(ch)\n yield ch\n\nTraversing.traverse(parsed, post_order_traversing)\n```\n\nAlternatively, you can use `traverseSeparated` static method, that call different functions for nonterminals, terminals and rules.\n\n```python\ndef postOrder(root):\n def travRule(item, callback):\n resp = [callback(ch) for ch in item.to_symbols]\n return functools.reduce(operator.add, resp, []) + [item]\n def travNonterm(item, callback):\n return callback(item.to_rule) + [item]\n def travTerm(item, callback):\n return [item]\nreturn Traversing.traverseSeparated(root, travRule, travNonterm, travTerm)\n```\n\nClass Traverse also provide `print` static method, that returns string representing the structure of the AST.\n\n```text\n(R)ChomskySplitTempRule81\n|--(N)NoBracketExpression\n| `--(R)ChomskySplitRule20\n| |--(T)\n| `--(N)ChomskyGroupNonterminal20\n| `--(R)ChomskySplitTempRule20\n| |--(N)NoBracketExpression\n| | `--(R)ReducedSplitRules6\n| | |--(T)\n| | `--(N)ExpressionBody\n| | `--(R)SplitRules7\n| | `--(T)\n| `--(T)\n`--(T)\n```\n\n\n## Roadmap\n\nCurrently only small subset of operations are implemented.\n\nLibrary should at least support these operations:\n- Transform of grammar into valid LL(k) grammar\n- Removing of recursion\n- Transformations into another representation (like regular grammar to state machine), but this will be probably in separate package.\n\n-----\n\nVersion: 1.2.5\n\nAuthor: Patrik Valkovi\u010d\n\nLicence: GNU General Public License v3.0", "description_content_type": "text/markdown", "docs_url": null, "download_url": "https://github.com/PatrikValkovic/grammpy-transforms/archive/v1.2.5.tar.gz", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/PatrikValkovic/grammpy-transforms", "keywords": "", "license": "GNU General Public License v3.0", "maintainer": "", "maintainer_email": "", "name": "grammpy-transforms", "package_url": "https://pypi.org/project/grammpy-transforms/", "platform": "", "project_url": "https://pypi.org/project/grammpy-transforms/", "project_urls": { "Download": "https://github.com/PatrikValkovic/grammpy-transforms/archive/v1.2.5.tar.gz", "Homepage": "https://github.com/PatrikValkovic/grammpy-transforms" }, "release_url": "https://pypi.org/project/grammpy-transforms/1.2.5/", "requires_dist": null, "requires_python": "", "summary": "Set of transformations for grammpy library.", "version": "1.2.5" }, "last_serial": 4959706, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "9f9a94371b7f0031c805dd8ee88cff59", "sha256": "7de47cf2cd060bbf15a101340cfb955190c2edd66a4fbe32d8df3f04961a6796" }, "downloads": -1, "filename": "grammpy-transforms-1.0.0.tar.gz", "has_sig": false, "md5_digest": "9f9a94371b7f0031c805dd8ee88cff59", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5232, "upload_time": "2017-08-28T19:41:40", "url": "https://files.pythonhosted.org/packages/07/1c/0ee66ca38f8128ce20a79337cf74c864291cb27d7e68c7c1ca6abfec56b0/grammpy-transforms-1.0.0.tar.gz" } ], "1.1.0": [ { "comment_text": "", "digests": { "md5": "767a90eeb836215fddd557b8c1653972", "sha256": "9e400d6b671c83825eff2fbece16ca6baa02dbc43a00d466af71db9917fb036e" }, "downloads": -1, "filename": "grammpy-transforms-1.1.0.tar.gz", "has_sig": false, "md5_digest": "767a90eeb836215fddd557b8c1653972", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6558, "upload_time": "2017-09-03T17:41:55", "url": "https://files.pythonhosted.org/packages/07/04/1ccaa362d75db595538fd78fff344c98aae803f7e131ef042198d528178f/grammpy-transforms-1.1.0.tar.gz" } ], "1.2.0": [ { "comment_text": "", "digests": { "md5": "64317af4e7f15a11902ea5d8ab93fdaf", "sha256": "1f7bf2bf73c64897d85c04654310f0b9a84458e754cb888e074d8f38b9dad144" }, "downloads": -1, "filename": "grammpy-transforms-1.2.0.tar.gz", "has_sig": false, "md5_digest": "64317af4e7f15a11902ea5d8ab93fdaf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7294, "upload_time": "2018-03-31T11:49:15", "url": "https://files.pythonhosted.org/packages/4d/da/678842d19d5831d43a1dbe01f02a21da871a67dde35190ea42324062c86e/grammpy-transforms-1.2.0.tar.gz" } ], "1.2.1": [ { "comment_text": "", "digests": { "md5": "94981eba3a57a678dfc2c1f5c72c6ebe", "sha256": "b017e313124e9ecfe36438eb7a11afbe0aac8986dacfa488301281992a1aab3f" }, "downloads": -1, "filename": "grammpy-transforms-1.2.1.tar.gz", "has_sig": false, "md5_digest": "94981eba3a57a678dfc2c1f5c72c6ebe", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7294, "upload_time": "2018-03-31T12:56:30", "url": "https://files.pythonhosted.org/packages/f5/85/b10089637814e744deaf790702f673f64b0bf3238d4e413a0901f8296307/grammpy-transforms-1.2.1.tar.gz" } ], "1.2.2": [ { "comment_text": "", "digests": { "md5": "621d0f33cbbc8555470fa5b5af3192b9", "sha256": "4098484352e8e1c611a4bb43bab6b1f53348e1b8ecb8a5382916771c89e52002" }, "downloads": -1, "filename": "grammpy-transforms-1.2.2.tar.gz", "has_sig": false, "md5_digest": "621d0f33cbbc8555470fa5b5af3192b9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7463, "upload_time": "2018-03-31T14:07:02", "url": "https://files.pythonhosted.org/packages/af/ae/11cf0951288521a99358a3a89869c6ac4b2d8611a0b5d022c01cabd8c53f/grammpy-transforms-1.2.2.tar.gz" } ], "1.2.3": [ { "comment_text": "", "digests": { "md5": "8d5c62c2477646faf89c997d182f40c1", "sha256": "b08e83ca610f02e6d8c72cb62e97f9984610cab22d558e99ce2e81b1c08b01ba" }, "downloads": -1, "filename": "grammpy-transforms-1.2.3.tar.gz", "has_sig": false, "md5_digest": "8d5c62c2477646faf89c997d182f40c1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7682, "upload_time": "2018-04-01T13:37:13", "url": "https://files.pythonhosted.org/packages/12/05/d24c2de8176791f44612592577272e62c00f59384a58a97bac382519a03c/grammpy-transforms-1.2.3.tar.gz" } ], "1.2.4": [ { "comment_text": "", "digests": { "md5": "0d2adcd59643dd39832b9873d8151fe4", "sha256": "4a0f66d9ed891464b4a2a452fe35c81931585ae639c4ad1ad068bc60f227d421" }, "downloads": -1, "filename": "grammpy-transforms-1.2.4.tar.gz", "has_sig": false, "md5_digest": "0d2adcd59643dd39832b9873d8151fe4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7679, "upload_time": "2018-04-01T14:15:45", "url": "https://files.pythonhosted.org/packages/5c/66/6fcf32430b9bcd8345d7f5190aa62eaa47ce5f62f1b2d0d99f742d9385b0/grammpy-transforms-1.2.4.tar.gz" } ], "1.2.5": [ { "comment_text": "", "digests": { "md5": "093ce834c3e2b313db45428a8d98e7af", "sha256": "2c329d888aaefe38ebabb7e3fbc96c3aba5b25dc90ed3c37734c3acdfdb5d2b2" }, "downloads": -1, "filename": "grammpy-transforms-1.2.5.tar.gz", "has_sig": false, "md5_digest": "093ce834c3e2b313db45428a8d98e7af", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16859, "upload_time": "2019-03-19T16:51:32", "url": "https://files.pythonhosted.org/packages/b0/c2/596dafbd8f5485c01771e79728204734535455ba09d76ba9e915a5f0b603/grammpy-transforms-1.2.5.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "093ce834c3e2b313db45428a8d98e7af", "sha256": "2c329d888aaefe38ebabb7e3fbc96c3aba5b25dc90ed3c37734c3acdfdb5d2b2" }, "downloads": -1, "filename": "grammpy-transforms-1.2.5.tar.gz", "has_sig": false, "md5_digest": "093ce834c3e2b313db45428a8d98e7af", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16859, "upload_time": "2019-03-19T16:51:32", "url": "https://files.pythonhosted.org/packages/b0/c2/596dafbd8f5485c01771e79728204734535455ba09d76ba9e915a5f0b603/grammpy-transforms-1.2.5.tar.gz" } ] }