1

I'm coding a C++ grammar in Jison and I've managed to correctly detect character literals with this regex:

\'([^\\\']|\\.)\'  return 'CHAR_LIT'

and then add it to the AST like this:

| CHAR_LIT
    {$$ = new yy.Ast('CHAR_LIT', [$1])}

However, with the input string literal '\n' the value of $1 evaluates to its string representation "'\\n'" rather than its actual character value "\n", which is what I want.

Is there any way to tell Jison that I want the character value rather than its representation? Otherwise, what is the best way in Javascript to parse a string with a character representation into its corresponding character value, for instance parse "\\t" into "\t"?

albertsgrc
  • 13
  • 4
  • It's your responsibility to parse character string literals if that's what you want. The program text you are parsing *is* a representation, and there is nothing built-in which would cause jison to guess that you want a transformation of a string value. – rici May 31 '16 at 18:08
  • I also guessed that, just wanted to make sure there was absolutely no way it could be done with jison alone, thanks! – albertsgrc May 31 '16 at 18:12
  • you could write a different jison grammar to just deal with character string literals, but that would probably be overkill; in JS, you can just use regular expressions substitutions (but remember to do all of them just once, to avoid double-decoding). – rici May 31 '16 at 19:11

0 Answers0