Search code examples
compiler-constructionlanguage-agnosticescapingquoteslanguage-design

Could a language be designed that does not require the escaping of quotes in string literals?


In C++ (and, after translation, most languages) the following is of course a syntax error:

std::string str = "Hello "Jesus""; // oopsquotes

Could a C++-like language be created that doesn't need these quotes escaping? Could a compiler see a line like the above and intelligently determine that I didn't want the string to terminate after Hello, in the general case?

Languages and compilers like to require us to write precise syntax to avoid ambiguities, but I can't seem to think up a non-contrived example similar to the above where the meaning could be anything but "please put Hello "Jesus" in a string". In C++, "Jesus" would have to be a preprocessor macro that expanded to some string literal "x", for the above to potentially mean anything else. Is it very important to support this potential case in code where no such expansion exists?

So, could a language be created where we didn't need to escape quotes in a string literal? Can you think of any non-contrived counter-examples? Should a language like this exist? Perhaps one already does...?

Discuss.


Solution

  • It is relatively easy to implement in a PEG-based parser, utilising its infinite lookahead capability. But, as the others already mentioned, there is no point in doing it, as it won't always be possible to resolve the ambiguities, especially in cases when you want to embed a well-formed code into a string. It might be somewhat easier if you disallow multi-line strings.