0

I have a situation where my language allows quotes strings but sometimes I want to interpret the contents of the quoted string as language constructs. Think of it as, say, eval function.

So to support quoted strings i need a lexer rule and it overrides my attempts to have a grammar rule evaluating things in quotes if prefixed with 'eval'. Is there any way to deal with this in the grammar?

MK.
  • 33,605
  • 18
  • 74
  • 111

2 Answers2

1

IMO you should not try to handle this case directly through the lexer.
I think I would leave the string as it in the lexer and add some code in the eval rule of the parser that calls a sub-parser on the string content.

greydet
  • 5,509
  • 3
  • 31
  • 51
1

If you want to implement an eval function, you're really looking for a runtime interpreter.

The only time you need an "eval" function is when you want to build up the content to compile at runtime. If you have the content available at compile-time, you can parse it without it being a string...

So... keep it as a string, and then use the same parser at runtime to parse/compile its contents.

Scott Stanchfield
  • 29,742
  • 9
  • 47
  • 65
  • I should make myself more clear. I'm not building a compiler or an interpreter; it is more like static analyzer. It can get information it needs from analyzing the string. – MK. Jan 12 '13 at 02:35
  • Be aware that if it was intended to be an eval, much of the data being evaluated with likely be unavailable statically... – Scott Stanchfield Jan 12 '13 at 07:30
  • yes, of course. But the nature of the language is such that I can get a lot of information. The strings passed to this "eval" function are always constant and do not reference any variables. It's silly, but I don't feel comfortable giving exact details about the nature of the language parsed (I have this tiny hope of productizing this some day) – MK. Jan 12 '13 at 16:13