Ale*_*lex 4 string bison flex-lexer
我正在编写一个Bison/Flex程序,将LaTeX转换为MathML.目前,处理函数(即\ sqrt,\ frac等)的工作方式与此类似,每个函数都有一个标记
\\frac {return FUNC_FRAC;}
Run Code Online (Sandbox Code Playgroud)
并将令牌FUNC_FRAC传递回bison,bison在此子树的描述中起作用:
function: FUNC_FRAC LBRACE atom RBRACE LBRACE atom RBRACE {$$ = "<mfrac>" + $3 + $6 + "</mfrac>";}
Run Code Online (Sandbox Code Playgroud)
但这意味着我需要定义和处理可能无限数量的令牌.我想做的是这样的事情,它不能像书面那样工作.在flex中:
\\[A-Za-z]+[0-9]* {return the-matched-string;}
Run Code Online (Sandbox Code Playgroud)
在野牛:
function: "\frac" LBRACE atom RBRACE LBRACE atom RBRACE {$$ = "<mfrac>" + $3 + $6 + "</mfrac>";}
Run Code Online (Sandbox Code Playgroud)
Flex应该将抽象令牌值返回给Bison.
您可以在Flex中找到值中的lexeme(匹配的字符串):
yytext
Run Code Online (Sandbox Code Playgroud)
所以你可以这样做:
{id} { yylval->strval=strdup(yytext); return(TOK_ID); }
Run Code Online (Sandbox Code Playgroud)
等等.该yylval结构将IIRC与野牛联盟/无论你用什么来评估过去的令牌类型......所以我可能会在Bison
%union {
char *strval;
int intval;
node node_val;
}
Run Code Online (Sandbox Code Playgroud)
返回令牌类型以外的任何内容都会破坏Bison中的自动机.您的Bison行动可以访问,例如:
id_production: TOK_ID
{
$<node_val>$ = create_id_node(yylval.strval);
xfree(yylval.strval); // func makes a copy, so we are cool.
}
Run Code Online (Sandbox Code Playgroud)
等等.除此之外的任何解释,我可能会开始重复文档.咨询事项:
祝好运
| 归档时间: |
|
| 查看次数: |
4459 次 |
| 最近记录: |