All Projects → yiminghe → kison

yiminghe / kison

Licence: MIT license
A LALR(1)/LL(1)/LL(K) parser generator for javascript/typescript

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects
shell
77523 projects

Projects that are alternatives of or similar to kison

lalr
Modern LALR(1) parser for C++
Stars: ✭ 56 (+40%)
Mutual labels:  parser-generator, lalr
tree-sitter-cli
CLI tool for creating and testing tree-sitter parsers
Stars: ✭ 43 (+7.5%)
Mutual labels:  parser-generator
Pasukon
JavaScript practical parser generator library using combinators
Stars: ✭ 107 (+167.5%)
Mutual labels:  parser-generator
Npeg
PEGs for Nim, another take
Stars: ✭ 163 (+307.5%)
Mutual labels:  parser-generator
Gogll
Generates generalised LL (GLL) and reduced size LR(1) parsers with matching lexers
Stars: ✭ 125 (+212.5%)
Mutual labels:  parser-generator
Reduce.jl
Symbolic parser generator for Julia language expressions using REDUCE algebra term rewriter
Stars: ✭ 172 (+330%)
Mutual labels:  parser-generator
Nice Parser
Nice parsers in OCaml without the boilerplate
Stars: ✭ 91 (+127.5%)
Mutual labels:  parser-generator
lilt
LILT: noun, A characteristic rising and falling of the voice when speaking; a pleasant gentle accent.
Stars: ✭ 18 (-55%)
Mutual labels:  parser-generator
kiuatan
A parser library for Pony.
Stars: ✭ 15 (-62.5%)
Mutual labels:  parser-generator
Ecsharp
Home of LoycCore, the LES language of Loyc trees, the Enhanced C# parser, the LeMP macro preprocessor, and the LLLPG parser generator.
Stars: ✭ 141 (+252.5%)
Mutual labels:  parser-generator
Lalrpop
LR(1) parser generator for Rust
Stars: ✭ 1,929 (+4722.5%)
Mutual labels:  parser-generator
Irony
A modified version of the Irony project (https://irony.codeplex.com) with .NET Core support
Stars: ✭ 127 (+217.5%)
Mutual labels:  parser-generator
Tatsu
竜 TatSu generates Python parsers from grammars in a variation of EBNF
Stars: ✭ 198 (+395%)
Mutual labels:  parser-generator
Antlr4
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
Stars: ✭ 11,227 (+27967.5%)
Mutual labels:  parser-generator
nearley-playground
⛹ Write Grammars for the Nearley Parser!
Stars: ✭ 76 (+90%)
Mutual labels:  parser-generator
Fall
Stars: ✭ 92 (+130%)
Mutual labels:  parser-generator
Csly
a C# embeddable lexer and parser generator (.Net core)
Stars: ✭ 129 (+222.5%)
Mutual labels:  parser-generator
Pegparser
💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
Stars: ✭ 164 (+310%)
Mutual labels:  parser-generator
abnf parsec
ABNF in, parser out
Stars: ✭ 42 (+5%)
Mutual labels:  parser-generator
usfm-grammar
An elegant USFM parser.
Stars: ✭ 29 (-27.5%)
Mutual labels:  parser-generator

kison

NPM version NPM downloads Build Status

https://yiminghe.me/kison

https://github.com/yiminghe/kison

A LALR(1)/LL(1)/LL(K) parser generator for javascript/typescript

examples

https://yiminghe.me/kison/examples/

npm version NPM downloads

  • json: es5 compliant javascript implementation of JSON parser

npm version NPM downloads

npm version NPM downloads

npm version NPM downloads

npm version NPM downloads

run command

npx kison@latest -g xx-grammar.js

grammar and lexer definition

LALR

cal-grammar.js: support operator precedence

module.exports = {
  productions: [
    {
      symbol: 'exp',
      rhs: ['primaryExpression'],
    },

    {
      symbol: 'exp',
      rhs: ['exp', '^', 'exp'],
      action() {
        return {
          v: Math.pow(this.$1.v, this.$3.v),
          l: this.$1,
          r: this.$3,
          op: '^',
        };
      },
    },
    {
      symbol: 'exp',
      rhs: ['exp', '-', 'exp'],
      action() {
        return { v: this.$1.v - this.$3.v, l: this.$1, r: this.$3, op: '-' };
      },
    },
    {
      symbol: 'exp',
      rhs: ['exp', '*', 'exp'],
      action() {
        return { v: this.$1.v * this.$3.v, l: this.$1, r: this.$3, op: '*' };
      },
    },
    {
      symbol: 'exp',
      rhs: ['exp', '/', 'exp'],
      action() {
        return { v: this.$1.v / this.$3.v, l: this.$1, r: this.$3, op: '/' };
      },
    },
    {
      symbol: 'exp',
      precedence: 'UMINUS',
      rhs: ['-', 'exp'],
      action() {
        return { v: -this.$2.v, op: 'UMINUS' };
      },
    },
    {
      symbol: 'exp',
      rhs: ['exp', '+', 'exp'],
      action() {
        return { v: this.$1.v + this.$3.v, l: this.$1, r: this.$3, op: '+' };
      },
    },
    {
      symbol: 'primaryExpression',
      rhs: ['(', 'exp', ')'],
      action() {
        return this.$2;
      },
    },
    {
      symbol: 'primaryExpression',
      rhs: ['NUMBER'],
      action() {
        return { v: Number(this.$1) };
      },
    },
  ],

  operators: [
    ['left', '+', '-'],
    ['left', '*', '/'],
    ['right', '^'],
    ['right', 'UMINUS'],
  ],

  lexer: {
    rules: [
      {
        regexp: /^\s+/,
        token: '$HIDDEN',
      },
      {
        regexp: /^[0-9]+(\.[0-9]+)?\b/,
        token: 'NUMBER'
      }
    ]
  }
};

LL/LL(K)

cal-grammar.js:

  • LL(1) and LL(K) support:
    • direct left recursive
    • operator precedence
    • repeat notation(*/+)
    • optional notation(?)
    • group notation('('/')')
    • alternative natation('|')
  • LL(K) extra support:
    • lazy repeat notation(*?/+?)
    • lazy optional notation(??).
const startGroup = `'('`;
const endGroup = `')'`;
const alternative = `'|'`;

module.exports = () => ({
  productions: [
    {
      symbol: 'program',
      rhs: ['statements'],
    },
    {
      symbol: 'statements',
      rhs: [startGroup, 'exp', 'NEW_LINE', endGroup + '*'],
    },
    {
      symbol: 'exp',
      rhs: [
        'exp', '+', 'exp',
        alternative,
        'exp', '-', 'exp',
        alternative,
        'exp', '*', 'exp',
        alternative,
        'exp', '/', 'exp',
        alternative,
        'exp', '^', 'exp',
      ],
      label: 'binary-exp',
    },
    {
      symbol: 'exp',
      rhs: ['-', 'exp'],
      precedence: 'UMINUS',
    },
    {
      symbol: 'exp',
      rhs: ['NUMBER'],
    },
    {
      symbol: 'exp',
      rhs: ['(', 'exp', ')'],
    },
  ],

  operators: [
    ['left', '+', '-'],
    ['left', '*', '/'],
    ['right', '^'],
    ['right', 'UMINUS'],
  ],

  lexer: {
    rules: [
      {
        regexp: /^\n/,
        token: 'NEW_LINE',
      },
      {
        regexp: /^\s+/,
        token: '$HIDDEN',
      },
      {
        regexp: /^[0-9]+(\.[0-9]+)?\b/,
        token: 'NUMBER',
      },
    ],
  },
});

command options

  • --es: generate es module
  • -g: grammar file
  • -m: ll or lalr or llk (llk is powerful than ll but less performant!)
  • --babel: use babel to transform code. need install @babel/[email protected] and @babel/preset-env manually
  • --declaration: generate d.ts type file for LL parser

LALR

npx kison@latest -g cal-grammar.js

LL

ll parser generator

npx kison@latest -m ll -g cal-grammar.js

changelog

0.5.0 - 2021/09/30

  • support '-m llk' support LL(K)

0.4.35 - 2021/09/22

  • support --declaration to generate d.ts type for LL

0.4.x - 2021/06/24

  • LL & LALR

    • add $HIDDEN token type
    • use js config file
    • add excel formula demo
    • support filter for lexer config
    • support operator precedence
  • LL

    • support LL parser
    • support direct left recursive elimination and extract common prefix for LL
    • support parser tree auto return even if partly error

0.3.0 - 2014/08/25

  • optimize error debug info

Fork me on GitHub

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].