It is possible to get the string while you are in the lexer, precisely at the point that is matching the token (eg. Lexing.lexeme
). It is too late to try to get it in the parser. We don't want the token stream to keep all strings in memory as it would increase memory consumption a lot (in practice most tokens never need their string representation).
Why don't you build a reverse table from keyword values (or token values) to string, at the same time you build the first mapping ? hash_table
could be renamed into hash_tables
and return the two reverse maps.
It probably needs to be defined in the parser if you want it visible from both the parser and the lexer.