【发布时间】:2014-11-18 08:57:01
【问题描述】:
以为我已经完成并准备提交这个小项目,直到我得到这个意想不到的曲线球。
目标是使用令牌词法分析器制作解析器。
本质上
<underline><red> R <green> G </green> <blue> B </blue> and back to red </red></underline>
将以各自的颜色和属性输出为:“RGB 并变回红色”。
在 Windows 上一切正常,但是当我将其移至 Linux 系统时,它输出颜色代码而没有任何反应。
#include <iostream>
#include <sstream>
#include <stack>
#include <map>
#include <cstdlib>
#include <vector>
#include "cmd.h"
#include "Lexer.h" // you should make use of the provided Lexer
#include "term_control.h"
#include "error_handling.h"
using namespace std;
map<string, term_colors_t> colorMap;
map<string, term_attrib_t> attribMap;
string display(const string& expression)
{
if(validate(expression) == "VALID") {
Lexer lex;
Token tok;
vector<term_colors_t> colorVect;
vector<term_attrib_t> attribVect;
lex.set_input(expression);
while(lex.has_more_token()){
tok = lex.next_token();
string sTok = tok.value;
if(tok.type == TAG && tok.value.at(0) != '/'){
cout<<term_cc(colorMap[tok.value], DEFAULT_COLOR, attribMap[tok.value]);
colorVect.push_back(colorMap[tok.value]);
attribVect.push_back(attribMap[tok.value]);
}
if(tok.type == TAG && tok.value.at(0) == '/'){
colorVect.pop_back();
cout<<term_cc(colorVect.back(), DEFAULT_COLOR, attribVect.back());
}
if(tok.type != TAG){
cout<<tok.value;
}
}
}
else if(validate(expression) != "VALID") return validate(expression);
return "";
}
_
cout<term_cc(Color, DEFAULT_COLOR, Attribute)
是隐藏问题的具体方法,我一直在寻找,但似乎找不到合适的方法。
cout<<term_fg(color)
该方法在 Linux 系统上正确显示颜色,但我无法使用该方法获得属性。
我一直在阅读的所有内容仅与颜色有关,而不是颜色和属性,它们还使用 echo 命令和特定终端的硬编码颜色。这些将需要对我的所有代码进行重大更改,并导致它无法在 Windows 上运行,并且只能在 Linux 上运行,所以我试图避免这种情况。
提前感谢大家对这个问题的任何建议,我很感激,希望我能在 12 点之前得到这个!
【问题讨论】:
-
什么是
term_cc或term_fg?你在使用一些图书馆吗?如果是这样,则应在问题或标签中提及。 -
您提供的代码中是指
cout<term_cc还是cout<<term_cc? -
添加了对混音家伙的所有抱歉。还有 cout
-
您的词法分析器是否从文件中读取,如果确实如此,它是否正确解释了在 Windows 上换行符是 CRLF 而在 Linux 上通常是 LF 的事实?
-
它从控制台获取输入,所以输入会显示出来所以。当它得到一个标记
时,它知道它是一个“标签”并解析出箭头,返回一个字符串“red”。 @MichaelPetch