- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在用 C++ 编写一个词法分析器,我能够编译该程序并将其链接得很好;然而,在运行它时,我得到了大量的输出转储,我不知道为什么会这样,或者它是否与我编写的程序有关。我什至无法复制整个转储,但这是一个 gif它的发生。这是一个很好的衡量标准的程序。
我已经在 Windows 10 上用 cygwin 编译和运行了。
token.h
#ifndef TOKEN_H
#define TOKEN_H
#include <string>
class ice_lexer;
enum ice_type_enum {
ICE_NONE,
ICE_EOF,
ICE_MODULE,
ICE_IDENT,
ICE_FLT,
ICE_NUM,
ICE_STR,
// SYMBOLS
ICE_RPARA,
ICE_LPARA,
ICE_RBRAC,
ICE_LBRAC,
ICE_RCURL,
ICE_LCURL,
ICE_PERIOD,
ICE_COLON,
ICE_SEMIC,
ICE_COMMA,
// COMPARISON
ICE_EQLTO,
ICE_NOTEQL,
ICE_GRT,
ICE_GRTEQL,
ICE_LES,
ICE_LESEQL,
// ASSIGNMENT
ICE_EQL,
ICE_ADDEQL,
ICE_SUBEQL,
ICE_MULEQL,
ICE_DIVEQL,
ICE_CAREQL,
// URNARY
ICE_URNARYNOT,
ICE_URNARYSUB,
// BINARY
ICE_ADD,
ICE_SUB,
ICE_MUL,
ICE_DIV,
ICE_MOD,
ICE_CAR,
ICE_CAT,
// TERNARY
ICE_TERNARY,
// KEYWORDS
ICE_IMPORT,
ICE_AS,
ICE_WHILE,
ICE_FOR,
ICE_BREAK,
ICE_IF,
ICE_RETURN,
ICE_TRUE,
ICE_FALSE,
// TYPES
ICE_TYPEDEF,
ICE_CLASS,
ICE_ENUM,
ICE_INT,
ICE_FLOAT,
ICE_STRING,
ICE_BOOLEAN,
ICE_VOID,
ICE_STRUCT
};
typedef struct ice_keyword {
std::string word;
ice_type_enum type;
} ice_keyword;
class ice_type {
public:
std::string string;
ice_type_enum type;
ice_type(ice_type_enum t);
};
class ice_token {
public:
ice_type type;
size_t line;
size_t pos;
std::string str;
double flt;
size_t num;
ice_token(const ice_lexer* lexer, ice_type_enum t);
~ice_token() {};
void print();
};
#endif
token.cpp
#include <string>
#include <iostream>
#include "token.h"
#include "lexer.h"
static const std::string ice_type_enum_strings[] = {
// ABSTRACT
"no type",
"end of file",
"module",
"identifier",
"floating",
"number",
"string",
// SYMBOLS
"left para",
"right para",
"left brac",
"right brac",
"left curl",
"right curl",
"period",
"colon",
"semi",
"comma",
// COMPARISON
"eql to",
"not eql",
"grt",
"grt eql",
"les",
"les eql",
// ASSIGNMENT
"eql",
"add eql",
"sub eql",
"mul eql",
"div eql",
"car eql",
// URNARY
"urnarynot",
"urnarysub",
// BINARY
"add",
"sub",
"mul",
"div",
"mod",
"car",
"cat",
// TERNARY
"ternary",
// KEYWORDS
"import",
"as",
"while",
"for",
"break",
"if",
"return",
"true",
"false",
// TYPES
"typedef",
"class",
"enum",
"int",
"float",
"string",
"bool",
"void",
"struct"
};
ice_type::ice_type(ice_type_enum t) {
string = ice_type_enum_strings[t];
type = t;
}
ice_token::ice_token(const ice_lexer* lexer, ice_type_enum t):type(t) {
line = lexer->line;
pos = lexer->pos;
num = 0;
}
void ice_token::print() {
std::cout << "TYPE [ STR: \'" << this->type.string << "\' | ENUM: " << this->type.type << " ]:" << std::endl;
std::cout << "\tLINE NO. " << this->line << " IN THE " << this->pos << "TH ROW" << std::endl;
if (this->type.type == ICE_NUM)
std::cout << "\tNUMBER VALUE: \'" << this->num << "\'" << std::endl;
if (this->type.type == ICE_FLT)
std::cout << "\tFLOAT VALUE: \'" << this->flt << "\'" << std::endl;
if (this->type.type != ICE_NUM && this->type.type != ICE_FLT)
std::cout << "\tSTR VALUE: \'" << this->str << "\'" << std::endl;
}
lexer.h
#ifndef LEXER_H
#define LEXER_H
#include "token.h"
#include <string>
class ice_token;
#define newline() { \
this->line++; \
this->pos = 0; \
}
class ice_lexer {
public:
std::string source;
size_t length;
size_t line;
size_t pos;
size_t ptr;
ice_lexer(std::string file_name);
~ice_lexer() {};
ice_token lexer_next();
private:
const char look(int offset);
const char advance(int offset);
void comment(void);
void nested(void);
ice_token symbol(ice_type_enum type, std::string string, int move);
void ident(void);
};
#endif
lexer.cpp
#include <fstream>
#include <string>
#include <assert.h>
#include "token.h"
#include "lexer.h"
extern const ice_keyword keywords[] = {
{"import", ICE_IMPORT},
{"as", ICE_AS},
{"while", ICE_WHILE},
{"for", ICE_FOR},
{"break", ICE_BREAK},
{"if", ICE_IF},
{"return", ICE_RETURN},
{"true", ICE_TRUE},
{"false", ICE_FALSE},
{"typedef", ICE_TYPEDEF},
{"class", ICE_CLASS},
{"enum", ICE_ENUM},
{"int", ICE_INT},
{"float", ICE_FLOAT},
{"string", ICE_STRING},
{"bool", ICE_BOOLEAN},
{"void", ICE_VOID},
{"struct", ICE_STRUCT}
};
ice_lexer::ice_lexer(std::string file_name) {
std::ifstream input_file(file_name);
std::string content((std::istreambuf_iterator<char>(input_file)),
(std::istreambuf_iterator<char>()));
line = 1;
pos = 0;
ptr = 0;
}
const char ice_lexer::look(int offset = 0) {
if (this->ptr + offset < this->length)
return '\0';
return this->source[this->ptr + offset];
}
const char ice_lexer::advance(int offset) {
assert(this->ptr + offset < this->length);
return look();
}
void ice_lexer::comment(void) {
while (look() != '\n') { advance(1); }
newline();
}
void ice_lexer::nested(void) {
size_t depth = 1;
while (depth > 0) {
if (look(0) == '\\' && look(1) == '\\') {
advance(2);
depth--;
} else if (look(0) == '/' && look(1) == '/') {
advance(2);
depth++;
} else {
advance(1);
}
}
}
ice_token ice_lexer::symbol(ice_type_enum type, std::string string, int move) {
ice_token token(this, type);
token.str = string;
advance(move);
return token;
}
ice_token ice_lexer::lexer_next() {
/*
while (look() != '\0') {
switch (look()) {
case '+':
if (look(1) == '=')
return symbol(ICE_ADDEQL, "+=", 2);
else
return symbol(ICE_ADD, "+", 1);
break;
case '-':
if (look(1) == '=')
return symbol(ICE_ADDEQL, "+=", 2);
else
return symbol(ICE_SUB, "-", 1);
break;
case '*':
if (look(1) == '=')
return symbol(ICE_ADDEQL, "+=", 2);
else
return symbol(ICE_MUL, "*", 1);
break;
case '/':
if (look(1) == '=')
return symbol(ICE_DIVEQL, "/=", 2);
else if(look(1) == '/')
nested();
else
return symbol(ICE_DIV, "/", 1);
break;
case '^':
if (look(1) == '=')
return symbol(ICE_CAREQL, "^=", 2);
else
return symbol(ICE_CAR, "^", 1);
break;
case '~':
comment();
case '(':
return symbol(ICE_LPARA, "(", 1);
break;
case ')':
return symbol(ICE_RPARA, ")", 1);
break;
case '[':
return symbol(ICE_LBRAC, "[", 1);
break;
case ']':
return symbol(ICE_RBRAC, "]", 1);
break;
case '{':
return symbol(ICE_LCURL, "{", 1);
break;
case '}':
return symbol(ICE_RCURL, "}", 1);
break;
default:
break;
}
}
*/
}
最佳答案
我做了一些更改,并且有点让它工作了。您正在做的一些事情有点靠不住。
ice_lexer::ice_lexer(std::string file_name) {
std::ifstream input_file(file_name);
std::string content((std::istreambuf_iterator<char>(input_file)),
(std::istreambuf_iterator<char>()));
line = 1;
pos = 0;
ptr = 0;
}
您正在将文件读入 content
,这是一个局部变量。您可能打算将其读入 source
.您也没有更新 length
,我猜应该是 source.size()
.
ice_lexer::look()
中存在错误:
const char ice_lexer::look(int offset = 0) {
if (this->ptr + offset < this->length)
return '\0';
return this->source[this->ptr + offset];
}
条件应该是>=
, 不是 <
. ice_lexer::advance()
中的另一个类似错误:
const char ice_lexer::advance(int offset) {
assert(this->ptr + offset < this->length);
return look();
}
应该是<=
.
当您在控制台上看到垃圾时,通常意味着您正在打印一个没有 \0
的字符串。最后,它只是在内存中逃跑,直到找到 \0
。某处。如果您在不该去的地方阅读,它也可能会崩溃。这反过来可能意味着您有未初始化的变量。
学习正确使用调试器。通过逐步执行函数和查看变量,这很容易弄清楚。
关于c++ - Cygwin 控制台转储大量神秘输出然后键入 1;2;6;22c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39049917/
我正在尝试更新我的 jtable(更改值并按 Enter 键),但出现错误。由于大小原因,错误未完整。我认为其余部分只是 c3p0 池连接工具生成的不相关信息。 假设 起初,我认为这可能是 c3p0
每当我有两个水平并排的元素并指定了右和/或左填充和/或边距时,元素之间通常会在我指定的上方和上方有空格。我希望有人能告诉我如何消除该空间(没有像负边距这样的笨拙东西)。 请注意:我并不是在寻找替代的多
String[] parts = msg.split(" +\n?"); String room = parts[0]; System.out.println(msg); Sy
我知道“一定有什么东西被改变了”,但我的代码似乎在一夜之间无缘无故地崩溃了。 我的服务器目录结构是这样的: / /scripts /audit /other_things 我在“scripts”文件夹
我正在尝试了解 GCM 的工作原理。为此,我复制/粘贴 http://developer.android.com/ 的代码在“实现 GCM 客户端”部分中提出。 从服务器发送消息是可行的,但是当我的客
在生成随机整数时,我发现了一些有趣的事情(至少对我而言),我无法向自己解释,所以我想我会把它贴在这里。 我的需求很简单:我要生成随机积分 (Int32) ID 并旨在最大程度地减少冲突。生成时间不是问
在这里https://stackoverflow.com/a/19915925/4673197我了解到我可以通过设置 IFS 将字符串拆分为数组。 在这里https://stackoverflow.c
我现在正在为我的 CS 测试学习,并尝试编写代码,以明文形式给出整个 IMDB 数据库,找到电影中共同点最多的 Actor 。我已经差不多完成了,只是不断遇到一个奇怪的 KeyError。这是我的代码
在 Android 平台上开发了几个月之后,我仍然有一个悬而未决的问题。很久以前,我注意到我有一个 Activity 不符合应用程序主题的其余部分。这意味着默认情况下,Activity 的字体颜色是白
本周,我注意到我的团队 Azure 门户上有一个持续的网络作业。 团队中没有人表示他们已经部署了它,或者熟悉它。我找到了这个博客: https://azure.microsoft.com/en-ca/
所以我正在制作一个小型闲置游戏,我的部分努力是格式化所有数字,以便它们之间有逗号(出于美观目的)。我成功地让我的货币 Energy 带有这些逗号,但我很难添加其他变量。我用了num.toLocaleS
我遇到了一个我以前从未见过的奇怪问题,我认为它一定是一些我在代码中没有看到的简单问题。 我有一个项目,其中定义了 2 个 Windows 服务。一个我称为 DataSyncService,另一个称为
我有这个jsfiddle一次有效。 function toggle_off(itemID){ alert(itemID+'->'+document.getElementById(itemID).g
更新:已解决,我是白痴,谢谢大家! Okay little bit weird.. I just created a layout file for list items, I can see it
问题:这段代码究竟在做什么? 另外:“w”的使用方式是否是某种现有算法?我试图弄清楚函数的意图,或者至少描述它产生的数字种类。 上下文:我正在查看 Martin O'Leary 的“Fantasy M
你能帮帮我吗?我正在将自己传递给它自己的纯虚函数。 n->dataCallback(handler, n, hangup); 其中 n 是我的类指针,dataCallback 是它自己的(纯)虚函数(
我知道这里有数百万篇关于这个异常(exception)的帖子,但我不明白这里的这个。我有一个极端简单的示例管道服务: [ServiceContract] public interface ISRARi
此代码有效,但它如何不实际调用任何列出的方法。它有效,但它的工作原理和原因似乎几乎是神奇的。我实际上从未调用过 Equals、GetHashCode 或 Compare,但代码有效。我也从不在实现两个
警告: Element 'TextStyle' from SDK library 'ui.dart' is implicitly hidden by 'text_style.dart'. 代码摘录:
我有一个似乎无法解开的谜。我有这个非常简单的单元测试,它使用了一个非常简单的自定义属性。该属性仅添加到甚至未实例化的 1 个类。我计算属性被构建的次数。由于类 MyDummyClass 上的属性,我希
我是一名优秀的程序员,十分优秀!