{"id":16627108,"url":"https://github.com/ttulka/recursive-expressions","last_synced_at":"2025-10-08T01:45:57.101Z","repository":{"id":57730155,"uuid":"107458247","full_name":"ttulka/recursive-expressions","owner":"ttulka","description":"Java library for working with recursive expressions and context-free languages and grammars","archived":false,"fork":false,"pushed_at":"2018-02-05T19:35:42.000Z","size":193,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-08T01:45:56.632Z","etag":null,"topics":["context-free","context-free-grammar","context-free-language","expression","java","java-library","recursive-expressions"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ttulka.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-18T20:17:21.000Z","updated_at":"2018-01-17T09:25:45.000Z","dependencies_parsed_at":"2022-09-07T20:31:16.000Z","dependency_job_id":null,"html_url":"https://github.com/ttulka/recursive-expressions","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/ttulka/recursive-expressions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Frecursive-expressions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Frecursive-expressions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Frecursive-expressions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Frecursive-expressions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ttulka","download_url":"https://codeload.github.com/ttulka/recursive-expressions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ttulka%2Frecursive-expressions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278877093,"owners_count":26061380,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["context-free","context-free-grammar","context-free-language","expression","java","java-library","recursive-expressions"],"created_at":"2024-10-12T04:13:25.485Z","updated_at":"2025-10-08T01:45:57.067Z","avatar_url":"https://github.com/ttulka.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Recursive Expressions\n\nJava library for working with recursive expressions and context-free languages and grammars.\n\n## Prerequisites\n\n- Java 6\n\n## Characteristics\n\n- Not Thread-safe\n\n## Usage\n\nCopy the Maven dependency into your Maven project:\n\n```\n\u003cdependency\u003e\n    \u003cgroupId\u003ecz.net21.ttulka.recexp\u003c/groupId\u003e\n    \u003cartifactId\u003erecursive-expressions\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Recursive Expressions as an Extension of Regular Expressions \n\nRegular expression standard Java library does not use hierarchical grouping of a parsing result:\n```\nMatcher matcher = Pattern.compile(\"(a)((b))\").matcher(\"ab\");    // this is standard Java\nmatcher.groupCount();   // 3\nmatcher.group(1);       // a\nmatcher.group(2);       // b\nmatcher.group(3);       // b\n```\n\nWith Recursive Expressions are groups created hierarchically:\n```\nRecexpMatcher matcher = Recexp.compile(\"(a)((b))\").matcher(\"ab\");\n\nmatcher.groupCount();       // 2\n\nmatcher.group(1).name();    // a\nmatcher.group(1).value();   // a\n\nmatcher.group(2).name();    // (b)\nmatcher.group(2).value();   // b\n\nmatcher.group(2).groupCount();      // 1 \nmatcher.group(2).group(1).name();   // b\nmatcher.group(2).group(1).value();  // b\n```\n\nHierarchical expression tree from the example above:\n```\n    (a)((b))\n    /     \\\n   a      (b)\n            \\\n             b\n```\n\n#### Match flags\n\nRecursive Expressions are using the standard match flags from the `Pattern` class:\n```\nRecexp.compile(\"a\", Pattern.CASE_INSENSITIVE)    // case-insetive\n    .matches(\"A\");  // true\n```\nThe flags are applied to all the rules.\n\n### Recursive Expressions as a Context-Free Grammar\n\nA context-free grammar can be defined as a set of rules with a starting rule `S`. The rules are of the form `A → w`, where `A` is a name of the rule and `w` is a string which can contain characters and rule references.\n\n```\nRecexp grammar = Recexp.builder()\n    .rule(\"S\", \"@A@B\")\n    .rule(\"A\", \"a\")\n    .rule(\"B\", \"b\")\n    .build();\n    \nRecexpMatcher matcher = grammar.matcher(\"S\", \"ab\");\nmatcher.matches();  // true - the grammar accepts the input \"ab\"\n```\n\nThe *matcher* contains a result of a derivation from the starting rule. If the starting rule is omitted, each rule is a starting rule.\n\nWhen the rule name is omitted, the whole expression is used as a rule name and the rule cannot be referenced.\n\n**Tip:** *Use the convenience shortcut `Recexp.compile(rule1, ..., ruleN)` for defining a grammar with multiple anonymous rules.*  \n\n#### Recursive rule references\n\nRule expression can reference another rule and/or itself. \n\nReferences have syntax `@RefName` where `RefName` can contain only word characters (letters, digits and underscore `_`).\n\n```\nRecexp recexp = Recexp.builder()\n    .rule(\"MyRef\", \"@A@MyRef?@B\")\n    .rule(\"A\", \"a\")\n    .rule(\"B\", \"b\")\n    .build();\n    \nRecexpMatcher matcher = recexp.matcher(\"aabb\");\n    \nmatcher.matches();              // true\nmatcher.groupCount();           // 3    \n\nmatcher.group(1).name();        // @A\nmatcher.group(1).value();       // a   \n\nmatcher.group(2).name();        // @MyRef?\nmatcher.group(2).value();       // ab\n\nmatcher.group(3).name();        // @B\nmatcher.group(3).value();       // b\n```\n\n#### Self-reference `@this`\n```\nRecexp recexp = Recexp.compile(\"a@this|b\");\n\nrecexp.matches(\"b\");     // true\nrecexp.matches(\"ab\");    // true\nrecexp.matches(\"aab\");   // true\nrecexp.matches(\"aaab\");  // true\n\nrecexp.matcher(\"a\").matches();     // false\n```\n\n#### Empty reference `@eps` (Epsilon)\n\n*Epsilon* has syntax `@eps` and can be use as a rule defining an empty string:\n```\nRecexp recexp = Recexp.compile(\"a|@eps\");\n\nrecexp.matches(\"\");      // true\nrecexp.matches(\"a\");     // true\n```\n\nEpsilon is a shortcut for an empty rule:\n```\nRecexp recexp = Recexp.builder()\n    .rule(\"epsilon\", \"\")\n    ...\n```\n\n## Examples\n\n### Palindromes\n```\nS → 0S0 | 1S1 | 0 | 1 | ε \n```\n```\nRecexp recexp = Recexp.builder()\n    .rule(\"S\", \"0\")\n    .rule(\"S\", \"1\")\n    .rule(\"S\", \"0(@S)0\")\n    .rule(\"S\", \"1(@S)1\")\n    .rule(\"S\", \"@eps\")\n    .build();\n    \nrecexp.matches(\"\");        // true\nrecexp.matches(\"0\");       // true\nrecexp.matches(\"1\");       // true\nrecexp.matches(\"11\");      // true\nrecexp.matches(\"00\");      // true\nrecexp.matches(\"010\");     // true\nrecexp.matches(\"101\");     // true\nrecexp.matches(\"000\");     // true\nrecexp.matches(\"111\");     // true\nrecexp.matches(\"0110\");    // true\nrecexp.matches(\"1001\");    // true\nrecexp.matches(\"10101\");   // true\nrecexp.matches(\"10\");      // false\nrecexp.matches(\"01\");      // false\nrecexp.matches(\"1101\");    // false\n```\nThe same definition can be compactly created like:\n```\nRecexp recexp = Recexp.builder()\n    .rule(\"S\", \"0(@S)0|1(@S)1|0|1|@eps\")\n    .build();\n```\nOr alternatively by using the `@this` self-reference:\n```\nRecexp recexp = Recexp.compile(\n    \"0(@this)0|1(@this)1|0|1|@eps\");\n```\n\n### Strings with the same number of 0s and 1s\n```\nS → 0S1S | 1S0S | ε \n```\n```\nRecexp recexp = Recexp.builder()\n    .rule(\"S\", \"0(@S)1(@S)\")\n    .rule(\"S\", \"1(@S)0(@S)\")\n    .rule(\"S\", \"@eps\") \n    .build();\n    \nrecexp.matches(\"\");         // true\nrecexp.matches(\"0101\");     // true\nrecexp.matches(\"1010\");     // true\nrecexp.matches(\"1100\");     // true\nrecexp.matches(\"110010\");   // true\nrecexp.matches(\"110100\");   // true\nrecexp.matches(\"11000101\"); // true\nrecexp.matches(\"0\");        // false\nrecexp.matches(\"1\");        // false\nrecexp.matches(\"00\");       // false\nrecexp.matches(\"11\");       // false\nrecexp.matches(\"101\");      // false\nrecexp.matches(\"010\");      // false     \n```\nThe same definition can be compactly created like:\n```\nRecexp recexp = Recexp.builder()\n    .rule(\"S\", \"0(@S)1(@S)|1(@S)0(@S)|@eps\")\n    .build();\n```\nOr alternatively by using the `@this` self-reference:\n```\nRecexp recexp = Recexp.compile(\n    \"0(@this)1(@this)|1(@this)0(@this)|@eps\");\n```  \n\n### Arithmetic expressions over variables X and Y\n```\nE → E±T | T           (expressions)\nT → T×F | F           (terms)\nF → (E) | X | Y       (factors)\n```\n```\nRecexp recexp = Recexp.builder()\n    .rule(\"E\", \"@E±@T|@T\")\n    .rule(\"T\", \"@T×@F|@F\")\n    .rule(\"F\", \"\\\\(@E\\\\)|X|Y\")\n    .build();\n\nrecexp.matches(\"X±Y\");            // true\nrecexp.matches(\"X×Y\");            // true\nrecexp.matches(\"(X±X)×Y\");        // true\nrecexp.matches(\"(X±X)×(Y×X)\");    // true\n\nrecexp.matches(\"(X×X)(Y×X)\");     // false\n```\n\n### More examples\n\nFor more examples see [unit tests](http://github.com/ttulka/recursive-expressions/blob/master/src/test/java/cz/net21/ttulka/recexp/test/RecexpTest.java).\n\n## Release Changes\n\n### 1.0.0\nInitial version.\n\n## License\n\n[Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttulka%2Frecursive-expressions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fttulka%2Frecursive-expressions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fttulka%2Frecursive-expressions/lists"}