I'm studying the dragon book and can't wait to write an expression parser.
In order to deal with negative number input, my lexer reads digits when meet the symbol '-' to return a number token.
"-4+2" Will get (-4,number) (+,operator) (2,number)
but then I found that it can't do things as easy as "4-2" because
(4,number) (-2,number) It's a wrong syntax.
One of my solution is doing some pre-processing before evaluating an expression, such as appending a zero if the first token is a minus. I'm wondering how you guys deal with this situation?
Thanks.
You should have the following grammar, but not make "-" number
into a token.
number := DIGIT+
unary := number
unary := "-" unary
expr := expr "+" unary
expr := expr "-" unary
...
As there are unary expressions, it is not a operator-precedence grammar. You should parse it with a more complex parser.
my lexer reads digits when meet the symbol '-' to return a negative number
Don't. Unary operators should be dealt with by the parser, not the lexer.
One of my solution is doing some pre-processing before evaluating an expression, such as appending a zero if the first token is a minus.
No. Fix the problem.
When you're in a hole, stop digging.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.