Question

Is there a way to create a uniquie identifier for each token that flex reads?

ex:

flex:

"+"     {yylval.string =yytext;return PLUS;}

[0-9]+ {
yylval.string =yytext;
return INT;}

bison:

E: INT PLUS INT 
{
Node($1.ID);Node(PLUS.ID);Node($3.ID);Edge(PLUS.ID,$1.ID);Edge(PLUS.ID,$3.ID);
}

Every time there is input unique nodes are created. This is only an example of what i'd like to accomplish. When Node(...) and Edge(...) are called there are prints to a file that is later converted to a graph. I'd like them unique because I want to create this:

enter image description here

and not this:

enter image description here

Other types of solutions are also welcome.

Was it helpful?

Solution

The obvious and easiest way to do this would be to use a global variable, and increment it each time you need a new node ID. Whether you do this in the lexer or the parser is more or less irrelevant; there are arguments either way. If you do it in the parser, though, you could use a local variable in case you're allergic to globals (although in that case, you could also use a "reentrant" flex scanner.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top