Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The verifier script is shown below. Please make sure it works with the verifier! The expected results are shown at the bottom. If you do,

The verifier script is shown below. Please make sure it works with the verifier! The expected results are shown at the bottom. If you do, I will be sure to upvote :)

import Stacklang1 {- Test Cases for Part 1 Stack Language 1 Test by loading hw4p1verifier.hs Call function runAllTests Results Test 1: Just [36] Test 2: Just [13] Test 3: Just [10] Test 4: Nothing Test 5: Just [10,2,3,4,5] Test 6: Nothing Test 7: Just [1,1,1,2,3,4,5] -} s1 :: Stack s1 = [1, 2, 3, 4, 5] s2 :: Stack s2 = [10]

t1 = [LD 3,DUP,ADD,DUP,MULT] t2 = [LD 3,ADD] t3 = [] t4 = [ADD, ADD, ADD, ADD] t5 = [LD 10, MULT] t6 = [MULT] t7 = [DUP, DUP] tList = [t1, t2, t3, t4, t5, t6, t7] sList = [[], s2, s2, [], s1, s2, s1]

runAll :: [Stack] -> [Prog]->Int-> String runAll [] _ _ = " " runAll _ [] _ = " "

runAll (s:ss) (p:ps) n = "Test "++show(n)++": "++show(run p s) ++ " " ++ (runAll ss ps (n+1)) runAllTests :: IO () runAllTests = putStrLn (runAll sList tList 1) --

--

image text in transcribed

image text in transcribed

Part 1: Stack Language 1 Consider the stack language S defined by the following grammar. S::=CC,SC::=LDIntADDMULTDUP An S1-program essentially consists of a (non-empty) sequence of commands/operations C. The meaning of an S1-program is to start with an empty stack and to perform its first operation on it, which results in a new stack to which the second operation in S (if it exists) is applied, and so forth. The stack that results from the application of the last command is the result of the program. The following are the definitions of the operations. - LD Int - loads its integer parameter onto the stack. - ADD - removes the two topmost integers from the stack and puts their sum onto the stack. If the stack contains fewer than two elements, ADD produces an error. - MULT - removes the two topmost integers from the stack and puts their product onto the stack. If the stack contains fewer than two elements, MULT produces an error. - DUP - places a copy of the stack's topmost element on the stack. If the stack is empty then DUP produces an error. Here is a definition of the abstract syntax and type definitions that you should use. type Prog =[Cmd] data Cmd =LDInt ADD MULT |DUP derivingShow type Stack =[ Int ] run :: Prog Stack Maybe Stack A program is ran with a stack. If there is an error in the program you can return "Nothing", otherwise return "Just" the contents of the stack. When defining the run function you will probably need auxiliary functions for the semantics of the individual operations. For example: semCmd::CmdStackMaybeStacksemCmd(LDn)=Just(n:s) Submit a file named Stacklang1 hs. The first line of the code should be module Stacklang1 where. You can use the following S-programs to test Stack Lang 1. stack1 =[1,2,3,4,5] test1 =[ LD 3, DUP, ADD, DUP, MULT ] test2 =[ LD 3, ADD ] test3 =[] test4 =[ ADD, ADD, ADD, ADD ] *Main> run test1 [] Just [36] *Main> run test1 stack1 Just [36,1,2,3,4,5] *Main> run test2 [] Nothing *Main> run test2 stack1 Just [4,2,3,4,5] *Main> run test3 [] Just [] *Main> run test3 stack1 Just [1,2,3,4,5] *Main> run test4 [] Nothing *Main> run test4 stack1 Just [15]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

=+ What would it look like? Who should deliver it?

Answered: 1 week ago