Skip to content
/ calcjs Public

学习编译原理的demo。解析四则运算字符串,并输出结果

Notifications You must be signed in to change notification settings

robinwp/calcjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

calcjs

calcjs是学习编译原理所写的案例,一个输出四则运算结果的工具。

example

demo

// <script src=calcjs.js></script>
const {result, tree, list} = calcjs('-1.25 * (10 - 2) / -1');
// result 是计算的结果  tree是解析出的语法二叉树, list 是解析出的词法集合
console.log(result); // 10
console.log(list); // [{"type":7,"value":"-1.25"},{"type":3,"value":"*"},{"type":5,"value":"("},{"type":7,"value":"10"},{"type":2,"value":"-"},{"type":7,"value":"2"},{"type":6,"value":")"},{"type":4,"value":"/"},{"type":7,"value":"-1"}]
console.log(tree); // {"uid":"f8dc6807-ab37-49dd-9f5e-005ad70bda93","type":4,"value":"/","leftChild":{"uid":"14e8e6c7-c624-4f01-a6ff-6943646978e3","type":3,"value":"*","leftChild":{"type":7,"value":"-1.25"},"rightChild":{"uid":"531f109f-a486-447f-8263-261e8e6d1ef7","type":2,"value":"-","leftChild":{"type":7,"value":"10"},"rightChild":{"uid":"24d036d0-2957-4ff8-91bc-c6563b609bee","type":7,"value":"2","leftChild":null,"rightChild":null,"status":0},"status":1},"status":1},"rightChild":{"uid":"5f733bfc-4c88-423a-b70e-7926808d320f","type":7,"value":"-1","leftChild":null,"rightChild":null,"status":0},"status":1}

// 如果输入的文本不符合规范, 则报错
calcjs('-1.2 5 * (10 - 2) / -1'); // 报错

About

学习编译原理的demo。解析四则运算字符串,并输出结果

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published