CS 331 Spring 2026 > Assignment 1 (Formal Languages)
CS 331 Spring 2026
Assignment 1 (Formal Languages)
Assignment 1 is due at 5 pm on Thursday, January 29. It is worth 85 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.
- Your submission must be a PDF with your name near the top of the first page.
- I may not look at your homework submission immediately. If you have questions, e-mail me.
Exercises (A–J, 85 pts total)
- [5 pts]
Get the file
check_lua.luafrom 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.luain 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.
- You can open the file
- [5 pts] Take the Programming Languages Survey [Google Forms]. You must be signed into your UA Google account to take this survey. Be sure to record your UA e-mail account to be included with your response (check the box), so I know you took the survey.
- [7 pts]
- Is the type checking in the Swift programming language primarily static or dynamic?
- What does this mean? In this part, you are not telling me about Swift. You are telling me what it means for type checking to be static, or for type checking to be dynamic.
- [5 pts]
Consider the following grammar, whose start symbol is \(S.\)
\[ \begin{align*} S &\rightarrow rAB \\ A &\rightarrow Ao \mid \varepsilon \\ B &\rightarrow zB \mid z \end{align*} \]
Which of the following strings are in the language generated by this grammar?
- \(roz\)
- \(ozr\)
- \(rzzz\)
- \(rooo\)
- \(roozz\)
- \(rzzoo\)
- \(rozoz\)
- [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 aa S bb \mid X \\ X &\rightarrow c X \mid \varepsilon \end{align*} \]
- [5 pts]
Consider the following regular expression.
\[ x{*}(ab|c){*} \]
Which of the following strings are matched by this regular expression?
- \(xccc\)
- \(xxbbcc\)
- \(xxabc\)
- \(abc\)
- \(abab\)
- \(baba\)
- \(cab\)
- [5 pts] Write a regular expression that matches words that contain at least one \(b,\) and no letters other than lower-case \(a,\) \(b,\) and \(c.\) (Here, a word is any sequence of letters.)
- [20 pts]
This problem deals with the following grammar,
which has start symbol \(S.\)
\[ \begin{align*} S &\rightarrow SaS \mid X \\ X &\rightarrow b \end{align*} \]
- Is this grammar a regular grammar? Justify your answer.
- Is this grammar a context-free grammar? Justify your answer.
- Based on this grammar, give a leftmost derivation for the string \(bab.\)
- Based on this grammar, give a rightmost derivation for the string \(bab.\)
- Verify that this grammar is ambiguous by finding a string with two different parse trees. For your answer, give the string and the two parse trees.
- Write a non-ambiguous grammar that generates the same language as the above grammar.
- [20 pts]
Consider the language containing
those strings consisting of
a single \(x\) character.
followed by
two or more \(a\) characters.
\[ \{xaa, xaaa, xaaaa, xaaaaa, \dots \} \]
- Write a regular expression that generates this language.
- Draw the diagram of a DFA that recognizes this language.
- Write a grammar that generates this language.
- Is your grammar from the previous step ambiguous? Justify your answer.
- [8 pts]
Write a BNF (not EBNF!) grammar for the name of a person,
as it is usually encountered in the US—something
like “
Berferd Snerd” or “Berferd Q. Snerd”. More precisely, such a name consists of either a first name followed by a last name OR first name then middle initial then last name. A first name and a last name begin with an upper-case letter and have one or more lower-case letters after that. A middle initial is an upper-case letter followed by a period. The various parts of a name are separated by blanks; two blanks in a row is not allowed.Your start symbol must be
<name>. You may assume that symbols<uc-letter>and<lc-letter>have already been defined with appropriate productions. The former expands to any string consisting of exactly one upper-case letter, while the latter expands to any string consisting of exactly one lower-case letter.