Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/125.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
莱克瑟一直在跳过最后一个标记 我不使用C++来创建CHIP-8程序集,但总是跳过第三个令牌。< /P>_C++_Assembly_Lexer_Chip 8 - Fatal编程技术网

莱克瑟一直在跳过最后一个标记 我不使用C++来创建CHIP-8程序集,但总是跳过第三个令牌。< /P>

莱克瑟一直在跳过最后一个标记 我不使用C++来创建CHIP-8程序集,但总是跳过第三个令牌。< /P>,c++,assembly,lexer,chip-8,C++,Assembly,Lexer,Chip 8,例如(我的函数读取一行装配) 添加V1、V2 它只找到ADD,V1作为标记,跳过V2; 同样的 DRW V3、V4,2 它只找到DRW、V3、V4 我错在哪里 #include <fstream> #include <sstream> #include <string> #include <vector> #include <algorithm> #include <exception> #include <iostr

例如(我的函数读取一行装配)

添加V1、V2

它只找到ADD,V1作为标记,跳过V2; 同样的

DRW V3、V4,2

它只找到DRW、V3、V4 我错在哪里

#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <exception>
#include <iostream>
#include <iterator>
static std::string C8InsTable[] = {"ADD", "SUB",  "SUBN", "AND", "OR", "XOR",
     "SE",  "SNE",  "SYS",  "JP",  "LD", "DRW",
     "SKP", "CALL", "CLS",  "RET"}; //Instruction Table
static const std::string C88BitReg[] = {"V1", "V2", "V3", "V4", "V5",
          "V6", "V7", "V8", "V9", "VA",
          "VB", "VC", "VD", "VE", "VF"}; //Name of Regsiters
//Define Exceptions
class LexEmptyExc : public std::runtime_error {
public:
    LexEmptyExc() : runtime_error("Empty Line."){};
    virtual const char* what() const throw() { return runtime_error::what();}
} LexEE;
class LexErrorVar : public std::runtime_error {
public:
    LexErrorVar()
    : runtime_error("Error var type.Out of range or not a valid var."){};
    virtual const char* what() const throw() { 
      return runtime_error::what();}}LexEV;

class LexErrorToken : public std::runtime_error {
public:
  LexErrorToken() : runtime_error("Error Token."){};
  virtual const char* what() const throw() { return runtime_error::what(); }
  } LexET;
//To write Log (Test purpose)
bool LexerLogWriter(std::string LogLine, std::ofstream& LexerLogFile) {
  if (!LexerLogFile.good())
    return false;
  else {
    LexerLogFile << LogLine;
    return true;
  }
 };
//It returns Tokens into a string vector
std::vector<std::string> lexer(std::string ProgLine,
 std::ofstream& LexerLogFile) {
  try {
  if (ProgLine.length() == 0) throw LexEE;
  bool IsVar = false;
  bool Is8BitReg = false;
  bool IsIns = false;
  std::stringstream LogStream;
  std::string Token("");
  std::string Var("");
  std::vector<std::string> TokenList;
  std::string param("");
  for (auto& x : ProgLine) {
    std::string LogType;
    if ((x != ',' && x != ' ')){ //Skip spaces and commas
      Token += x;
      std::cout << "Now Token is " << Token << "(lexing)" << std::endl;
    } else{
      if (find(std::begin(C8InsTable), std::end(C8InsTable), Token) !=std::end(C8InsTable)) {  //Find Instruction
        LogType = "Instruction";
    } else if (find(std::begin(C88BitReg), std::end(C88BitReg), Token) !=std::end(C88BitReg)) {
        LogType = "8 Bit Register";
    } else if (Token == "DT") {
        LogType = "Delay Timer";
    } else if (Token == "ST") {
      LogType = "Sound Timer";
    } else if (Token == "I") {
    LogType = "Address Register";
    } else {
        if (Token.length() <= 4) { //4~16bitvar
        LogType = "Var";
        } else { //Var bigger than 16bit-max
          LogStream << "Error:Invalid var exist." << std::endl;
          throw LexEE;
          }
      }
      std::cout << "Now Token is " << Token << "(not lexing)" << std::endl;
      if (!LogType.empty()) { //Is correct token
      LogStream << LogType << " found: " << Token << std::endl;
      LogType = "";
      std::cout << "Pushing " << Token << "..." << std::endl;
      TokenList.push_back(Token);
      Token = ""; //clear the string to store next one
    }
  }
}
  if (!LogStream.str().empty() && LexerLogFile.good())
    LexerLogWriter(LogStream.str(), LexerLogFile); //Write logs
    return TokenList;
  } catch (std::exception& e) {
    std::cerr << "Error occured!Error info: " << e.what() << std::endl;
  }
  }
//Test Main Func
int main() {
std::ofstream a("Test.log"); //Output log file
std::ifstream b("test.asm"); //Infile
for (std::string s; getline(b, s, '\n');)
for (auto x : lexer(s, a)) std::cout << x << std::endl;
  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
静态标准::字符串C8InsTable[]={“添加”、“子”、“子网”、“和”、“或”、“异或”,
“SE”、“SNE”、“SYS”、“JP”、“LD”、“DRW”,
“SKP”、“CALL”、“CLS”、“RET”}//指令表
静态常量std::字符串C88BitReg[]={“V1”、“V2”、“V3”、“V4”、“V5”,
“V6”、“V7”、“V8”、“V9”、“VA”,
“VB”、“VC”、“VD”、“VE”、“VF”}//注册人姓名
//定义例外情况
类LexEmptyExc:public std::runtime\u错误{
公众:
LexEmptyExc():运行时_错误(“空行”){};
虚拟常量char*what()常量throw(){return runtime_error::what();}
}莱克西;
类LexErrorVar:public std::runtime\u错误{
公众:
LexErrorVar()
:runtime_error(“error var type.超出范围或不是有效的变量”){};
虚拟常量char*what()常量throw(){
返回运行时_错误::what();}}LexEV;
类LexErrorToken:public std::runtime\u错误{
公众:
LexErrorToken():运行时_错误(“错误标记”){};
虚拟常量char*what()常量throw(){return runtime_error::what();}
}LexET;
//写入日志(测试目的)
bool LexerLogWriter(std::string LogLine,std::ofstream&LexerLogFile){
如果(!LexerLogFile.good())
返回false;
否则{

LexerLogFile事实上,在我更改分支之后

if ((x == ',' && x == ' ')) 
进入


然后它就开始工作了。

这是我第一次问有关StackOverflow的问题,而且…我在粘贴代码时遇到了麻烦。如何插入已格式化的代码块?我花了很长时间在代码中添加空格。您是否也尝试在
(x!=',&&x!=''中检查换行符(或字符串结尾)
?最后一个标记已被读取,但“else”的情况下没有任何剩余内容,因此它从未被处理过。(即使添加一个尾随“”也会有所帮助,但这将是一个累赘)第一个版本没有意义。一个变量不能同时有两个值。
if ((x == ',' || x == ' ' || &x == &ProgLine.back()))