Flex可以将字符串匹配返回到bison

时间:2023-02-01 09:41:04

I'm writing a Bison/Flex program to convert LaTeX into MathML. At the moment, dealing with functions (i.e. \sqrt, \frac, etc) works like this, with a token for every function

我正在编写一个Bison / Flex程序,将LaTeX转换为MathML。目前,处理函数(即\ sqrt,\ frac等)的工作方式与此类似,每个函数都有一个标记

\\frac         {return FUNC_FRAC;}

and passes the token FUNC_FRAC back to bison, which plays its part in the description of this subtree:

并将令牌FUNC_FRAC传递回bison,bison在此子树的描述中起作用:

function: FUNC_FRAC LBRACE atom RBRACE LBRACE atom RBRACE {$$ = "<mfrac>" + $3 + $6 + "</mfrac>";}

But this means that I need to define and juggle a potentially unlimited number of tokens. What I would like to do is something like this, which doesn't work as written. In flex:

但这意味着我需要定义和处理可能无限数量的令牌。我想做的是这样的事情,它不能像书面那样工作。在flex中:

\\[A-Za-z]+[0-9]*  {return the-matched-string;}

and in bison:

在野牛:

function: "\frac" LBRACE atom RBRACE LBRACE atom RBRACE {$$ = "<mfrac>" + $3 + $6 + "</mfrac>";}

1 个解决方案

#1


Flex should return the abstract token value to Bison.

Flex应该将抽象令牌值返回给Bison。

You can find the lexeme (the string matched) in Flex in the value:

您可以在Flex中找到值中的lexeme(匹配的字符串):

yytext

And so you can do:

所以你可以这样做:

{id}    { yylval->strval=strdup(yytext); return(TOK_ID); }

And so forth. The yylval struct relates IIRC to the bison union/whatever you are using to evaluate past the token-type .. so I might have in Bison

等等。 yylval结构将IIRC与野牛联盟/你用来评估过去的令牌类型相关联......所以我可能会在Bison

%union {
    char *strval;
    int intval;
    node node_val;
}

Returning anything other than a token-type will break the automaton in Bison. Your Bison actions can access such as:

返回令牌类型以外的任何内容都会破坏Bison中的自动机。您的Bison行动可以访问,例如:

id_production: TOK_ID
    { 
        $<node_val>$ = create_id_node(yylval.strval);
        xfree(yylval.strval); // func makes a copy, so we are cool.
    }

And so on. Any more explanation than this and I will probably start repeating documentation. Things to consult:

等等。除此之外的任何解释,我可能会开始重复文档。咨询事项:

  1. Dragon Book (as always)
  2. 龙书(一如既往)

  3. Modern Compiler Implementation in C (great for getting started)
  4. C中的现代编译器实现(非常适合入门)

  5. Bison docs
  6. Flex docs

Good Luck

#1


Flex should return the abstract token value to Bison.

Flex应该将抽象令牌值返回给Bison。

You can find the lexeme (the string matched) in Flex in the value:

您可以在Flex中找到值中的lexeme(匹配的字符串):

yytext

And so you can do:

所以你可以这样做:

{id}    { yylval->strval=strdup(yytext); return(TOK_ID); }

And so forth. The yylval struct relates IIRC to the bison union/whatever you are using to evaluate past the token-type .. so I might have in Bison

等等。 yylval结构将IIRC与野牛联盟/你用来评估过去的令牌类型相关联......所以我可能会在Bison

%union {
    char *strval;
    int intval;
    node node_val;
}

Returning anything other than a token-type will break the automaton in Bison. Your Bison actions can access such as:

返回令牌类型以外的任何内容都会破坏Bison中的自动机。您的Bison行动可以访问,例如:

id_production: TOK_ID
    { 
        $<node_val>$ = create_id_node(yylval.strval);
        xfree(yylval.strval); // func makes a copy, so we are cool.
    }

And so on. Any more explanation than this and I will probably start repeating documentation. Things to consult:

等等。除此之外的任何解释,我可能会开始重复文档。咨询事项:

  1. Dragon Book (as always)
  2. 龙书(一如既往)

  3. Modern Compiler Implementation in C (great for getting started)
  4. C中的现代编译器实现(非常适合入门)

  5. Bison docs
  6. Flex docs

Good Luck