module HW3 where {- For each of the following questions, put your answer directly below the question. This homework is due on Friday, October 16, by 3pm. Email your code directly to Adam Procter: amp269@mizzou.edu. (1) You must use a text editor (e.g., vi, textpad, emacs, etc.) to prepare your solution. (2) You must write type declarations for each and every one of your Haskell definitions. (3) The program you turn in must be the product of your effort alone. -} {- Question 1. Write the BNF grammar corresponding to Lam. data Lam = Var String | App Lam Lam | Lambda String Lam -} data Symbol = Nonterminal String | Terminal String data Production = String :--> [Symbol] {- Question 2. Write instance declarations for Symbol and Production. To display a non-terminal, wrap it with brackets "<" and ">". -} {- Question 3. The following data type represents grammars as separate sets of terminals and nonterminals (both represented by [String]) and a set of productions (represented by [Production]). For example, consider the grammar we saw in class: Exp --> Int Exp --> Exp + Exp Exp --> Exp * Exp This would be represented as a grammar by arithgrammar below. Note that a value of type Grammar may *not* represent a grammar. Say badgrammar = Grammar nts ts ps, then badgrammar is bad: 1. If there is a production in ps, x :--> xs, where x is not in nts, or 2. There is a nonterminal occuring in xs that does not occur in nts, or 3. There is a terminal occuring in xs that does not occur in ts, or 4. nts and ts have some symbol in common. Write a function, goodgrammar :: Grammar -> Bool, that returns True if its argument is not bad, and false if it is bad. -} data Grammar = Grammar [String] -- non-terminals [String] -- terminals [Production] -- productions arithgrammar = Grammar nonterminals terminals productions where nonterminals = ["Exp"] terminals = ["Int","+","*"] productions = ["Exp" :--> [Terminal "Int"], "Exp" :--> [Nonterminal "Exp", Terminal "+", Nonterminal "Exp"], "Exp" :--> [Nonterminal "Exp", Terminal "*", Nonterminal "Exp"]] {- Question 4. We saw the following grammar in the LanguageProcessing1 slides: Exp ::= ( Op ExpList ) Exp ::= Int | Real Op ::= + | - | * | / ExpList ::= Exp ExpList ExpList ::= In this last production, I have left the lambda out. Represent this grammar as a Grammar value. -} {- Question 5. The following is a derivation in the grammar from Question 4 (it also comes from the same slide set). Exp => ( Op ExpList ) => ( + ExpList ) => ( + Exp ExpList ) => ( + 1 ExpList ) => ( + 1 Exp ExpList ) => ( + 1 2 ExpList ) => ( + 1 2 ) A derivation can be represented by a list of lists, [s1,...,sn], where each si :: [Symbol]. That is, a derivation can be represented by a value of type [[Symbol]]. Represent the above derivation as a value of type [[Symbol]]. -}