Consider some input string s and some index i. The longest substring s[i], ... , s[j − 1] that is a valid expression consists of some number of terms added together. In all of these cases, it begins with a term. By magic, assume that the GetTerm routine already works. Calling GetTerm(s, i) will return pterm and jterm, where pterm is the parsing of this first term and jterm indexes the token immediately after this term. Specifically, if the expression has another term then jterm indexes the ‘+’ that is between these terms. Hence, we can determine whether there is another term by checking s[jterm]. If s[jterm] = ‘+,’ then GetExp will call GetTerm again to get the next term. If s[jterm] is not a ‘+’ but some other character, then GetExp is finished reading in all the terms. GetExp then constructs the parsing consisting of all of these terms added together.
Consider some input string s and some index i. The longest
substring s[i], ... , s[j − 1] that is a valid expression consists of some number of terms
added together. In all of these cases, it begins with a term. By magic, assume that the
GetTerm routine already works. Calling GetTerm(s, i) will return pterm and jterm, where
pterm is the parsing of this first term and jterm indexes the token immediately after this
term. Specifically, if the expression has another term then jterm indexes the ‘+’ that
is between these terms. Hence, we can determine whether there is another term by
checking s[jterm]. If s[jterm] = ‘+,’ then GetExp will call GetTerm again to get the next
term. If s[jterm] is not a ‘+’ but some other character, then GetExp is finished reading
in all the terms. GetExp then constructs the parsing consisting of all of these terms
added together.
Step by step
Solved in 2 steps