4
votes

I am currently working on a verilog parser using bison and flex as the tokenizer. My grammar works fine and now my goal is to store the data collected in a database. (If you don't know what verilog is, it doesn't really matter but you can find info here : http://www.verilog.com/VerilogBNF.html)

I am currently stuck on getting the token values. My grammar mainly consists of strings and some numbers.

I browsed the internet and find usefull stuff that lead me tu write this kind of lex rules :

[A-Za-z_]+[A-Za-z0-9_$]* {
    lline = yylineno;yylval.str = strdup(yytext);
    return K_IDENTIFIER;
}

In my bison input file .y I made some adjustments that are

#define YYSTYPE char*
....
%union {char* str;
  double val;
}
%token<str>K_IDENTIFIER
...
beginning of the grammar rules

Now, when i have a grammar rule that has a K_IDENTIFIER in it such as

module : K_MODULE K_IDENTIFIER list_of_ports_parameters K_POINTVIRG
{cout << "the value of K_IDENTIFIER is :"<< $2.str << endl;}
list_of_module_itemsE
    K_ENDMODULE

fdsfds

As you can see, i would like to print the value (which is a string) of K_IDENTIFIER. This is just an example to help me understand the mechanism behind that because later i will create some c++ object and fill information in them depending on what i read.

If you need more code to see my problem, feel free to ask. thank you

1
I'm pretty sure you meant to tag this with gnu-Flex and not Adobe Flex, so I retagged it.JeffryHouser
yeah thank you for the typo fixdjfoxmccloud
I cleaned up your formatting a little bit. If you have a large block of code, indent it by four spaces. It's much better than trying to make the backticks work.Chris Lutz

1 Answers

6
votes

With %union, the #define for YYSTYPE is not necessary; that may be part of the problem. Bison will automatically pick the proper member of the union so you only need $2 to reference the value, not $2.str, i.e.:

{ cout << "the value of K_IDENTIFIER is: " << $2 << endl; }