gpt4 book ai didi

c - 如何使用 mpc 解析器定义标准数学符号

转载 作者:行者123 更新时间:2023-11-30 14:45:33 24 4
gpt4 key购买 nike

我正在这里阅读编译器教程 www.buildyourownlisp.com 。它使用一个名为 mpc 的解析器组合器。我现在所拥有的将解析波兰表示法,但我正在尝试找出如何使用标准表示法。我只是似乎不知道该怎么做。解析器的规则如下:

.   Any character is required.
a The character a is required.
[abcdef] Any character in the set abcdef is required.
[a-f] Any character in the range a to f is required.
a? The character a is optional.
a* Zero or more of the character a are required.
a+ One or more of the character a are required.
^ The start of input is required.
$ The end of input is required.
"ab" The string ab is required.
'a' The character a is required.
'a' 'b' First 'a' is required, then 'b' is required.
'a' | 'b' Either 'a' is required, or 'b' is required.
'a'* Zero or more 'a' are required.
'a'+ One or more 'a' are required.
<abba> The rule called abba is required.

波兰语表示法是这样写的:

     "                                                                                       \
number: /-?[0-9]+/'.'?/[0-9]+/ ; \
operator: '+' | '-' | '*' | '/' | '%' | \"add\" | \"sub\" | \"mul\" | \"div\" ; \
expr: <number> | '(' <operator> <expr>+ ')' ; \
dlispy: /^/ <operator> <expr>+ /$/ ;",

我已经设法通过添加 '.'?/[0-9]+/ 使其接受十进制数字,但我不知道如何重组它以接受标准表示法例如 2*(3+2) 而不是 *2 (+ 3 2)。我知道我必须重写 expr 和 dlispy 规则,但我对正则表达式和 BNF 很陌生。希望您能帮忙,谢谢

最佳答案

按照 yacc 规则编写,即:

expr    : '(' expr ')
| expr operator expr
| number
;

关于c - 如何使用 mpc 解析器定义标准数学符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52967767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com