Question

with reference to How to tokenize String in Lex and Yacc but in this post i am looking for integer token

when i am getting input

a_2 = _6 + b_3;

in the lex file i want to get a_2 as integer type token, how can i get this token below is my lex file

 %{
        /*
            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]+)  { yylval=atoi(yytext); return(var);}

    .   ;




    %%

this is giving some binary value in digit

Was it helpful?

Solution

Here is a simple but complete example showing a way to process both ints and strings using a union (yylval). Note that I may not have understood all your rules/regex requirements, but this should get you started.

Lexer file: example.l

%{
#include "y.tab.h"
%}
SSAINTEGER [a-z\_0-9]*\_[0-9]+
%option noyywrap
%%
[0-9]+          { yylval.number = atoi(yytext); return NUMBER; }
{SSAINTEGER}    { yylval.string = strdup(yytext); return SSANUMBER; }
[ \t\n]+        /* eat up whitespace */
%%

Parser file example.y

%{
#include<stdio.h>
void yyerror (char const *s) {
       fprintf (stderr, "%s\n", s);
}
%}
%union {
    char * string;
    int    number;
}
%type <string>  SSANUMBER
%type <number>  NUMBER
%token NUMBER SSANUMBER
%%

myinputs: myinput
        | myinput myinputs

myinput :       NUMBER    { printf("Number: %d\n", $1); }
        |       SSANUMBER { printf("SSA Number: %s\n", $1); }
        ;
%%
int main()
{
    return yyparse();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top