CSC8503: Principles of Programming Languages - AEST - Information Technology Assessment Answer

Download Solution Order New Solution
Subject Code: CSC8503

Information Technology Assessment Answer

Assignment Task: Part A – Haskell Complete the following Haskell function definitions. Unless stated otherwise do not use library functions that are not in the Haskell standard prelude. This constraint is so that you gain practice in simple Haskell recursive programming. The Haskell 2010 standard prelude definition is available at https://www.haskell.org/onlinereport/haskell2010/haskellch9.html You may, however, write any ‘helper’ functions that you need to be able to solve any of the following problems. The testing process may use many more test cases than the ones shown in the specification. So, please test your functions extensively to ensure that you maximise your marks. 1. [3 marks] Write the function crypt :: String -> String -> Either String Int crypt cipher plaintext encrypts plaintext using the translation contained in cipher. The plain text includes only the 26 lower case letters plus the punctuation characters ‘.’ (full stop) and ’ ’ (space). The cipher is a string of 28 printable characters which indicate how each plain text character is translated. The plain text character ‘a’ is encoded as the first character of cipher. The plain text ‘b’ is encoded as the second character of cipher, and so on. Every character in cipher must be distinct — there can be no repeated entries. If the string "ZqXwCeVrBtyUaszxImOKlFfdPcQv" is used as the cipher then crypt applies the following translation: plain text a b c d e f g h i j k l m n o p q r s t u v w x y z . encrypted text Z q X w C e V r B t y U a s z x I m O K l F f d P c Q v crypt returns either an encrypted string (value Left encrypted) or an error code (value Right code). The Either data type is defined in the Standard Prelude as data Either a b = Left a | Right b The possible errors and their associated codes are as follows: 1 The ciphertext is not 28 characters long. 2 The ciphertext contains some non-printable characters. 3 The ciphertext contains some repeated characters. 4 The plain text contains characters that are not either lower case alphabetic or space or full stop. 2. [2 marks] Write the function insertAt :: Int -> a -> [a] -> [a]. insertAt n x xs will insert the element x into the list xs at position n items from the beginning of xs. In other words, skip n items in xs, then insert the new element. You can assume that n will be a non-negative number. If n is greater than the length of the list xs then add it to the end of the list. For example insert 3 ’-’ "abcde" ? "abc-de" insertAt 2 100 [1..5] ? [1,2,100,3,4,5] Hint: Use standard prelude functions ++ and split. 3. [2 marks] Write a recursive function maxList :: Ord a => [a] -> a that returns the largest value held in the argument list. You can assume that there is always at least one element in the list. For example: maxList [1] ? 1 maxList [1,2,5,4,2,4] ? 5 maxList [1,2,5,4,2,4,10] ? 10 Hint: you may wish to use the standard prelude function max :: Ord a => a -> a -> a 4. [1 mark] Recode the list maximum problem to use foldl. Call your function maxListF. This function will not recursively call itself 5. [2 marks] Write a function isAsc :: Ord a => [a] -> Bool that tests whether a list is sorted in ascending order. For example: isAsc [1,2,3] ? True isAsc [1,2,2,1] ? False isAsc [1,2,2] ? True 6. [1 mark] Write the function insertBefore :: String -> String -> String -> String. insertBefore pat str xs will insert str into the string xs at position immediately before the pattern pat. If pat is not in xs then no insertion will occur. For example insertBefore "xx" "aa" "bbbb" ? "bbbb" insertBefore "xx" "aa" "bbbbxx" ? "bbbbaaxx" insertBefore "" "aa" "bbbbxx" ? "aabbbbxx" Hint: Use the functions insertAt which you have just written (question 2) and the function findStr shown below: findStr :: String -> String -> Maybe Int findStr pat s = find’ 0 (length pat) pat s find’:: Int -> Int -> String -> String -> Maybe Int find’ i [] = Just ifind’ [] = Nothing find’ i n pat xs = if pat == take n xs -- is pat at start of xs? then Just i else find’ (i+1) n pat (tail xs) 7. There are two parts to this question; do part (b) first. (a) [1 mark] Write the function barchart :: Int -> [(String,Int)] -> String, that takes a width specifier (say w), and a list of (label,value) pairs and draws a horizontal bar chart. For example bar chart 5 [("xxxx",5),("yyy",8),("zzzzzzzz",3)] ? "xxxx *****\nyyy ********\nzzzzz ***\n" or using putStr for nicer display: putStr $ barchart 5 [("xxxx",5),("yyy",8),("zzzzzzzz",3)] ? xxxx ***** yyy ******** zzzzz *** (b) [1 mark] To implement bar chart, first write the simpler function: bar :: Int -> (String,Int) -> String that creates just a single line of the bar chart. One ‘*’ character is drawn for each value, and the label is shown padded out to w characters wide or truncated to w characters wide. Then bar chart can be written in terms of bar, map and unlines. Note: unlines:: [String] -> String is a standard prelude function that takes a list of strings and joins them together (similar to concat) after adding a newline character to the end of each string. The result is a single string that displays as a sequence of lines. Part B – SPL Part B of this assignment is submitted by pushing your local SPL repository to your remote USQ repository. I will pull from your USQ remote repository and mark the code in the branch ass2 or, if you wish to use separate branches for each question, ass2q1, ass2q2, ass2q3. See https://tau.usq.edu.au/courses/CSC8503/git.html for more information about Git and the USQ remote repository. I will pull your code and mark this assignment following the due date unless you have received an official extension. There is no “submit” action on your part except to make sure you have pushed your assignment branch(es) by the due date • Information about SPL, and using the Bison compiler generator • Laboratory exercises to guide your learning of SPL • Information on obtaining the SPL source code and using Git repositories to manage and submit your assignment. You are required to make a number of modifications to the SPL compiler to implement new features of the SPL language. Each of the following questions is independent in that they do not rely on any of the other modifications. In marking the assignment, they will be tested independently. I will be compiling and running your SPL system so your code must compile (make) without errors. If you are unable to get some parts working and GCC or Bison compile errors exist, then comment out the error-producing code so that I can compile and execute what you do have working. 1. [3 marks] Implement an autoincrement operator for variables. The syntax for these new operations is described by the extended factor grammar rule factor ? ++id | id++ | id | num | ( expression ) | - expression These have the same semantics as the C operators of the same kind. The value of pre-increment expression ++x is the current value of x, plus one, while the value of post-increment expression x++ is the current value of x. Evaluation of both operators would increase the stored value of x by one. You should implement autoincrement for all kinds of variables (globals, locals, and parameters). The simplest kind is global variables because CPU instructions can access them with DIRECT addressing. I suggest that you start by implementing globals; locals and parameters will attract less marks than globals. Consider the following program. var a,b; { a := 1; b:=1; display a,b; b := ++a; display a,b; b := a++; display a,b; } 4 On execution it should produce as output the sequence 1, 1, 2, 2, 3, 2. You will need to modify the lexer lexer.c and the parser spl.y as follows: • Create new token name (say) INC in spl.y, and modify lexer.c to recognise the corresponding ++ symbol. Look at the way two-character symbols like ‘>=’ are handled. Make sure that you update dispToken(). • Add grammar rules for the two new factor alternatives in spl.y. • Generate the increment code for the two increment operators. Use the current rule for factor: IDENT as a basis. You will need to generate add and move operations. You’ll probably need a new temporary register, whose number will be stored in a variable like a reg, to store the operand ‘1’. 2. [2 marks] Implement a do ... until post-tested loop. The statement has the syntax: do statement+ until condition; Note that the body is a list of statements. This is different from the ‘while’ loop whose body is a compound statement. Also, note the trailing semicolon. You will need to modify the lexer lexer.c and the parser spl.y as follows: • Create new token name (say) UNTIL in spl.y, and modify lexer.c to recognise the corresponding until reserved word. Make sure that you update dispToken(). • Add the ‘until’ loop grammar rule to spl.y. • Add actions to the loop rule to generate corresponding code. Use the existing ‘while’ code for guidance, but beware the semantics are different. Most importantly, the condition test follows the body of the loop, so the conditional jump address does not need to be backpatched into the jump instruction. Also, unlike the ‘while’ loop, this loop terminates when the condition is true 3. [3 marks] Implement simple global constant identifiers. (Do not implement procedure local constants.) The declaration has the EBNF form const id = num {, id = num}; There may be zero or one constant declaration statement(s) (i.e. it is optional). For example, you could declare const max = 100. You will need to do the following: • Create a new token name (say) CONST in spl.y, and modify lexer.c to recognise the corresponding const reserved word. Make sure that you update dispToken(). • Add grammar rules to spl.y for (1) a constant declaration and (2) a list of constant declarations; modify the program rule to include the constant declarations. • Modify the symbol table to record the constant identifier’s value. Modify st.h to add a new identifier class and add a ‘value’ attribute to the attr structure. Modify list st in st.c so that the value and not the address of constant identifiers are displayed. • Add actions to spl.y. You should – Add a symbol table entry when a constant declaration is recognised. – Generate the correct machine code instruction to load a constant value into a register when a constant IDENT in the factor rule is recognised.
This Information Technology Assessment has been solved by our Information Technology experts at My Uni Paper. Our Assignment Writing Experts are efficient to provide a fresh solution to this question. We are serving more than 10000+ Students in Australia, UK & US by helping them to score HD in their academics. Our Experts are well trained to follow all marking rubrics & referencing style.

Get It Done! Today

Country
Applicable Time Zone is AEST [Sydney, NSW] (GMT+11)
+

Every Assignment. Every Solution. Instantly. Deadline Ahead? Grab Your Sample Now.