{"id":22287582,"url":"https://github.com/botchris/slr-phparser","last_synced_at":"2025-03-25T20:46:55.268Z","repository":{"id":26549112,"uuid":"30002721","full_name":"botchris/slr-phparser","owner":"botchris","description":"A very simple LR(0) and LR(1) parser generator written in PHP.","archived":false,"fork":false,"pushed_at":"2015-05-11T11:16:46.000Z","size":224,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-01-30T18:30:46.698Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/botchris.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-29T03:58:20.000Z","updated_at":"2024-05-10T08:27:43.000Z","dependencies_parsed_at":"2022-08-21T08:20:24.634Z","dependency_job_id":null,"html_url":"https://github.com/botchris/slr-phparser","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/botchris%2Fslr-phparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/botchris%2Fslr-phparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/botchris%2Fslr-phparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/botchris%2Fslr-phparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/botchris","download_url":"https://codeload.github.com/botchris/slr-phparser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245543227,"owners_count":20632638,"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","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":[],"created_at":"2024-12-03T17:00:40.848Z","updated_at":"2025-03-25T20:46:55.229Z","avatar_url":"https://github.com/botchris.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# SLR PHParser\n\nA very simple LR(0) and LR(1) parser generator written in PHP.\n\n## Example\n\nSimple calculator with operator precedence:\n\n```php\nuse Phparser\\Grammar\\Grammar;\nuse Phparser\\Lexer\\Lexer;\nuse Phparser\\Parser\\Parser;\nuse Phparser\\Rule\\LR1\\RulesCollection;\n\n/**\n * This will be used to hold the result of the calculator.\n */\n$RESULT = 0;\n\n/**\n * Calculator's language definition.\n */\n$productions = new RulesCollection([\n    'S -\u003e exp_a' =\u003e function ($info) {\n        global $RESULT;\n        $info = $info[0];\n        $RESULT = $info;\n    },\n    'exp_a -\u003e exp_a T_PLS exp_a ' =\u003e function (\u0026$info) {\n       $info = $info[0] + $info[2];\n    },\n    'exp_a -\u003e exp_a T_SUB exp_a' =\u003e function (\u0026$info) {\n       $info = $info[0] - $info[2];\n    },\n    'exp_a -\u003e exp_a T_MUL exp_a' =\u003e function (\u0026$info) {\n        $info = $info[0] * $info[2];\n    },\n    'exp_a -\u003e exp_a T_DIV exp_a' =\u003e function (\u0026$info) {\n        $info = $info[0] / $info[2];\n    },\n    'exp_a -\u003e T_LP exp_a T_RP' =\u003e function (\u0026$info) {\n        $info = $info[1];\n    },\n    'exp_a -\u003e T_SQRT T_LP exp_a T_RP' =\u003e function (\u0026$info) {\n        $info = sqrt($info[2]);\n    },\n    'exp_a -\u003e T_NUM' =\u003e function (\u0026$info) {\n        $info = intval($info[0]-\u003evalue());\n    },\n]);\n\n/**\n * Tokens definition.\n */\n$tokens = [\n    '\\*' =\u003e 'T_MUL',\n    '\\/' =\u003e 'T_DIV',\n    '\\+' =\u003e 'T_PLS',\n    '\\-' =\u003e 'T_SUB',\n    'sqrt' =\u003e 'T_SQRT',\n    '[0-9]+' =\u003e 'T_NUM',\n    '\\(' =\u003e 'T_LP',\n    '\\)' =\u003e 'T_RP',\n    '\\s+|\\n+|\\t+' =\u003e '',\n];\n\n/**\n * Startup the parser.\n */\n$lexer = new Lexer($tokens);\n$grammar = new Grammar($productions);\n$parser = new Parser($lexer, $grammar);\n\n/**\n * Configure tokens precedence.\n */\n$grammar-\u003eprec('T_MUL', 4);\n$grammar-\u003eprec('T_DIV', 3);\n$grammar-\u003eprec('T_SUB', 2);\n$grammar-\u003eprec('T_PLS', 1);\n\n/**\n * The expression to parse.\n */\n$exp = '50 + 20 * 3 / 2 - 1';\n\n/**\n * Run the parser.\n */\n$parser-\u003erun($exp);\n\n/**\n * Draw parsing table.\n */\nrequire 'tests/config/drawer.php';\necho \"\u003ch2\u003eParsing table:\u003c/h2\u003e\";\necho draw_table($grammar-\u003etransitionTable()-\u003etoArray());\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbotchris%2Fslr-phparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbotchris%2Fslr-phparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbotchris%2Fslr-phparser/lists"}