I had researched recursive descent parsers, but seemed overly complex for the challenge set to us. So I thought to use this forum as I wasn’t making any headway. I have seen a solution on the 1st Edition forum for this chapter, using a recursive method (as opposed to splitting into new arrays, appending, removing, etc).
It works great, is succinct without having to add significant amounts of code. But I’m struggling to see how its actually working. My interpretation is:
- Adjust the Token enum to include the extra parameters - no problems, was the 1st thing I added myself anyway
- Adding extra case statements to the lex() function to add as tokens - again, extension of the above, already did this
- Changed the parse() function so that it now has a changed flow
my best explanation for this “flow”:
- Multiply & divide cases are first - presuming because the order the maths needs to occur in
- They call the getNumber() function as normal (value has a value, this loop through the token has a mulitply value, and the nextNumber gets the next number and multiplies them together.
- Switch statement continues to the Plus / Minus cases, where it does a recursive call to the parse() function. i think this is to skip onto the next token value? But why wouldn’t we use advance()?
Can someone explain the workings please? My brain works easiest with graphically explaining, if its possible to do…