CS 224 Lab 1Parsing

Objectives

  • Write basic parser functions in Haskell
  • Parse expressions
  • Perform the following tasks in the order given.

    1. Download the Lab1 project and import it into Eclipse as a Haskell project (select Haskell as the Perspective under the Window directory).
       
    2. Read through the notes that are provided.  We will complete the basic parser functions in this lab.
       
      Assignment:
      Complete Exercises 2 in the notes to write the function in the Parser module
           becomes
      :: Parser String
      which accepts the string ":=" and returns Nothing if it is unsuccessful.

      Be sure to test your functions with strings that will succeed and with strings that will fail.


       


    3.  
      Assignment:
      Complete Exercises 4 and 5 in the notes to write the functions in the Parser module
           letter
      :: Parser Char
           space
      :: Parser Char
      which accept and letter and a space character respectively.
      Then define
         alphanum :: Parser Char
      which accepts a letter or a digit.

      Be sure to test your functions with strings that will succeed and with strings that will fail.


       

    4. Assignment:
      Complete Exercises 7 in the notes to redefine the function
           becomes
      :: Parser (Char,Char)
      using twochars and the ? operator.  The definition should not include the string argument.


       


    5.  
      Assignment:
      Complete Exercises 9 in the notes to write the function in the Parser module
           sndchar
      :: Parser Char
      which accepts two characters and returns the second one.  Use twochars and the transformations operator >->


       


    6.  
      Assignment:
      Complete Exercises 11 and12 in the notes to write the functions in the Parser module
          (-#)
      :: Parser a -> Parser b -> Parser b
           (#-)
      :: Parser a -> Parser b -> Parser a
      which applies two parsers in sequence but throws away one of the results. (-#) throws away the first result and (#-) throws away the second result.


       


    7.  
      Assignment:
      Complete Exercise 15 in the notes to write the function in the Parser module
          require
      :: String -> Parser a
      The parser require w accepts the same string input as accept w but reports the missing string using err in case of failure


       

    8. The parser for expressions, given in the notes, is implemented in the Interpreter module.  Run this module and test out some expressions like:
              expr "x * 2 + 3"
              expr "(x * 2) + 3"

      Make sure that you understand the results that are given.

       

    9. Send me your modified zipped project in the dropbox in BlackBoard.