我刚刚重新编写了一些使用bison编写的类似编译器的代码.当我这样做时,我想知道现代的等价物是什么?是否有一个很好的.NET(或类似的)编译器编写框架,它采用BNF语法并拆分出一个执行解析的DLL?
我试图将XSS安全字符串插值方案的概念证明放在一起.
给定一个替换字符串,
"Hello <b>$planetoid</b>!"
Run Code Online (Sandbox Code Playgroud)
我希望将其分解为字面部分和替换("Hello<b>" planetoid "</b>!"),然后在字面部分上从左到右运行状态机.当我达到内插值(planetoid在上面)时,我需要能够从状态到达适当的转义函数.
有没有人知道如何使用lex/yacc/bison来派生状态机并能够将语法中的标签与输出状态相关联?我想派生一个状态机,我可以在javascript中使用它们,并尝试替换PHP的底层字符串实现.
我这样做的原因是描述在这里.
欢呼,迈克
拜托我需要你的帮忙.基本上,我在使用gcc编译时遇到此警告消息,并且无法推断出错误:以下是详细信息:
我收到的警告信息按字面意思如下:
y.tab.c:在函数'yyparse'中:y.tab.c:1317
警告:内置函数'strlen'的不兼容隐式声明
我的Lex文件看起来像:
%{
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "y.tab.h"
void yyerror(const char*);
char *ptrStr;
%}
%START nameState
%%
"Name:" { BEGIN nameState; }
<nameState>.+ {
ptrStr = (char *)calloc(strlen(yytext)+1, sizeof(char));
strcpy(ptrStr, yytext);
yylval.sValue = ptrStr;
return sText;
}
%%
int main(int argc, char *argv[])
{
if ( argc < 3 )
{
printf("Two args are needed: input and output");
}
else
{
yyin = fopen(argv[1], "r");
yyout = fopen(argv[2], "w");
yyparse();
fclose(yyin);
fclose(yyout);
}
return …Run Code Online (Sandbox Code Playgroud) 我有一个我正在写的语法,叫做portugol.文件名是基本的,但我选择调用我的c程序portugol.c.
所以,基本上,我必须这样做:
flex portugol.l ==> lex.yy.c
bison -dy portugol.y ==> y.tab.c and y.tab.h
gcc y.tab.c lex.yy.c portugol.c -o portugol.bin -lm -ly ==> portugol.bin
Run Code Online (Sandbox Code Playgroud)
(我也有portugol.h,但它与问题无关)
很长一段时间以来,我正在使用我称之为的shell脚本flexyagcc.sh.过程中没有错误.
所以现在我决定学习makefile.
我面临的问题是,由于一些奇怪的原因," bison -dy"我打电话后跟这个命令我没写:mv -f y.tab.c portugol.c
好吧,这破坏了我手工制作的源文件!
我尽我所能,但无法摆脱这个" mv".
我甚至做了一个sleep尝试过:
y.tab.c y.tab.h : portugol.y
@echo --- bison -----------------------------------------------
mv -f portugol.c ptg.c
$(BISON) $(BFLAGS) portugol.y
@echo --- bison sleeping --------------------------------------
sleep 5
-mv -f portugol.c y.tab.c
-mv -f ptg.c portugol.c
Run Code Online (Sandbox Code Playgroud)
但令我惊讶的是,我得到了以下事件(按此顺序): …
我的语法中有一个简单的规则,它查找空格的序列:
ws: ws|' ';
Run Code Online (Sandbox Code Playgroud)
当野牛看到这个规则时,它会抱怨:
警告:由于冲突,规则在解析器中无用:ws:ws
为什么会这样?我有一个简单的语法规则,寻找一个正则表达式?
我有以下Bison语法:
%error-verbose
%{
#include "node.h"
NBlock *programBlock;
#define YYDEBUG 1
extern int yylex();
void yyerror(const char *s) { printf("Error: %s\n", s); }
%}
%union {
Node *node;
NBlock *block;
NBody *body;
NHeader *header;
NExpression *expression;
NStatement *statement;
NIdentifier *identifier;
NVariableDeclaration *variableDeclaration;
NDoWhileStatement *doWhileStatement;
NWhileStatement *whileStatement;
NIfStatement *ifStatement;
NForStatement *forStatement;
std::vector<NVariableDeclaration*> *variableDeclarations;
std::vector<NExpression*> *expressions;
std::vector<NStatement*> *statements;
std:string *string;
int token;
}
/*
The %token directive is used to associate a type to a terminal symbol.
%token <type> 'terminal_list'
associates the specific …Run Code Online (Sandbox Code Playgroud) 在Bison中实现简单while循环的最佳方法是什么?如果它有所不同,我使用C,但我也可以使用C++.
我正在为输出C/C++代码的解析器生成器定义自己的语法.我为此选择了Bison,但遇到了问题.我想在规则中使用括号("(",")"),但是Bison不接受它们.例如,声明我使用的字符(三个点代替所有其他可能的ASCII字符):
character: "'" ("\0" | "\t" | "\n" | " " | "!" | ... | "}" | "~") "'";
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用括号表示字符必须带引号,任何字符,然后是另一个引号.
我为整数做了类似的事情:
integer: (["-"] digits) | "0";
Run Code Online (Sandbox Code Playgroud)
在Bison有没有办法实现类似的东西?或者,是否有一个解析器生成器接受EBNF,甚至只是括号并输出c ++代码?
我知道我可以%parse-param {struct my_st *arg}在.y文件中声明.所以yyparse()改变了yyparse(struct my_st *arg).但是我如何引用flex规则中的参数?例如:
[0-9]+ { do_work(arg); return NUMBER; }
Run Code Online (Sandbox Code Playgroud)
我想制作一个可重入的解析器,所以我需要这样做.请帮帮我,谢谢!
这是我的html.l:
DOC_START "<html>"|"<HTML>"
DOC_END "</html>"|"</HTML>"
SPACE " "
TEXT .
%%
%%
Run Code Online (Sandbox Code Playgroud)
这是我的html.y:
%{
#include "lex.yy.c"
%}
%%
Doc : DOC_START Other DOC_END
Other : TEXT
| SPACE
%%
Run Code Online (Sandbox Code Playgroud)
这是我的html文件:
<HTML>
foo bar
</HTML>
Run Code Online (Sandbox Code Playgroud)
我正在编译第一个flex文件,在bison文件之后.它给出了has no rules错误.我想检查一下,如果这个文件是一个正确的html文件,如Doc声明中所述.并且预计会给出错误或消息stdout.我们需要做什么?