{"action":"created","issue":{"url":"https://api.github.com/repos/nltk/nltk/issues/1365","repository_url":"https://api.github.com/repos/nltk/nltk","labels_url":"https://api.github.com/repos/nltk/nltk/issues/1365/labels{/name}","comments_url":"https://api.github.com/repos/nltk/nltk/issues/1365/comments","events_url":"https://api.github.com/repos/nltk/nltk/issues/1365/events","html_url":"https://github.com/nltk/nltk/pull/1365","id":149030988,"number":1365,"title":"Raise warning for infinite recursion","user":{"login":"alvations","id":1050316,"avatar_url":"https://avatars.githubusercontent.com/u/1050316?v=3","gravatar_id":"","url":"https://api.github.com/users/alvations","html_url":"https://github.com/alvations","followers_url":"https://api.github.com/users/alvations/followers","following_url":"https://api.github.com/users/alvations/following{/other_user}","gists_url":"https://api.github.com/users/alvations/gists{/gist_id}","starred_url":"https://api.github.com/users/alvations/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alvations/subscriptions","organizations_url":"https://api.github.com/users/alvations/orgs","repos_url":"https://api.github.com/users/alvations/repos","events_url":"https://api.github.com/users/alvations/events{/privacy}","received_events_url":"https://api.github.com/users/alvations/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":5,"created_at":"2016-04-18T03:36:43Z","updated_at":"2016-04-19T03:34:28Z","closed_at":"2016-04-18T04:49:52Z","pull_request":{"url":"https://api.github.com/repos/nltk/nltk/pulls/1365","html_url":"https://github.com/nltk/nltk/pull/1365","diff_url":"https://github.com/nltk/nltk/pull/1365.diff","patch_url":"https://github.com/nltk/nltk/pull/1365.patch"},"body":"Patching #7\r\nRaises humanly-understandable warning for infinite recursion."},"comment":{"url":"https://api.github.com/repos/nltk/nltk/issues/comments/211712905","html_url":"https://github.com/nltk/nltk/pull/1365#issuecomment-211712905","issue_url":"https://api.github.com/repos/nltk/nltk/issues/1365","id":211712905,"user":{"login":"alvations","id":1050316,"avatar_url":"https://avatars.githubusercontent.com/u/1050316?v=3","gravatar_id":"","url":"https://api.github.com/users/alvations","html_url":"https://github.com/alvations","followers_url":"https://api.github.com/users/alvations/followers","following_url":"https://api.github.com/users/alvations/following{/other_user}","gists_url":"https://api.github.com/users/alvations/gists{/gist_id}","starred_url":"https://api.github.com/users/alvations/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alvations/subscriptions","organizations_url":"https://api.github.com/users/alvations/orgs","repos_url":"https://api.github.com/users/alvations/repos","events_url":"https://api.github.com/users/alvations/events{/privacy}","received_events_url":"https://api.github.com/users/alvations/received_events","type":"User","site_admin":false},"created_at":"2016-04-19T03:34:27Z","updated_at":"2016-04-19T03:34:27Z","body":"@nschneid If we're raising error for the recursion, I think raising `RuntimeError` to be consistent with the catch might be better than `StandardError`. \r\n\r\n**Proposal 1: With `StandardError`**\r\n\r\n```python\r\ndef _generate_all(grammar, items, depth):\r\n    if items:\r\n        try:\r\n            for frag1 in _generate_one(grammar, items[0], depth):\r\n                for frag2 in _generate_all(grammar, items[1:], depth):\r\n                    yield frag1 + frag2\r\n        except RuntimeError as _error:\r\n            if _error.message == \"maximum recursion depth exceeded\":\r\n                # Helpful error message while still showing the recursion stack.\r\n                raise StandardError(\"The grammar has rule(s) that yield infinite recursion!!\")\r\n            else:\r\n                raise\r\n    else:\r\n        yield []\r\n```\r\n\r\n[out]:\r\n\r\n\r\n```python\r\n>>> from nltk.parse import generate\r\n>>> generate.demo_grammar = \"\"\"\r\n... A -> A B | 'a'\r\n... B -> 'b'\r\n... \"\"\"\r\n>>> generate.demo()\r\nGenerating the first 23 sentences for demo grammar:\r\n\r\nA -> A B | 'a'\r\nB -> 'b'\r\n\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\n  File \"nltk/parse/generate.py\", line 79, in demo\r\n    for n, sent in enumerate(generate(grammar, n=N), 1):\r\n  File \"nltk/parse/generate.py\", line 42, in _generate_all\r\n    for frag1 in _generate_one(grammar, items[0], depth):\r\n  File \"nltk/parse/generate.py\", line 58, in _generate_one\r\n    for frag in _generate_all(grammar, prod.rhs(), depth-1):\r\n  File \"nltk/parse/generate.py\", line 42, in _generate_all\r\n    for frag1 in _generate_one(grammar, items[0], depth):\r\n  File \"nltk/parse/generate.py\", line 58, in _generate_one\r\n... (repeating errors)\r\n  File \"nltk/parse/generate.py\", line 48, in _generate_all\r\n    raise StandardError(\"The grammar has rule(s) that yield infinite recursion!!\")\r\nStandardError: The grammar has rule(s) that yield infinite recursion!!\r\n```\r\n\r\n----\r\n\r\n**Proposal 2: With `RuntimeError`**\r\n\r\n```python\r\ndef _generate_all(grammar, items, depth):\r\n    if items:\r\n        try:\r\n            for frag1 in _generate_one(grammar, items[0], depth):\r\n                for frag2 in _generate_all(grammar, items[1:], depth):\r\n                    yield frag1 + frag2\r\n        except RuntimeError as _error:\r\n            if _error.message == \"maximum recursion depth exceeded\":\r\n                # Helpful error message while still showing the recursion stack.\r\n                raise RuntimeError(\"The grammar has rule(s) that yield infinite recursion!!\")\r\n            else:\r\n                raise\r\n    else:\r\n        yield []\r\n```\r\n\r\n```python\r\n>>> from nltk.parse import generate\r\n>>> generate.demo_grammar = \"\"\"\r\n... A -> A B | 'a'\r\n... B -> 'b'\r\n... \"\"\"\r\n>>> generate.demo()\r\nGenerating the first 23 sentences for demo grammar:\r\n\r\nA -> A B | 'a'\r\nB -> 'b'\r\n\r\nTraceback (most recent call last):\r\n  File \"<stdin>\", line 1, in <module>\r\n  File \"nltk/parse/generate.py\", line 79, in demo\r\n    for n, sent in enumerate(generate(grammar, n=N), 1):\r\n  File \"nltk/parse/generate.py\", line 42, in _generate_all\r\n    for frag1 in _generate_one(grammar, items[0], depth):\r\n  File \"nltk/parse/generate.py\", line 58, in _generate_one\r\n    for frag in _generate_all(grammar, prod.rhs(), depth-1):\r\n  File \"nltk/parse/generate.py\", line 42, in _generate_all\r\n    for frag1 in _generate_one(grammar, items[0], depth):\r\n  File \"nltk/parse/generate.py\", line 58, in _generate_one\r\n... (repeating errors)\r\n  File \"nltk/parse/generate.py\", line 48, in _generate_all\r\n    raise RuntimeError(\"The grammar has rule(s) that yield infinite recursion!!\")\r\nRuntimeError: The grammar has rule(s) that yield infinite recursion!!\r\n```\r\n"}}