Write an F# program to evaluate arithmetic expressions written in the language g
ID: 3848650 • Letter: W
Question
Write an F# program to evaluate arithmetic expressions written in the language given by the following context-free grammar: E rightarrow n | -E | E + E | E - E | E * E | E/E | (E) In the above, n is an integer literal, -E is the negation of E, the next four terms are the sum difference, product, and quotient of expressions, and (E) is used to control the order of evaluation of expressions, as in the expression 3*(5-1) Use F# type definition (discriminated union type) to define an abstract syntax tree grammar based on the above concrete grammar by completing the following partial solution type Exp = Num of int | Prod of Exp * Exp | The example given above would simply be represented by Prod (Num 3, Diff (Num 5, Num 1)) To deal with possible division by zero, we make use of the built-in F# type in our evaluate function type 'a option = None | Some of 'a evaluate returns Some n in the case of a successful evaluation, and None in the case of an evaluation that fails due to dividing by zero. For example, > evaluate (Prod (Num 3 Diff (Num 5, Num 1)));; val it: int option = Some 12 > evaluate (Diff(Num 3, Quot(Num 5 Prod (Num 7, Num 0))));; val it: int option = None Write a recursive function evaluate to evaluate arithmetic expressions defined using the type definitions in (1) & (2), evaluate e should use pattern matching based on the discriminated union type to evaluate each of e's sub-expressions, it should also distinguish between the cases of successful or failed sub-evaluations. To get you started, here is the beginning of the definition of evaluate let rec evaluate = function | Num n rightarrow Some n | Neg e rightarrow match evaluate e with | Some n rightarrow Some (-n) | None rightarrow None | Sum (e1, e2) rightarrow match (evaluate e1, evaluate e2) withExplanation / Answer
type Exp = Num of int |Neg of Exp |Sum of Exp * Exp |Diff of Exp * Exp |Prod of Exp * Exp |Quot of Exp * Exp type 'a option = None | Some of 'a let rec evaluate o = let auxeval f a b = match evaluate a with |None -> None |Some x -> match evaluate b with |None -> None |Some 0 when (f 6 3 = 2) -> None |Some y -> Some (f x y) match o with |Num n -> Some n |Neg e -> auxeval (-) (Num 0) e |Sum (a, b) -> auxeval (+) a b |Diff (a, b) -> auxeval (-) a b |Prod (a, b) -> auxeval (*) a b |Quot (a, b) -> auxeval (/) a b //=============================== Testing ===================================// printfn "Evaluating an Abstract Syntax Tree " printfn "evaluate (Sum (Neg (Num 5), Num 0)) = %A" (evaluate (Sum (Neg (Num 5), Num 0))) printfn "evaluate (Prod (Num 3, Diff(Num 5, Num1))) = %A" (evaluate (Prod (Num 3, Diff(Num 5, Num 1)))) printfn "evaluate (Quot (Num 8, Sum(Num 3, Neg (Num1)))) = %A" (evaluate (Quot (Num 8, Sum(Num 3, Neg (Num 1))))) printfn "evaluate (Diff (Num 3, Quot (Num 5, Prod (Num 7, Num 0)))) = %A" (evaluate (Diff (Num 3, Quot (Num 5, Prod (Num 7, Num 0)))))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.