https://github.com/fujiharuka/arithmetic-expression
https://github.com/fujiharuka/arithmetic-expression
Last synced: 5 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/fujiharuka/arithmetic-expression
- Owner: FujiHaruka
- License: mit
- Created: 2016-09-02T13:01:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-24T14:42:07.000Z (over 9 years ago)
- Last Synced: 2025-02-25T13:51:44.002Z (10 months ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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'
}
}
```