简单计算器的 LL1 语法 (+,-,*,/,pow,root)
LL1 grammar for simple calculator(+,-,*,/,pow,root)
我想出了这个语法,它成功地 add/sub 括号中的数字等等。但是我发现很难将它扩展为 pow(^) 和 root(#) 逻辑。我很感激任何帮助,因为我现在很困难...
nonZeroDigit = "1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";
digit = "0" | nonZeroDigit;
naturalNumber = nonZeroDigit , {digit};
secondPriorityOperators= "+" |"-";
firstPriorityOperators= "*" | "/";
syntax=expr;
expr=term, {secondPriorityOperators, term};
term=factor, {firstPriorityOperators, factor};
factor="(", expr , ")" | naturalNumber;
root = "#" , root | factor;
power = root, "^" , power| root;
为了测试,我使用了这个网站:https://planetcalc.com/6385/
只需添加另一个优先级:
thirdPriorityOperators= "+" |"-";
secondPriorityOperators= "*" | "/";
firstPriorityOperators= "#" | "^";
term3rd=term2nd, {thirdPriorityOperators, term2nd};
term2nd=term1st, {secondPriorityOperators, term1st};
term1st=factor, {firstPriorityOperators, factor};
我想出了这个语法,它成功地 add/sub 括号中的数字等等。但是我发现很难将它扩展为 pow(^) 和 root(#) 逻辑。我很感激任何帮助,因为我现在很困难...
nonZeroDigit = "1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9";
digit = "0" | nonZeroDigit;
naturalNumber = nonZeroDigit , {digit};
secondPriorityOperators= "+" |"-";
firstPriorityOperators= "*" | "/";
syntax=expr;
expr=term, {secondPriorityOperators, term};
term=factor, {firstPriorityOperators, factor};
factor="(", expr , ")" | naturalNumber;
root = "#" , root | factor;
power = root, "^" , power| root;
为了测试,我使用了这个网站:https://planetcalc.com/6385/
只需添加另一个优先级:
thirdPriorityOperators= "+" |"-";
secondPriorityOperators= "*" | "/";
firstPriorityOperators= "#" | "^";
term3rd=term2nd, {thirdPriorityOperators, term2nd};
term2nd=term1st, {secondPriorityOperators, term1st};
term1st=factor, {firstPriorityOperators, factor};