c++ - How can I use a stack to validate if an expression or assignment with BNF in C++?
问题描述
I have tokenized with a space delimiter into a vector and then I have made it into stack. I want to use the stack to validate whether the expression or assignment is correct.
assignment shall have the form: id = exp; expression shall have the form: id op id {op id} -- any length as long as pairs of op and id are added A parenthesis pair may be used to group any id op id combination. Therefore: id op (id op id) op id AND id op id op (id op id) -- valid expressions
解决方案
你要这个 :-
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
这会将带括号的中缀表达式转换为堆栈上可以评估的后缀表达式。如果分流失败,则表达式无效,即通过适当的检查,您可以将 x=3+((-+) 捕获为格式错误的表达式。
推荐阅读
- html - 对齐文本 HTML 顶部的图标
- reactjs - 什么是我们在反应状态下称为函数的生命周期?
- python - 运行pytest时如何抑制所有警告?
- scala - 使用列列表和 joinExprs 动态连接 Spark Scala 数据帧
- groovy - Jmeter 等到 CSV 文件可读
- c# - 具有多个联接的 SQL 查询到 LINQ
- maven-3 - 如何发布 Maven 项目?
- react-native - react-native-render-html 如何在 react-native-render-html 中实现可访问性?
- ios - 使 UIView.animate 出现在子视图中而不是超级视图 Swift
- xcode - 应用中心:应用内更新已禁用:此版本是侧载或使用浏览器以私密模式下载的