The procedure for dealing with multiplicative operators is much the same. In fact, at the first level, they are almost identical, so I'll just show them here without much fanfare. The first one is our general form for Factor, which includes parenthetical subexpressions:
{ Parse and Translate a Factor } function Expression: char; Forward; function Factor: char; begin if Look = '(' then begin Match('('); Factor := Expression; Match(')'); end else if IsAlpha(Look) then Factor := Load(GetName) else Factor := LoadNum(GetNum); end; { Recognize and Translate a Multiply } Function Multiply(T1: char): char; begin Match('*'); Multiply := PopMul(T1, Factor); end; { Recognize and Translate a Divide } function Divide(T1: char): char; begin Match('/'); DIvide := PopDiv(T1, Factor); end; { Parse and Translate a Math Term } function Term: char; var Typ: char; begin Typ := Factor; while IsMulop(Look) do begin Push(Typ); case Look of '*': Typ := Multiply(Typ); '/': Typ := Divide(Typ); end; end; Term := Typ; end; |
These routines parallel the additive ones almost exactly. As before, the complexity is encapsulated within PopMul and PopDiv. If you'd like to test the program before we get into that, you can build dummy versions of them, similar to PopAdd and PopSub. Again, the code won't be correct at this point, but the parser should handle expressions of arbitrary complexity.