CS 331 Spring 2025  >  Assignment 1 (Formal Languages)


CS 331 Spring 2025
Assignment 1 (Formal Languages)

Assignment 1 is due at 5 pm on Thursday, January 30. It is worth 80 points.

Procedures

This assignment is to be done individually.

Turn in answers to the exercises below on the UA Canvas site, under Assignment 1 for this class.

Exercises (A–J, 80 pts total)

  1. [5 pts] Get the file check_lua.lua from the class Git repository. This is a Lua source file for a complete program. When it is executed, this program prints “Secret Message #1:”. Below that, it prints—you guessed it—a secret message. Run the program. What is the secret message?

    There are several ways to execute a Lua program.

    • You can open the file check_lua.lua in ZeroBrane Studio and click on the green double right arrow.
    • You can get a Lua interactive prompt, and, assuming the file is in the proper directory, type “dofile("check_lua.lua")”.
    • If you have Lua installed as a command-line application, then, in the proper directory, you can type at the command line “lua check_lua.lua

    The point of this exercise is to make sure you can execute Lua code—not merely that you know what the secret message is. More than half of the remaining assignments will involve writing Lua code, so being able to execute Lua code is required, if you are to succeed in this class. Now is the time to make sure you can.

  2. [5 pts] Take the Programming Languages Survey [Google Forms]. You must be signed into your UA Google account to take this survey.
  3. [6 pts]
    1. Is the type checking in the C++ programming language primarily static or dynamic?
    2. What does this mean?
  4. [5 pts] Consider the following grammar, whose start symbol is \(S\).

    \[ \begin{align*} S &\rightarrow ABn \\ A &\rightarrow Ax \mid x \\ B &\rightarrow oB \mid \varepsilon \end{align*} \]

    Which of the following strings are in the language generated by this grammar?

    1. \(xon\)
    2. \(nxo\)
    3. \(ooon\)
    4. \(xxxn\)
    5. \(xxoon\)
    6. \(ooxxn\)
    7. \(xoxon\)

  5. [5 pts] Give an English description of the language generated by the following grammar, whose start symbol is \(S\). Your description needs to be precise enough that, based only on your description, I can determine which strings lie in the language and which do not.

    \[ \begin{align*} S &\rightarrow a S b \mid X \\ X &\rightarrow cc X \mid \varepsilon \end{align*} \]

  6. [5 pts] Consider the following regular expression.

    \[ x{*}(a|bc){*} \]

    Which of the following strings are matched by this regular expression?

    1. \(xccc\)
    2. \(xxbbcc\)
    3. \(xxabc\)
    4. \(abc\)
    5. \(bcbc\)
    6. \(cbcb\)
    7. \(bca\)

  7. [5 pts] Write a regular expression that matches words that contain at least one \(a\), and no letters other than lower-case \(a\), \(b\), and \(c\). (Here, a word is any sequence of letters.)
  8. [16 pts] This problem deals with the following grammar, which has start symbol \(S\).

    \[ \begin{align*} S &\rightarrow SS \mid X \\ X &\rightarrow ab \end{align*} \]

    1. Give a leftmost derivation for the string \(abab\).
    2. Give a rightmost derivation for the string \(abab\).
    3. Verify that the grammar is ambiguous by finding a string with two different parse trees. Your answer must consist of the string and the two parse trees.
    4. Write a non-ambiguous grammar that generates the same language as the above grammar.
  9. [20 pts] Consider the language containing those strings consisting of two or more \(x\) characters followed by a single \(a\) character.

    \[ \{xxa, xxxa, xxxxa, xxxxxa, \dots \} \]

    1. Write a regular expression that generates this language.
    2. Draw the diagram of a DFA that recognizes this language.
    3. Write a grammar that generates this language.
    4. Is your grammar from the previous step ambiguous? Justify your answer.
  10. [8 pts] In class we described the syntax of regular expressions using informal methods. In this exercise, you will describe the syntax using a formal specification method.

    Specifically, write a BNF (not EBNF!) grammar for regular expressions, as we originally defined them in class—without the various shortcuts. Your start symbol is <reg-exp>. You may assume that symbols <single-char> and <epsilon> have already been defined with appropriate productions. The former expands to any string containing exactly one character, which must be in the alphabet, and the latter expands to the string consisting of a single lower-case epsilon: “\(\varepsilon\)”.

    When I say “without the various shortcuts” I mean that you must not include “.”, “+”, “?”, or the ways of using brackets, like “[^a-zA-Z]”. Your grammar needs to generate a language containing strings like the following.

    (a|x)*c(bb|y)
    

    But do not handle any syntax beyond what is used above.