indexing title: "Parser-Interpreter"; cluster: "Inf_3_Aufgabe_1"; project: "Inf_3_Aufgabe_1"; copyright: ""; author: "Silviya Marinska"; original: ""; version: "$Revision: 1.1 $"; last_change: "$Date: 2005/02/01 11:46:51 $"; done_at: ""; class MAIN inherit EXCEPTIONS create make feature {NONE}-- Creation make (args : ARRAY [STRING]) is do -- Startup code... end -- make feature -- stringLenght : INTEGER is 60 -- Queries endOfInput : BOOLEAN i: INTEGER readInput : STRING symbol : CHARACTER error : STRING is "Es ist Fehler aufgetreten" a,b,c,d : REAL Interpret :REAL is --Initilize local --done : BOOLEAN do endOfInput := false i := -1; ReadNextSymbol if not endOfInput then result := ReadExpression(); --CheckSuccess end --rescue -- done:= true --if developer_exception then --message := developer_exeption_name; --end end ReadNextSymbol is do i := i+1 if i > ReadInput.count then endOfInput := true --factorStr else symbol := ReadInput.item(i) end end -- readnextsymbol CheckSucces(condition:BOOLEAN;errormessage:STRING) is do if not condition then raise(errormessage) end end -- checksucces Value(left : REAL; operation :CHARACTER; right : REAL) : REAL is do if operation = '+' then result := left+right elseif operation = '-' then result := left-right elseif operation = '*' then result := left*right elseif operation = '/' then --CheckSuccess(right /= 0, "Division by 0") result := left/right end end ReadExpression : REAL is --[Add op] Term {Add Op Term} --Add Op = "+"|"-". local negative : BOOLEAN addOp : CHARACTER do negative := false if symbol = '-' THEN negative := symbol = '-' ReadNextSymbol else end result := ReadTerm() if negative then result := -result END from result:=result until endOfInput or symbol/='+' AND symbol/='-' loop addOp:= symbol ReadNextSymbol --checkSuccess(endOfInput = false, factorStr) result := Value(Result,addOp,ReadTerm()) end end ReadTerm : REAL is -- Term = Factor {Mul Op Factor} --Mul Op = "*"|"/" local term : REAL mulOp : CHARACTER do term := ReadFactor result := term from result := term --(endOfInput := false)and (symbol = "*") or (symbol = "/") until endOfInput or (symbol /= '*') and (symbol /= '/') loop mulOp := symbol ReadNextSymbol --CheckSuccess(endOfInput = false, factorStr) end result := Value(term, mulOp, ReadFactor()); end ReadFactor : REAL is --Factor = Ident| "(" Expr ")". do if (symbol>='a' AND symbol <= 'z') then result := ReadIdent() elseif symbol = '(' then ReadNextSymbol --CheckSuccess(endOfInput := false, ""+", "-" or" + factorStr ) result := ReadExpression --CheckSuccess ReadNextSymbol else --CheckSuccess(false, factorStr) end end ReadIdent : REAL is --Ident= "a"|"b"|"c"|"d". local --ident : REAL do inspect symbol when 'a' then result:=a ReadNextSymbol; when 'b' then result:=b ReadNextSymbol; when 'c' then result:=c ReadNextSymbol; when 'd' then result:=d ReadNextSymbol; else --CheckSuccess (false, "Unknown variable" symbol.out); end end end