npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jlguenego/syntax-analysis

v1.6.4

Published

Syntax analysis. Lot of different methods.

Downloads

18

Readme

Syntax Analysis

Syntax analysis (compiler step just after lexer).

Install

npm i @jlguenego/syntax-analysis

Code Style: Google

Usage

You should use Typescript in order to check your grammar in a easier way.

const t = defineTerminalAlphabet(['a', 'b'] as const);
const nt = defineNonTerminalAlphabet(['S', 'A'] as const);

const spec: CFGSpecifications<typeof t, typeof nt> = {
  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). 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

Theory

Author

Jean-Louis GUENEGO [email protected]