{"id":26560769,"url":"https://github.com/jlguenego/syntax-analysis","last_synced_at":"2025-03-22T13:29:54.411Z","repository":{"id":57121505,"uuid":"327393694","full_name":"jlguenego/syntax-analysis","owner":"jlguenego","description":"Syntax analysis - parsers.","archived":false,"fork":false,"pushed_at":"2021-03-01T11:00:51.000Z","size":1951,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-12T19:46:38.341Z","etag":null,"topics":["compiler","cs143","grammar","lalr1","ll1","llk","lr0","lr1","parser","slr","syntax-analysis"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/jlguenego.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2021-01-06T18:13:00.000Z","updated_at":"2023-01-29T21:39:23.000Z","dependencies_parsed_at":"2022-08-24T06:30:56.606Z","dependency_job_id":null,"html_url":"https://github.com/jlguenego/syntax-analysis","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlguenego%2Fsyntax-analysis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlguenego%2Fsyntax-analysis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlguenego%2Fsyntax-analysis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlguenego%2Fsyntax-analysis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jlguenego","download_url":"https://codeload.github.com/jlguenego/syntax-analysis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244962513,"owners_count":20539184,"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":["compiler","cs143","grammar","lalr1","ll1","llk","lr0","lr1","parser","slr","syntax-analysis"],"created_at":"2025-03-22T13:29:53.565Z","updated_at":"2025-03-22T13:29:54.393Z","avatar_url":"https://github.com/jlguenego.png","language":"TypeScript","readme":"# Syntax Analysis\n\nSyntax analysis (compiler step just after lexer).\n\n## Install\n\n```\nnpm i @jlguenego/syntax-analysis\n```\n\n[![Code Style: Google](https://img.shields.io/badge/code%20style-google-blueviolet.svg)](https://github.com/google/gts)\n\n## Usage\n\nYou should use Typescript in order to check your grammar in a easier way.\n\n```ts\nconst t = defineTerminalAlphabet(['a', 'b'] as const);\nconst nt = defineNonTerminalAlphabet(['S', 'A'] as const);\n\nconst spec: CFGSpecifications\u003ctypeof nt, typeof t\u003e = {\n  nt,\n  t,\n  productions: [\n    {LHS: 'S', RHS: ['a', 'A', 'a', 'a']},\n    {LHS: 'S', RHS: ['b', 'A', 'b', 'a']},\n    {LHS: 'A', RHS: []},\n    {LHS: 'A', RHS: ['b']},\n  ],\n  startSymbol: 'S',\n};\nexport const cfg = new ContextFreeGrammar(spec);\n\n// coming from a lexer (ex: @jlguenego/lexer)\nconst sentence: Sentence = 'abaa'.split('').map(str =\u003e ({\n  name: str,\n}));\n\n// the real job: get the parse tree.\nconst parseTree = parse(sentence, cfg, {\n  method: 'LLk',\n  lookaheadTokenNbr: 2,\n});\n```\n\n## Top down algorithm\n\n### Breadth First Search\n\n- **BFS1**: Naive Breadth First Search with nothing else (very slow, may take many days...).\n- **BFS2**: Like BFS1 with 2 checks for speeding BFS (slow, may take many hours...).\n  - checks the length of sentential form\n  - checks the sentence prefix of the sentential form\n- **BFS3**: Like BFS2 with LeftMost Derivation strategy (not so slow, mak take some minutes...).\n\n### Depth First Search\n\n- **DFS1**: Leftmost derivation strategy. (not so slow execpt for left recursive grammar)\n- **DFS2**: Like DFS1 but use one lookahead terminal to speed up a little bit.\n\n- **LL1**: Like DFS2 but use a LL1 table to know exactly wich production rule to use for the next sentential form.\n  This one is linear O(ng), n is the size of the string to parse, and g is the size of the grammar.\n\n  - Warning: the grammar must be LL(1) compatible. So you may have to refactor your grammar in some case:\n    - Convert left recursion to right recursion.\n    - Left factoring\n\n- **LLk**: This one do not use anymore search tree algorithm with possible backtracking but a k predictive algorithm, exactly as described in the Aho Ullman book (see [Theory](#theory)). It parses real LLk grammars (ie not only the strong LLk grammar), k can be any integer ≥ 1.\n\n## Bottom up algorithm\n\n- **LR0**: Use an LR0 automaton, and decide to shift or reduce without lookahead.\n- **LR1**: Use an LR1 automaton, and decide to shift or reduce with one lookahead.\n- **SLR1**: Use the LR0 automaton augmented with the FOLLOW terminals, and decide to shift or reduce with one lookahead.\n- **LALR1**: Use the LALR1 automaton (constructed with the \"Lazy Merging\" technique), and decide to shift or reduce with one lookahead.\n\nNote about grammar: LR0 \u003c SLR1 \u003c LALR1 \u003c LR1.\n\n## Project related\n\n- [@jlguenego/lexer](https://github.com/jlguenego/lexer)\n- [@jlguenego/tree](https://github.com/jlguenego/tree)\n\n## Theory\n\n- [Stanford CS143](https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/)\n- [Theory of Parsing, Translation and Compiling: Compiling vol. 1 - Aho Ullman](https://dl.acm.org/doi/pdf/10.5555/578789)\n\n## Author\n\nJean-Louis GUENEGO \u003cjlguenego@gmail.com\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlguenego%2Fsyntax-analysis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjlguenego%2Fsyntax-analysis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlguenego%2Fsyntax-analysis/lists"}