0

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

Community
  • 1
  • 1
william
  • 3
  • 1
  • 4
  • You need to treat the identifier as a string, not an integer. So, your grammar can access `yytext` (carefully), or you can set up a more complex `YYSTYPE` which has a union of types; integers for integers, and strings for identifiers. (As an aside: you should probably diagnose when you discard unrecognized input — the `.` rule should have an action identifying when it is invoked.) – Jonathan Leffler Mar 27 '14 at 06:05
  • What does SSA have to do with this question? – leppie Mar 27 '14 at 06:13
  • Are you trying to parse the integer out of the token (e.g. a_9 => 9) and store it in yylval or retain the original string token and put that in yylval? – emsworth Mar 27 '14 at 20:15
  • guys thanks for replying...actually i am working in a project where i am taking ssa as a input and generating graph(textual format), so i am parsing here the three address code, e.g a_2 = _6 + b_3; . so i am not getting idea that how can i tokenize the variable so that it can hold integer value.....jonathan should i use union, will it solve the problem....while i am generating textual format,i need to have variable in ssa format e.g a_1,a_2.....if anyone can give me any link or pdf which will help me.... :) – william Apr 01 '14 at 06:43

1 Answers1

0

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();
}
emsworth
  • 1,149
  • 10
  • 21