CSCI 384 FALL 2021HOMEWORK 1: GrammarsWe’ve spent class time describing program language syntax using grammars. The idea behind grammars is that we use production rules to give the layout of common constructs in the language. For example, we might use the rules stmt ::= loop | cndl to describe two kinds of program statments possible in C++. These would be among the many rules describing the full language. The rules describe how their code should be structured. For example, there is a expn ::= expn or else the negation of some other expression expn ::= and so forth. We also defined grammars more formally, in particular defining a derives relation for “computing” strings generated by a grammar. Our definition said that if A ::= w was a production, and we’ve derived l A r then we can say that l A r => l w r And so for example the derivation B is possible using the rules of the grammar for balanced brackets shown below: B ::= B B This means that The derivation just given is a leftmost derivation, because we are always choosing to replace the leftmost B in each derivation step. We noted that that grammar was ambiguous because there were two leftmost derivations for some string of tokens generated by the grammar. [HW1 EX1] brackets • Part A: Using the balanced bracket grammar, give a different leftmost derivation of the token string Now consider the following grammar: B ::= • Part B: Give a leftmost derivation of The grammar is not ambiguous, it turns out. • Part C: Argue that it generates the same language as the ambiguous one. [HW1 EX2] if then else Here is a grammar that models the syntax of programming languages that have “if-then” and “if-then-else” statements. X ::= This grammar is also ambiguous. • Part A: Why? Give a token string that has two leftmost derivations using this grammar. • Part B: Devise a grammar that generates the same set of token strings but is not ambiguous. [HW1 EX3] facts In class I worked out a grammar for a subset of the Python programming language. It specified that a program consisted of a series of function definitions (using the For this exercise, I’d like you to examine a similar grammar that defines a subset of the language OCaml. The subset I define is analogous to what we did with Python. I call it OCaml-lite. An OCaml-lite program consists of a series of procedure and function definitions, each ending with
It defines a function There are a few quirks. In particular there are two kinds of variables that can be managed within the statements of procedures. There are “immutable” variables that are given a value, but cannot be reassigned within the code. These are procedue parameters or else introduced with a The other variables are “mutable” (or “reference”) variables and introduced with a These variables can be reassigned with the statement name To access their values, you must prefix them with an exclamation mark, just as in the statement
in the example above. You cannot use mutable variables within function definitions, only mutable ones. The full summary of the syntax is given by this grammar • TO DO: Modify the code so that it does not define a function It should behave exactly the same as the program I wrote above, just use this different strategy for computing successive factorials, only using a modified Here is its behavior with one run of the program:
[HW1 EX4 BONUS] guess six Here is a link to some source code for a guessing game, written in OCaml-lite. • TO DO: Modify the guessing game code, changing it so that a player can make no more than 6 guesses. If they fail to guess after 6 tries it should let them know that they failed, and then reveal to them the number they failed to guess. NOTE: The guessing game code relies on random number generation, and this is not strictly part of our grammar’s syntax. Hopefully its use makes sense, anyway, and you write code that uses it. Testing OCaml-lite code on your computerThe two prior exercises have you write code in a novel language, one that is a sub-dialect of the OCaml language. This language runs on many computer platforms, and you might want to try to get it working on your own machine. If so, you can follow the instructions on the OCaml language’s web pages for installing the system, particularly these install pages. Once you feel like you have installed everything correctly, you’ll want to start working through their tutorial to see if it’s installed correctly. Just try a few simple interactions with the system, entering expressions as they suggest. On my Mac I just typed the following in Terminal
and the language system came up and gave me a prompt, just like in their tutorial. You don’t need to read their Chapter 1 exhaustively. Just try a few things out and report back to me on Friday how that went. If it is installed correctly, then you can use it to test your work for Exercises 3 and 4. Testing OCaml-lite code on-lineThere is also a web page that provides an editor and interpreter for OCaml. It can be found at One of its quirks is that the call to
instead of all that interactive code between The most you can do in this demo, for code that uses Testing OCaml-lite code on
|