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

ll1-grammar

v1.0.6

Published

根据产生式求取 First、Follow、Select 集和预测分析表

Downloads

7

Readme

编译原理 LL(1) 文法

根据产生式求取 First、Follow、Select 集和预测分析表。

import { makeLL1 } from 'll1-grammar'

const grammars = [ // 书写产生式是规定空格为分隔符,null 为空串
  'E -> T E1',
  'E1 -> + T E1 | null',
  'T -> F T1',
  'T1 -> * F T1 | null',
  'F -> ( E ) | id'
]
const terminalSymbols = ['+', '*', '(', 'id', ')'] // 指定终止符

/**
  返回 firstSet, followSet, selectSet, predictSet 和 print 方法
 */
const ll1 = makeLL1(grammars, terminalSymbols)
ll1.print()

// print
|-------------|-------------|-------------|-------------|-------------|-------------|-------------|
|             |  (          |  id         |  +          |  $          |  )          |  *          |
|-------------|-------------|-------------|-------------|-------------|-------------|-------------|
|  E          |  -> T E1    |  -> T E1    |             |             |             |             |
|-------------|-------------|-------------|-------------|-------------|-------------|-------------|
|  E1         |             |             |  -> + T E1  |  -> null    |  -> null    |             |
|-------------|-------------|-------------|-------------|-------------|-------------|-------------|
|  T          |  -> F T1    |  -> F T1    |             |             |             |             |
|-------------|-------------|-------------|-------------|-------------|-------------|-------------|
|  T1         |             |             |  -> null    |  -> null    |  -> null    |  -> * F T1  |
|-------------|-------------|-------------|-------------|-------------|-------------|-------------|
|  F          |  -> ( E )   |  -> id      |             |             |             |             |
|-------------|-------------|-------------|-------------|-------------|-------------|-------------|

说明

类型

重要的输入为产生式 grammars 和终结符 terminalSymbols,产生式和终结符由一维字符串数组组成。

grammars 规定以空格为分割单元,| 带有或的含义,以 $ 为前缀命名的串为该处执行函数,用于对生成的 AST 进行整合,对于终结符则不需要 $。

terminalSymbols 指定终结符集合,规定终结符不能是 null、|、$,其中 null 表示空串。

例子

对于下面加法表达式文法(1+2+...+3,其中 + id 为终结符)

E -> id E1 E1 -> +id E1 | null

grammars 对应的书写格式为:

const grammars = [
  'E -> id E1',
  'E1 -> + id E1 | null'
]

const grammars = [
  'E -> id E1',
  'E1 -> + id E1',
  'E1 -> null'
]

对于生成 AST 的过程我们可以在产生式中插入辅助函数标识,以 $ 开头

const grammars = [
  'E -> id E1',
  'E1 -> + id $mergeExp E1',
  'E1 -> null'
]

对于终结符则不需在产生式中插入辅助函数标识,具体用法可以查看 ./examples 目录下的任意例子,在目录下运行 npm index.js 即可。

类型表

type EMPTY_CHAIN = null
type $ = '$'

type grammars = string[]

type terminalSymbols = string[]

type right = (string | EMPTY_CHAIN)[][]
type $Set = (string | $)[][]

type rule = {
  left: string,
  right: right
}

type rules = rule[]

type firstSet = {
  [k: string]: right
}

type followSet = {
  [k: string]: $Set
}

type selectSet = Map<rule, string[][] | followSet>

type predictSet = Map<string, Map<left, right>>

API

makeLL1(grammars, terminalSymbols)

根据产生式和指定终止符返回 startSymbol, firstSet, followSet, selectSet, predictSet 和 print 方法。 其中 startSymbol 为产生式集的起始符。

splitGrammars(grammars)

根据产生式生成 rules 数组。

toGrammars(rules, isExpand = false)

把 rules 数组转换成产生式字符串数组,isExpand 参数为是否展开表达式,即把或拆分出来。

combineLikeTerms(rules)

配合 splitGrammars 使用,提取公共因子(注意:不能在产生式中插入辅助函数标识)。

clearLeftRecursion(rules) (未完成,目前只能消除直接左递归)

配合 splitGrammars 使用,消除左递归(注意:不能在产生式中插入辅助函数标识)。

makeFirstSet(rules, terminalSymbols)

生成 first 集。

makeUnionFirstSet(chainSet, firstSet, terminalSymbols)

求解多个 first 集并集。

makeFollowSet(rules, terminalSymbols, firstSet?)

生成 followSet 集,如果不传入 firstSet 参数将自动根据 rules、terminalSymbols 生成 first 集。

makeSelectSet(rules, terminalSymbols, firstSet?, follow?)

生成 followSet 集,如果不传入 firstSet、follow 参数将自动根据 rules、terminalSymbols 生成 first、follow 集。

isNotIntersect(selectSet)

给定 select 集判断该 select 集是否没有交集。

makePredictSet(selectSet)

生成预测分析表。

EMPTY_CHAIN

空集常数。

$

终止符常数。