Reputation: 1193
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:
and not this:
Other types of solutions are also welcome.
Upvotes: 1
Views: 446
Reputation: 241671
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.)
Upvotes: 0