How to tokenize String in Lex and Yacc

2019-06-12 10:49发布

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;

标签: c yacc lex
1条回答
倾城 Initia
2楼-- · 2019-06-12 11:51

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;
查看更多
登录 后发表回答