使用递归前缀转换前缀转换
Infix to Prefix Conversion using Recursion
我正在努力寻找前缀翻译方案的摘要。
我已经想到了infix to Postfix translation sceme:
expr -> Term, Rest
Rest -> +Term, { print('+') } , Rest | -Term, { print('-') }, Rest | epsilon
Term -> Factor, Rest_
Rest_ -> *Factor, { print('*') }, Rest_ | /Factor, { print('/') }, Rest_ | epsilon
Factor -> Digit | (expr)
Digit -> 0,1,2,3,4,5,6,7,8,9
和我的命中符号根据上述翻译方案:
#include<iostream>
using namespace std;
const char input[] = "9-5*2";
int index = 0;
char LookAhead = input[index];
void Match(char newChar);
void Factor();
void Rest_();
void Rest();
void Term();
void Expression();
int main(){
Expression();
return 0;
}
void Match(char newChar){
if(newChar == LookAhead){
index++;
LookAhead = input[index];
}
}
void Expression(){
Term();
Rest();
}
void Term(){
Factor();
Rest_();
}
void Rest(){
if(LookAhead == '+'){
Match('+');
Term();
cout << '+';
Rest();
}else if(LookAhead == '-'){
Match('-');
Term();
cout << '-';
Rest();
}else{
}
}
void Rest_(){
if(LookAhead == '*'){
Match('*');
Factor();
cout << '*';
Rest_();
}else if(LookAhead == '/'){
Match('/');
Factor();
cout << '/';
Rest_();
}else{
}
}
void Factor(){
if(isdigit(LookAhead)){
cout << LookAhead;
Match(LookAhead);
}
}
所以现在有任何专家可以帮助我了解前缀转换翻译方案的插图,这将不胜感激。
我们可以通过解析树测试。如果我们可以生成> -9 52 示例字符串 9-5 2 的前缀字符串。
告诉我,我是否需要对我的插图进行更多有关后缀转换翻译方案和代码的信息,以获得更好的理解。
预先感谢!
编辑:简而言之,我遇到了一个问题,发现前缀表达转换翻译方案的插图。举个例子,我的输入:
9-5+2
预期输出:
-9+52
,我想通过上面显示的与后缀转换相同的结构来实现这一目标。就是全部!
最简单的解决方案是在解析时构造一个抽象语法树(AST),然后使用预订的遍历递归地行走树。
您也可以在使用时构造字符串(例如,@chrisdodd在此处建议),但是:
- 这不是递归解决方案:)和
- 它涉及大量的字符串复制,因此它可能具有二次运行时间。
将后缀转换成某种临时数据结构(例如,堆栈)似乎也很诱人,然后通过打印前缀表达式来递归地行走后缀表示"评估"。这肯定会起作用,但是您会遇到一个以自然方式穿越后缀表示的问题左手参数。这意味着您需要向后构造字符串,这涉及另一个临时数据架构(例如,另一个堆栈)。
总体上,AST解决方案更干净,并为将来添加更多功能提供了良好的基础。
因此,我使用了一个时间变量来存储我的数字,并以维护订单的优先级进行递归检查和递归打印。
我的解决方案:
#include<iostream>
using namespace std;
const char input[] = "9-5+2";
int index = 0;
char LookAhead = input[index];
char TempLookAhead = 'x';
void Match(char newChar);
void Factor();
void Rest_();
void Rest();
void Term();
void Expression();
int main(){
Expression();
return 0;
}
void Match(char newChar){
if(newChar == LookAhead){
index++;
LookAhead = input[index];
}
}
void Expression(){
Term();
Rest();
cout << TempLookAhead;
}
void Term(){
Factor();
Rest_();
}
void Rest(){
if(LookAhead == '+'){
cout << '+';
cout << TempLookAhead;
Match(LookAhead);
Term();
Rest();
}else if(LookAhead == '-'){
cout << '-';
cout << TempLookAhead;
Match(LookAhead);
Term();
Rest();
}else{
}
}
void Rest_(){
if(LookAhead == '*'){
cout << '*';
cout << TempLookAhead;
Match(LookAhead);
Factor();
Rest_();
}else if(LookAhead == '/'){
cout << '/';
cout << TempLookAhead;
Match(LookAhead);
Factor();
Rest_();
}else{
}
}
void Factor(){
if(isdigit(LookAhead)){
TempLookAhead = LookAhead;
Match(LookAhead);
}
}
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 将单个字符转换为 std::string 前缀 \x01
- n-ary布尔语法从中缀到前缀的Boost::Spirit转换?
- 使用递归前缀转换前缀转换
- 将Infix转换为前缀表示法
- 一种在检查十六进制前缀后将字符串转换为int的更干净的方法
- 使用堆栈将前缀转换为后缀
- 前缀表示法字符串到int的转换
- c++中缀到前缀的转换
- 将前缀表达式树向量转换为ORF/Karva表示法表达式树向量