How to tokenize String in Lex and Yacc

谁说我不能喝 提交于 2019-12-23 05:20:20

问题


with reference to Reading new line giving syntax error in LEX YACC lex file we are using

 %{
        /*
            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);}

    .   ;




    %%

what should i do if i want to get token as a string i.e a_2 how to do it????

input file is

a_2 = _6 + b_3; 
  a_8 = b_7 - c_5;


回答1:


You can define token type in your bison file:

%union{
 char *string;
}

%token <string> var

and then replace

return(var);

with

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


来源:https://stackoverflow.com/questions/22576587/how-to-tokenize-string-in-lex-and-yacc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!