c - 如何在 Lex 和 Yacc 中标记字符串

标签 c yacc lex

引用Reading new line giving syntax error in LEX YACC 我们正在使用的 lex 文件

 %{
        /*
            parser for ssa;
        */

    #include<stdio.h>
    #include<stdlib.h>
    #include"y.tab.h"


    %}
    %%
    [\t]+   ;
    \n  ;



    [if]+       printf("first input\n");
    [else]+     return(op);
    [=]+        return(equal);
    [+]+        return(op);
    [*]+        return(op);
    [-]+        return(op);

    [\<][b][b][ ]+[1-9][\>] {return(bblock);}

    ([[_][a-z]])|([a-z][_][0-9]+)|([0-9]+)  {return(var);}

    .   ;




    %%

如果我想获取字符串形式的 token ,即 a_2,我该怎么办 怎么办呢???

输入文件是

a_2 = _6 + b_3; 
  a_8 = b_7 - c_5;
<小时/>

最佳答案

您可以在 bison 文件中定义 token 类型:

%union{
 char *string;
}

%token <string> var

然后替换

return(var);

yylval.string=malloc(yyleng); sprintf(yylval.string,"%s",yytext);return var;

关于c - 如何在 Lex 和 Yacc 中标记字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22576587/

相关文章:

Lex 保留字规则与查找表

Bison 的冲突

c - C 多线程环境中的浮点损坏

c - 为什么我的函数在 C 中不能正常工作?

c - 这可以自定义 printf 吗?

c - Yacc, union 结构指针

compiler-construction - 是否有任何语言语法示例可以让Yacc表达但不能为Antlr4表达?

c - 在下面的代码中,*ptr 如何与 (.) 一起使用以及 (&test) 与 -> 一起使用?

c - 我如何应对/创建/规避 Yacc/Bison 中处理多个 %types 的规则?

C with Bison+Flex 检查规则文件