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
endOfInput : BOOLEAN
i: INTEGER
readInput : STRING
symbol : CHARACTER
--error : STRING
--a,b,c,d : REAL
make (args : ARRAY [STRING]) is
local
do
-- Startup code...
if args = Void or else args.count<2
then
io.put_string("Please call the client with more than 1 arguments!")
else
io.put_real
(MAIN(args.item(1).to_real, args.item(2).to_real, args.item(3).to_real, args.item(4).to_real))
end
io.put_new_line
end -- make
-- stringLenght : INTEGER is 60 -- Queries
ReadNextSymbol is
do
i := i+1
if i > ReadInput.count
then endOfInput := true
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:= ReadTerm
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
a,b,c,d : REAL
--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
MAIN(a, b, c, d :REAL) : REAL is
--Initilize
do
endOfInput := false
i := -1;
ReadNextSymbol
if not endOfInput then
result := ReadExpression();
--CheckSuccess
end
end
end