4

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

2
  • I'm pretty sure you meant to tag this with gnu-Flex and not Adobe Flex, so I retagged it. Commented Apr 28, 2011 at 13:13
  • 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. Commented Apr 28, 2011 at 20:21

1 Answer 1

6

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; }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that did the trick ! I know i should have register to stack overflow before ^^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.