0.6.1 • Published 1 year ago

kison v0.6.1

Weekly downloads
49
License
MIT
Repository
github
Last release
1 year ago

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/core@7.x 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.6.0 - 2022/12/31

  • support incremental parser

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

0.6.1

1 year ago

0.6.0

1 year ago

0.5.48

2 years ago

0.5.43

2 years ago

0.5.44

2 years ago

0.5.42

2 years ago

0.5.40

2 years ago

0.5.47

2 years ago

0.5.45

2 years ago

0.5.46

2 years ago

0.5.38

2 years ago

0.5.39

2 years ago

0.5.36

2 years ago

0.5.37

2 years ago

0.5.34

2 years ago

0.5.35

2 years ago

0.5.32

2 years ago

0.5.33

2 years ago

0.5.30

2 years ago

0.5.31

2 years ago

0.5.29

2 years ago

0.5.28

2 years ago

0.5.25

3 years ago

0.5.26

2 years ago

0.5.23

3 years ago

0.5.24

3 years ago

0.5.18

3 years ago

0.5.19

3 years ago

0.5.16

3 years ago

0.5.17

3 years ago

0.5.21

3 years ago

0.5.22

3 years ago

0.5.20

3 years ago

0.5.15

3 years ago

0.5.10

3 years ago

0.5.11

3 years ago

0.5.14

3 years ago

0.5.12

3 years ago

0.5.13

3 years ago

0.5.8

3 years ago

0.5.7

3 years ago

0.5.9

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.6

3 years ago

0.5.5

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.39

3 years ago

0.4.38

3 years ago

0.4.37

3 years ago

0.4.36

3 years ago

0.4.35

3 years ago

0.4.32

3 years ago

0.4.33

3 years ago

0.4.34

3 years ago

0.4.31

3 years ago

0.4.30

3 years ago

0.4.29

3 years ago

0.4.28

3 years ago

0.4.26

3 years ago

0.4.27

3 years ago

0.4.25

3 years ago

0.4.24

3 years ago

0.4.22

3 years ago

0.4.23

3 years ago

0.4.20

3 years ago

0.4.21

3 years ago

0.4.19

3 years ago

0.4.18

3 years ago

0.4.17

3 years ago

0.4.15

3 years ago

0.4.16

3 years ago

0.4.14

3 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

3 years ago

0.4.10

3 years ago

0.4.9

3 years ago

0.4.8

3 years ago

0.4.5

3 years ago

0.4.7

3 years ago

0.4.6

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.4.0-alpha.16

3 years ago

0.4.0-alpha.17

3 years ago

0.4.0-alpha.14

3 years ago

0.4.0-alpha.15

3 years ago

0.4.0-alpha.11

3 years ago

0.4.0-alpha.13

3 years ago

0.4.0-alpha.9

3 years ago

0.4.0-alpha.8

3 years ago

0.4.0-alpha.7

3 years ago

0.4.0-alpha.4

3 years ago

0.4.0-alpha.3

3 years ago

0.4.0-alpha.2

3 years ago

0.4.0-alpha.1

3 years ago

0.4.0-alpha.0

3 years ago

0.4.0-alpha.6

3 years ago

0.4.0-alpha.5

3 years ago

0.3.6

9 years ago

0.3.5

10 years ago

0.3.4

10 years ago

0.3.3

10 years ago

0.3.2

10 years ago

0.3.1

10 years ago

0.3.0

10 years ago

0.2.2

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.5

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.2

10 years ago

0.1.1

10 years ago

0.1.0

10 years ago