Your question (as your final paragraph hints) is not really about the lexer, it is about the correct design of the interface between the lexer and the parser. As you might imagine there are many books about the design of lexers and parsers. I happen to like the parser book by Dick Grune, but it may not be a good introductory book. I happen to intensely dislike the C-based book by Appel, because the code is not usefully extensible into your own compiler (because of the memory management issues inherent in the decision to pretend C is like ML). My own introduction was the book by PJ Brown, but it's not a good general introduction (though quite good for interpreters specifically). But back to your question.
The answer is, do as much as you can in the lexer without needing to use forward- or backward-looking constraints.
This means that (depending of course on the details of the language) you should recognise a string as a " character followed by a sequence of not-" and then another " character. Return that to the parser as a single unit. There are several reasons for this, but the important ones are
- This reduces the amount of state the parser needs to maintain, limiting its memory consumption.
- This allows the lexer implementation to concentrate on recognising the fundamental building blocks and frees the parser up to describe how the individual syntactic elements are used to build a program.
Very often parsers can take immediate actions on receiving a token from the lexer. For example, as soon as IDENTIFIER is received, the parser can perform a symbol table lookup to find out if the symbol is already known. If your parser also parses string constants as QUOTE (IDENTIFIER SPACES)* QUOTE you will perform a lot of irrelevant symbol table lookups, or you will end up hoisting the symbol table lookups higher up the parser's tree of syntax elements, because you can only do it at the point you're now sure you are not looking at a string.
To restate what I'm trying to say, but differently, the lexer should be concerned with the spelling of things, and the parser with the structure of things.
You might notice that my description of what a string looks like seems a lot like a regular expression. This is no coincidence. Lexical analysers are frequently implemented in little languages (in the sense of Jon Bentley's excellent Programming Pearls book) which use regular expressions. I'm just used to thinking in terms of regular expressions when recognising text.
Regarding your question about whitespace, recognise it in the lexer. If your language is intended to be pretty free-format, don't return WHITESPACE tokens to the parser, because it will only have to throw them away, so your parser's production rules will be spammed with noise essentially - things to recognise just to throw them away.
As for what that means about how you should handle whitespace when it is syntactically significant, I'm not sure I can make a judgment for you that will really work well without knowing more about your language. My snap judgment is to avoid cases where whitespace is sometimes important and sometimes not, and use some kind of delimiter (like quotes). But, if you can't design the language any which way you prefer, this option may not be available to you.
There are other ways to do design language parsing systems. Certainly there are compiler construction systems that allow you to specify a combined lexer and parser system (I think the Java version of ANTLR does this) but I have never used one.
Last a historical note. Decades ago, it was important for the lexer to do as much as possible before handing over to the parser, because the two programs would not fit in memory at the same time. Doing more in the lexer left more memory available to make the parser smart. I used to use the Whitesmiths C Compiler for a number of years, and if I understand correctly, it would operate in only 64KB of RAM (it was a small-model MS-DOS program) and even so it translated a variant of C that was very very close to ANSI C.
Here is some code of mine without the TypeError: __call__() missing 1 required positional argument: 'self'
. How do you create this error?
>>> class X(Exception):
def __call__(self):
raise self
>>> x = X()
>>> x()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
x()
File "<pyshell#3>", line 3, in __call__
raise self
X
>>> class Other(X):
pass
>>> o = Other()
>>> o()
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
o()
File "<pyshell#3>", line 3, in __call__
raise self
Other
Best Answer
You cannot just use any Unicode character in Python identifiers. There are rules governing what can be used:
The above is the Unicode equivalent to any number of letters, digits and underscores, as long as you don't start with a digit.
The U+2032 PRIME codepoint doesn't fall in any of those classes; it is considered Po - other punctuation instead.
I've linked the Unicode categories to codepoint.net queries for you so you can see what kind of characters are permitted. Your other identifier choices are all Ll lowercase letter characters, by the way.
You do want to be careful and use some common sense in what you use as identifiers. Keep them readable and recognisable. Preferably they should be easy to type too, so personally I'd steer away from using greek lowercase letters myself. I'd rather use
alpha
,gamma
,epsilon
ands_prime
instead here.