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 pr开发者_StackOverflow中文版ecise 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.
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.
in Python you can do str = 'Hello "Jesus"'
without problem or str = """Hello "Jesus"."""
Some languages use different types of quotes, thus making it possible to have quotes of different types in string literals. For instance, Python has this with double quotes, single quotes and three double quotes.
Bash has some form of "user-customizable" quoting mechanism:
cat <<EOF
Hello "World"
EOF
I like that, because in cases where your string literal contains EOF, you can just choose to use something else for the "end of quote" delimiter.
精彩评论