gpt4 book ai didi

c - yacc生成的.y文件如何恢复.c文件?

转载 作者:行者123 更新时间:2023-11-30 17:43:28 25 4
gpt4 key购买 nike

我最近下载了一个名为re1(Goggle)的开源项目,因为我当前的研究主题是关于nfa和dfa的正则表达式匹配。 re1是一个非常非常简单的小项目,但是有一个parse.y文件但是我从来没有遇到过。经过google,我知道它是由yacc(又一个编译器编译器)生成的。还有一个makefile,所以我可以在Linux中运行它,但现在我想在Visual Studio(Windows)中运行它,因为我需要一步步调试(F5,F10,F11等非常有用)。但是现在它无法在VS中构建,因为VS无法识别它的.y文件,有很多“错误LNK2019:无法解析的外部符号”。我不知道如何解决,我可以将其转换或恢复为.c 文件吗?怎么做?

以下是parse.y的一部分:

%{
#include "regexp.h"

static int yylex(void);
static void yyerror(char*);
static Regexp *parsed_regexp;
static int nparen;

%}

%union {
Regexp *re;
int c;
int nparen;
}

%token <c> CHAR EOL
%type <re> alt concat repeat single line
%type <nparen> count

%%

line: alt EOL
{
parsed_regexp = $1;
return 1;
}

alt:
concat
| alt '|' concat
{
$$ = reg(Alt, $1, $3);
}
;

concat:
repeat
| concat repeat
{
$$ = reg(Cat, $1, $2);
}
;

repeat:
single
| single '*'
{
$$ = reg(Star, $1, nil);
}
| single '*' '?'
{
$$ = reg(Star, $1, nil);
$$->n = 1;
}
| single '+'
{
$$ = reg(Plus, $1, nil);
}
| single '+' '?'
{
$$ = reg(Plus, $1, nil);
$$->n = 1;
}
| single '?'
{
$$ = reg(Quest, $1, nil);
}
| single '?' '?'
{
$$ = reg(Quest, $1, nil);
$$->n = 1;
}
;

count:
{
$$ = ++nparen;
}
;

single:
'(' count alt ')'
{
$$ = reg(Paren, $3, nil);
$$->n = $2;
}
| '(' '?' ':' alt ')'
{
$$ = $4;
}
| CHAR
{
$$ = reg(Lit, nil, nil);
$$->ch = $1;
}
| '.'
{
$$ = reg(Dot, nil, nil);
}
;

%%

static char *input;
static Regexp *parsed_regexp;
static int nparen;
int gen;

static int
yylex(void)
{
int c;

if(input == NULL || *input == 0)
return EOL;
c = *input++;
if(strchr("|*+?():.", c))
return c;
yylval.c = c;
return CHAR;
}

void
fatal(char *fmt, ...)
{
va_list arg;

va_start(arg, fmt);
fprintf(stderr, "fatal error: ");
vfprintf(stderr, fmt, arg);
fprintf(stderr, "\n");
va_end(arg);
exit(2);
}

static void
yyerror(char *s)
{
fatal("%s", s);
}


Regexp*
parse(char *s)
{
Regexp *r, *dotstar;

input = s;
parsed_regexp = nil;
nparen = 0;
if(yyparse() != 1)
yyerror("did not parse");
if(parsed_regexp == nil)
yyerror("parser nil");

r = reg(Paren, parsed_regexp, nil); // $0 parens
dotstar = reg(Star, reg(Dot, nil, nil), nil);
dotstar->n = 1; // non-greedy
return reg(Cat, dotstar, r);
}

我尝试删除这些符号,如%、token和type,但我不知道如何解析规则(%%),当然它不起作用,我在VS中该怎么办,VS支持yacc吗?

最佳答案

有两个工具可以齐头并进。

Flex 和 Bison,“Lex 和 Yacc”的 GNU 团队。

有一本很棒的书叫“Flex and Bison”,另一本叫“Lex and Yacc”,它们真的很值得一读,你可以花几美分买到二手的。

Flex 和 Bison 是我所知道的两个最不受重视的东西。不断地重新实现。

了解已经发生的工作非常重要。不过,请勿尝试读取 Flex 的输出,这是由程序创建的优化结果,不适合人类使用!当您知道 Flex 的作用时,请阅读它的源代码。

GPL 许可证赋予您阅读其源代码的权利。享受它:)

顺便说一句,Unix 的东西,unix 哲学,告诉我们编写程序,为我们编写其他程序,用人类可读的文本交谈,只做一项工作,但做得很好。

Flex 和 Bison 就是最好的例子。

雷蒙德的另一句著名名言:“为 future 做好准备,因为它会比你想象的更快到来”,两者都还在这里,而且仍然非常好。

关于c - yacc生成的.y文件如何恢复.c文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20232642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com