js实现递归下降分析法

直接贴代码吧

    let printf = () => {
		let inputText = document.getElementById('textareaInput')

        printf= {
            log: (str) => {
                inputText.value += str + '\n'
            },
            clear: () => {
                inputText.value = ''
                inputText.value = ''
            }
        }
    }

    function getResult(strExpression) {
        let isValid = true,
            nowI = 0
        E()
        return isValid

        function E() {
            printf.log('E=>TG')
            T()
            G()
        }

        function T() {
            printf.log('T=>FS')
            F()
            S()
        }

        function G() {
            printf.log('G=>ε')
            if (strExpression[nowI] == '+') {
                nowI++
                printf.log('G >+TG')
                T()
                G()
            } else if (strExpression[nowI] == '-') {
                nowI++
                printf.log('G >-TG')
                T()
                G()
            }
        }

        function F() {
            if (strExpression[nowI] == '(') {
                nowI++
                E()
                if (strExpression[nowI] == ')') {
                    nowI++
                    printf.log('F=>(E)')
                } else flag = false
            } else if (strExpression[nowI] == 'i') {
                printf.log('F=>i')
                nowI++
            } else isValid = false
        }

        function S() {
            printf.log('S=>ε')
            if (strExpression[nowI] == '*') {
                nowI++
                F()
                S()
            } else if (strExpression[nowI] == '/') {
                nowI++
                F()
                S()
            }
        }
    }
    window.onload = () => {
        let inputExpress = document.getElementById('inputExpress'),
            inputInput = document.getElementById('inputInput')
			printf()
        inputInput.onclick = () => {
            printf.clear()
            printf.log(getResult(inputExpress.value) ? 'Accepted' : 'error')
        }
    }
   
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet" />
<style>
    *:focus {
        outline: none;
    }
</style>

<body>
    <div class="h-screen bg-gray-100 py-6 flex justify-center items-center w-full">
        <div class="w-full min-h-min md:w-1/2 md:h-1/2 m-9 relative">
            <div class="absolute inset-0 bg-gradient-to-r from-green-300 to-blue-300 shadow-lg transform md:-skew-y-6 sm:skew-y-0 sm:-rotate-6 sm:rounded-3xl rounded-md"></div>
            <div class="p-8 relative min-h-min z-10 bg-white rounded-xl">
                <div class="uppercase text-black text-3xl md:text-base">INPUT expression</div>
                <div class="mt-5">
                    <input type="text" class="px-2 ring-2 py-3 md:py-0 rounded-md w-full md:w-1/2" id="inputExpress" />
                    <input type="button" value="S t a r t" class="uppercase mt-6 md:mt-0 md:ml-3 px-3 md:py-1 py-2 w-full md:w-min rounded-md cursor-pointer bg-blue-400 text-white" id="inputInput" />
                </div>
                <div class="">
                    <div class="uppercase text-black text-3xl md:text-base mt-6">out result</div>
                    <textarea class="resize-none border ring-2 m-2 w-full h-36 rounded-md p-2 box-border" id="textareaInput"></textarea>
                </div>
            </div>
        </div>
    </div>
</body>

运行效果:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值