https://github.com/jlguenego/syntax-analysis
Syntax analysis - parsers.
https://github.com/jlguenego/syntax-analysis
compiler cs143 grammar lalr1 ll1 llk lr0 lr1 parser slr syntax-analysis
Last synced: about 1 year ago
JSON representation
Syntax analysis - parsers.
- Host: GitHub
- URL: https://github.com/jlguenego/syntax-analysis
- Owner: jlguenego
- Created: 2021-01-06T18:13:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-01T11:00:51.000Z (about 5 years ago)
- Last Synced: 2025-03-12T19:46:38.341Z (about 1 year ago)
- Topics: compiler, cs143, grammar, lalr1, ll1, llk, lr0, lr1, parser, slr, syntax-analysis
- Language: TypeScript
- Homepage:
- Size: 1.86 MB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Syntax Analysis
Syntax analysis (compiler step just after lexer).
## Install
```
npm i @jlguenego/syntax-analysis
```
[](https://github.com/google/gts)
## Usage
You should use Typescript in order to check your grammar in a easier way.
```ts
const t = defineTerminalAlphabet(['a', 'b'] as const);
const nt = defineNonTerminalAlphabet(['S', 'A'] as const);
const spec: CFGSpecifications = {
nt,
t,
productions: [
{LHS: 'S', RHS: ['a', 'A', 'a', 'a']},
{LHS: 'S', RHS: ['b', 'A', 'b', 'a']},
{LHS: 'A', RHS: []},
{LHS: 'A', RHS: ['b']},
],
startSymbol: 'S',
};
export const cfg = new ContextFreeGrammar(spec);
// coming from a lexer (ex: @jlguenego/lexer)
const sentence: Sentence = 'abaa'.split('').map(str => ({
name: str,
}));
// the real job: get the parse tree.
const parseTree = parse(sentence, cfg, {
method: 'LLk',
lookaheadTokenNbr: 2,
});
```
## Top down algorithm
### Breadth First Search
- **BFS1**: Naive Breadth First Search with nothing else (very slow, may take many days...).
- **BFS2**: Like BFS1 with 2 checks for speeding BFS (slow, may take many hours...).
- checks the length of sentential form
- checks the sentence prefix of the sentential form
- **BFS3**: Like BFS2 with LeftMost Derivation strategy (not so slow, mak take some minutes...).
### Depth First Search
- **DFS1**: Leftmost derivation strategy. (not so slow execpt for left recursive grammar)
- **DFS2**: Like DFS1 but use one lookahead terminal to speed up a little bit.
- **LL1**: Like DFS2 but use a LL1 table to know exactly wich production rule to use for the next sentential form.
This one is linear O(ng), n is the size of the string to parse, and g is the size of the grammar.
- Warning: the grammar must be LL(1) compatible. So you may have to refactor your grammar in some case:
- Convert left recursion to right recursion.
- Left factoring
- **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.
## Bottom up algorithm
- **LR0**: Use an LR0 automaton, and decide to shift or reduce without lookahead.
- **LR1**: Use an LR1 automaton, and decide to shift or reduce with one lookahead.
- **SLR1**: Use the LR0 automaton augmented with the FOLLOW terminals, and decide to shift or reduce with one lookahead.
- **LALR1**: Use the LALR1 automaton (constructed with the "Lazy Merging" technique), and decide to shift or reduce with one lookahead.
Note about grammar: LR0 < SLR1 < LALR1 < LR1.
## Project related
- [@jlguenego/lexer](https://github.com/jlguenego/lexer)
- [@jlguenego/tree](https://github.com/jlguenego/tree)
## Theory
- [Stanford CS143](https://web.stanford.edu/class/archive/cs/cs143/cs143.1128/)
- [Theory of Parsing, Translation and Compiling: Compiling vol. 1 - Aho Ullman](https://dl.acm.org/doi/pdf/10.5555/578789)
## Author
Jean-Louis GUENEGO