An open API service indexing awesome lists of open source software.

https://github.com/fujiharuka/arithmetic-expression


https://github.com/fujiharuka/arithmetic-expression

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# arithmetic-expression

## 算術式の再帰的定義

+ 1文字以上の単語構成文字は算術式である。(例: 1, 3, a, x, hoge)
+ s と t が算術式のとき、 (s+t), (s-t), (s*t), (s/t) も算術式である。
+ 空白文字は無視する。
+ 上記以外は算術式でない。

## 算術式の構文解析

### parse()

算術式の定義にかなっている文字列をトークンに分割して逆ポーランド記法による配列にする。

```js
> parse('(a + (9 * b))')
['a', '9', 'b', '*', '+']
```

### makeTree()

逆ポーランド記法にパースされた配列を抽象構文木にする。

```js
> makeTree(['a', '9', 'b', '*', '+'])
{
operator: '+',
left: 'a',
right: {
operator: '*',
left: '9',
right: 'b'
}
}
```