昨天晚上决定正面硬刚神课《编译原理》。硬上龙书。
下面是 一个简单的算术式中缀变后缀的翻译器。
这个也是 龙书中 一个C实现源码 。部分用c++改写。#include <iostream>#include <ctype.h>#include <stdlib.h>#include <stdio.h>using namespace std;int lookahead;void error()//错误处理{cout<<"error"<<endl;exit(1);}//token 用来匹配预扫描记号lookaheadvoid match(int t) //检测迭代函数{if(lookahead == t){lookahead = getchar();}else error();}void term()//判断函数 isdigit(char c);判断输入字符是否是0~9。被包含在头文件<ctype.h>中。{if(isdigit(lookahead)){putchar(lookahead);match(lookahead);}else error();}void expr(){term();while(1){if(lookahead == '+'){match('+');term();putchar('+');}else if(lookahead == '-'){match('+');term();putchar('-');}else break;}}int main(){lookahead = getchar();expr();cout<<endl;return 0;}
,而是深沉的意志恢弘的想象炙热的恋情;青春是生命的深泉在涌流。