|
On 30/09/2006, at 4:24 AM, Gunnar Zötl wrote:
actually, the unit recognizing such things would be the parser. The lexer just knows how to recognize tokens. However, if it is the parser recognizing negative numbers, and not the lexer, then the parser can easily be written in a way to recognize such stuff.
The way I did it in another project, it was at the lexer stage that the distinction was made. That is, when doing the lex, things like +1, -5, 42.23 are all considered a single lexical unit - a number.
However you obviously have a problem with handling things like a+1, because in this case you want 3 tokens and not 2. Thus, you tell the lexer with a flag, whether + and - are to be treated, at this point in the parse, as a token or potentially part of a number.
I think you will find that there is a very specific point in the parser (eg. after parsing an expression) that the very next token from the lexer should have the flag set (ie. treat a "+" or "-" as a single token).
- Nick