- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我认为我的程序并非在所有情况下都支持嵌套循环。这太荒谬了,但是这个 brainfuck 程序与我的拦截器一起正常工作并打印“Hello World!”到标准输出:
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
但是这个程序不能正常工作并且将内存错误打印到标准输出:
+[-[<<[+[--->]-[<<<]]]>>>-]>-.---.>..>.<<<<-.<+.>>>>>.>.<<.<-.
这两个示例与在线拦截器一起工作 - https://copy.sh/brainfuck/ .
我试图解决这个问题,但我花了很多时间,但我失败了,所以我请求你帮助我。
这是我的解释器的代码:
#include <stdio.h>
#include <sysexits.h>
#include <string.h>
#include <stdlib.h>
#define EX_SUCCESS 0
#define str1_is_less_than_str2 (strcmp("bf", file_extension) < 0)
#define str2_is_less_than_str1 (strcmp("bf", file_extension) > 0)
#define MEMSIZE 30000
#define NUMBER_OF_BF_INSTRUCTIONS 8
#define FIRST_ELEMENT_NOT_EXISTS (*head_ptr == NULL)
struct bf_instruction_node {
int bf_instruction;
struct bf_instruction_node *next_element;
};
struct stack_node {
struct bf_instruction_node *bf_instr_ptr;
struct stack_node *link;
};
void inc_ptr(int **values_ptr) { ++(*values_ptr); }
void dec_ptr(int **values_ptr) { --(*values_ptr); }
void inc_value(int *values_ptr) { ++(*values_ptr); }
void dec_value(int *values_ptr) { --(*values_ptr); }
void print_value(int *values_ptr) { putchar(*values_ptr); }
void input_value(int *values_ptr) { *values_ptr = getchar(); }
void push(struct stack_node **esp_ptr, struct bf_instruction_node *current_instr_ptr) {
struct stack_node *new_element_on_the_stack;
new_element_on_the_stack = (struct stack_node *)malloc(sizeof(struct stack_node));
new_element_on_the_stack->bf_instr_ptr = current_instr_ptr;
new_element_on_the_stack->link = *esp_ptr;
*esp_ptr = new_element_on_the_stack;
}
void pop(struct stack_node **esp_ptr) {
struct stack_node *tmp;
tmp = *esp_ptr;
*esp_ptr = (*esp_ptr)->link;
free(tmp);
}
void start_loop(int *values_ptr, struct stack_node **esp_ptr, struct bf_instruction_node *current_instr_ptr) {
push(esp_ptr, current_instr_ptr);
}
void end_loop(int *values_ptr, struct bf_instruction_node **current_instr_ptr, struct stack_node **esp_ptr) {
if( *values_ptr != 0 ) {
if( *values_ptr < 0 )
*values_ptr = 255;
if( *values_ptr > 255 )
*values_ptr = 0;
*current_instr_ptr = (*esp_ptr)->bf_instr_ptr;
// *current_instr_ptr = (*current_instr_ptr)->next_element;
} else {
pop(esp_ptr);
}
}
void execute_instructions(int **values_ptr, struct bf_instruction_node *head_ptr, struct bf_instruction_node **current_instr_ptr) {
struct stack_node *esp_ptr = NULL;
char brainfuck_instruction;
*current_instr_ptr = head_ptr;
while( *current_instr_ptr != NULL ) {
brainfuck_instruction = (*current_instr_ptr)->bf_instruction;
switch( brainfuck_instruction ) {
case '>': inc_ptr(values_ptr); break;
case '<': dec_ptr(values_ptr); break;
case '+': inc_value(*values_ptr); break;
case '-': dec_value(*values_ptr); break;
case '.': print_value(*values_ptr); break;
case ',': input_value(*values_ptr); break;
case '[': start_loop(*values_ptr, &esp_ptr, *current_instr_ptr);break;
case ']': end_loop(*values_ptr, current_instr_ptr, &esp_ptr); break;
}
*current_instr_ptr = (*current_instr_ptr)->next_element;
}
}
struct bf_instruction_node *create_new_element(struct bf_instruction_node **head_ptr, struct bf_instruction_node **current_instr_ptr, int char_from_file) {
struct bf_instruction_node *new_element;
*current_instr_ptr = *head_ptr;
while( (*current_instr_ptr)->next_element != NULL )
*current_instr_ptr = (*current_instr_ptr)->next_element;
new_element = (struct bf_instruction_node *)malloc(sizeof(struct bf_instruction_node));
return new_element;
}
void add_instruction_to_the_list(struct bf_instruction_node **head_ptr, struct bf_instruction_node **current_instr_ptr, int char_from_file) {
if( FIRST_ELEMENT_NOT_EXISTS ) {
*head_ptr = (struct bf_instruction_node *)malloc(sizeof(struct bf_instruction_node));
if( *head_ptr == NULL ) {
perror("Memory allocation failed");
exit(EXIT_FAILURE);
} else {
(*head_ptr)->bf_instruction = char_from_file;
(*head_ptr)->next_element = NULL;
*current_instr_ptr = *head_ptr;
}
} else {
struct bf_instruction_node *new_element = create_new_element(head_ptr, current_instr_ptr, char_from_file);
if( new_element == NULL ) {
perror("Memory allocation failed.");
exit(EXIT_FAILURE);
} else {
new_element->bf_instruction = char_from_file;
new_element->next_element = NULL;
(*current_instr_ptr)->next_element = new_element;
*current_instr_ptr = new_element;
}
}
}
void print_instructions(struct bf_instruction_node *head_ptr, struct bf_instruction_node **current_instr_ptr) {
*current_instr_ptr = head_ptr;
while( *current_instr_ptr != NULL ) {
printf("%c", (*current_instr_ptr)->bf_instruction);
*current_instr_ptr = (*current_instr_ptr)->next_element;
}
}
void clear_the_memory(struct bf_instruction_node *head_ptr, struct bf_instruction_node **current_instr_ptr) {
struct bf_instruction_node *earlier_element;
*current_instr_ptr = head_ptr;
while( (*current_instr_ptr) != NULL ) {
earlier_element = *current_instr_ptr;
*current_instr_ptr = (*current_instr_ptr)->next_element;
free(earlier_element);
}
puts("Memory is cleared.");
}
const char *is_bf_instruction(int char_from_file) {
const char bf_alphabet[NUMBER_OF_BF_INSTRUCTIONS] = {'>', '<', '+', '-', ',', '.', '[', ']'};
return memchr(bf_alphabet, char_from_file, sizeof(bf_alphabet));
}
const char *get_file_extension(const char *filename) {
const char *dot = strchr(filename, '.');
if( dot == NULL )
return NULL;
const char *file_extension = dot + 1;
return file_extension;
}
int main(int argc, char **argv) {
if( argc < 2 ) {
fprintf(stderr, "File not specified.\n");
puts("usage: ./bf_interpreter <filename.bf>");
return EX_USAGE;
}
const char *filename = argv[1];
const char *file_extension = get_file_extension(filename);
if( file_extension == NULL || str1_is_less_than_str2 || str2_is_less_than_str1 ) {
fprintf(stderr, "Incorrect file extension.\n");
puts("usage: ./bf_interpreter <filename.bf>");
return EX_DATAERR;
}
FILE *file_with_bf_code = fopen(filename, "r");
if( file_with_bf_code == NULL ) {
perror(filename);
return EX_NOINPUT;
}
int values[MEMSIZE] = {0}, *values_ptr = values;
int char_from_file;
struct bf_instruction_node *head_ptr = NULL, *current_instr_ptr;
while( (char_from_file = fgetc(file_with_bf_code)) != EOF ) {
if( is_bf_instruction(char_from_file) != NULL )
add_instruction_to_the_list(&head_ptr, ¤t_instr_ptr, char_from_file);
}
execute_instructions(&values_ptr, head_ptr, ¤t_instr_ptr);
clear_the_memory(head_ptr, ¤t_instr_ptr);
return EX_SUCCESS;
}
我认为这个函数会产生内存冲突的错误:
void end_loop(int *values_ptr, struct bf_instruction_node **current_instr_ptr, struct stack_node **esp_ptr) {
if( *values_ptr != 0 ) {
if( *values_ptr < 0 )
*values_ptr = 255;
if( *values_ptr > 255 )
*values_ptr = 0;
*current_instr_ptr = (*esp_ptr)->bf_instr_ptr;
// *current_instr_ptr = (*current_instr_ptr)->next_element;
} else {
pop(esp_ptr);
}
}
我希望有人能帮我解决这个问题。干杯,提前谢谢你!
最佳答案
你需要调试。
我试过你的代码。根据我是否使用优化得到不同的结果。这是未定义行为 的良好指示。通过优化 -O3
和 -O2
我得到了这个段错误:
Program received signal SIGSEGV, Segmentation fault.
execute_instructions (values_ptr=0x7ffffffe0ca8, head_ptr=<optimized out>, current_instr_ptr=0x7ffffffe0cb8) at bf.c:83
83 case ']': end_loop(*values_ptr, current_instr_ptr, &esp_ptr); break;
使用 -O1
得到:
$ ./a.out fil.bf
��Memory is cleared.
没有优化似乎陷入了无限循环。我让它运行了大约 5 秒钟,但什么也没发生。
此外,您的代码会产生两个警告:
$ gcc -Wall -Wextra -std=c11 -pedantic bf.c
bf.c: In function ‘start_loop’:
bf.c:48:22: warning: unused parameter ‘values_ptr’ [-Wunused-parameter]
void start_loop(int *values_ptr, struct stack_node **esp_ptr, struct bf_instruction_node *current_instr_ptr) {
^~~~~~~~~~
bf.c: In function ‘create_new_element’:
bf.c:90:139: warning: unused parameter ‘char_from_file’ [-Wunused-parameter]
struct bf_instruction_node *create_new_element(struct bf_instruction_node **head_ptr, struct bf_instruction_node **current_instr_ptr, int char_from_file) {
^~~~~~~~~~~~~~
关于c - brainfuck 中的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51579460/
是 Brainfuck如果单元是位,并且 + 和 - 操作只是翻转一点,那么图灵完备?是否有一个简单的证据表明无论单元大小如何,类 Brainfuck 的语言都是图灵完备的,还是我需要考虑一个模拟图灵
作为我的 Brainf*ck 实现的调试器/测试程序,我创建了以下计数循环: +[[>+]+] 对于单字节、环绕单元和 30k 单元,这会创建一个无限计数循环。它将每个单元格设置为 1,然后将每个单元
我想知道是否可以只用代码开头的数字 k 来计算 brainfuck 中 1+2+3+...+k 的总和? 例如是否可以像这样做 1+2+3: +++> (这里的代码创建了一个二和三个相加,创建一个并相
我正在研究一个直接解释 brainfuck 的物理面包板 8 位 CPU。 语言规范表明左括号和右括号都有逻辑: [ => Jump to matching ] If Zero ] => Jump t
我正在尝试在 Brainfuck 中制作一个我认为也称为“Brainflake”的程序,它将添加两个用 ASCII 数字字符输入的单位十进制整数,并在输出中以 ASCII 数字字符显示总和。我将如何着
例如,如果数组像 0 0 0 0 ... 0 0[n]s o m e d a t a 4 9 9 9 9 9 9 8 3 7 ...,如何移动指针通过 n,在指针移动后不改变 s o m e d a
有人把这个发给我,并声称这是 Brainfuck 中的一个 Hello World (我希望如此......) ++++++++++[>+++++++>++++++++++>+++>+++.>+.++
我正在尝试使用 BrainFuck 编写一个程序,该程序可以读取最多为 9 的两个数字,计算它们的总和,然后将结果打印出来,例如3 & 5 给出结果 8 。 我只是想了解 BF 语言,但它看起来比我想
有人可以在程序设计语言Brainf * ck中发布一个代码段,以将两个一位数字相乘吗? 最佳答案 ,>, go to cell #2 [ ->+>+> go to cell #4 [ -
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
你能创建一种像 Brainfuck 这样只有一个符号的编程语言吗? 最佳答案 是的,以前已经做过 - 请参阅 Unary . 基本上这是一种奇怪的 Brainfuck 编码。将每个 BF 命令视为一个
这是我正在使用的代码,它测试“b”的输入,如果有其他输入,应该输出“Y”。 拜托,没有人问我为什么选择使用“Y”作为错误值.. > , ----- ---- ----- ---
我有以下正在通过最小测试套件的 Brainfuck 解释器。除了一个相当大的问题,比如打印斐波那契数列似乎失败了(套件中的最后一个测试)。我解释执行失败的 brainfuck fibonacci 代码
我决定使用 Borland 的 CppBuilder6 用 C++ 编写一个简单的 Brainfuck IDE。我已经为 RichEdit 装上了我的,让它看起来有点像记事本。然后我为输入添加了 TE
我是 C 的新手。目前我正在尝试编写一个 Brainfuck 解释器。到目前为止,我已经尝试过了。 #include #include char *line; int curr_po
我认为我的程序并非在所有情况下都支持嵌套循环。这太荒谬了,但是这个 brainfuck 程序与我的拦截器一起正常工作并打印“Hello World!”到标准输出: ++++++++[>++++[>++
我坐下来尝试实现 BrainFuck。 syntax看起来很简单。我无法让愚蠢的事情发挥作用。我已经有一段时间了;我承认我需要 sleep 。也许这就是问题所在。解释器没有输出任何东西。我很确定问题很
是的,我正在制作 Brainfuck 解释器,但我还需要从其代码创建 AST。可以很容易地在节点中使用原始操作(+ - . , > rightBracketsIds; private rea
我写了一个 brainfuck ide 的 alpha 版本。我写了自己的解释器,虽然我从我的老师那里得到了关于代码循环的大量帮助,因为我很难以“IT 方式”理解它。现在对于我的报告,我需要解释器算法
我正在为该语言编写解释器 Brainfuck 我用的是命令行 java bfinterpreter/BFInterpreter >output.bin bf 程序应该输出 00 到 FF 的十六进制,
我是一名优秀的程序员,十分优秀!