Sie sind auf Seite 1von 3

CS314 Fall 2014

Assignment 1
Due Wednesday, September 17, 11:59pm
Submission: pdf file through sakai.rutgers.edu
1

Problem Simple Rewrite Systems

Remember our game in the second lecture.


1. Given the same input, i.e., a sequence of characters starting with $ and ending with
#, and any combination of 0s and 1s in-between, specify a set of rewrite rules that
determine whether the string contains the same number of 0s and 1s.
Here is some sample output:
$0011# should be rewritten as $#
$1001# should be rewritten as $#
$110110# should be rewritten as $11#
$0001100# should be rewritten as $000#
In other words, the $# indicates that the input string has the same number of 0s and
1s. If the string does not contain the same number of 0s and 1s, the resulting string
shows how many more 0s or 1s there are in the input string.
2. Is there at most only a single rewrite rule that can be applied at any point in time
during the rewrite process? Explain.
3. Show the steps of your rewrite system for the input strings $0110# and $00010#.

Problem Rewrite Systems for Logic Expressions

An interpreter for a language L maps programs written in L to their answers. Remember


that a language is a set of words. Lets define our language Llogic inductively as follows:
1. The words #t, #f, and #? are in Llogic .
2. Assume that both A and B stand for words in the language Llogic . Then
(a) (AandB) are also in Llogic .
(b) (AorB) are also in Llogic .
We call the words in the language Llogic logic expressions. Examples of logic expressions
are: ((#tor#f)and#?) and (#tor(#?and#?)). However,
#tor#f
1

is not in the language (parenthesis are missing).


Give a rewrite system that evaluates or computes the value of logic expressions
in Llogic . The operators and and or correspond to the standard truth value functions,
augmented by the unknown value.
truth value
false
false
true
true
false
unknown
true
unknown
unknown

truth value
false
true
false
true
unknown
false
unknown
true
unknown

logical-and logical-or
false
false
false
true
false
true
true
true
false
unknown
false
unknown
unknown
true
unknown
true
unknown unknown

Our syntax to semantics map is as follows:


#t corresponds to true
#f corresponds to false
#? corresponds to unknown
and corresponds to logical-and
or corresponds to logical-or
1. Define a rewrite system for logical expressions in Llogic that produces the final value
of the expression. A final value is represented by either #t, #f or #?. Your rewrite
system is basically an interpeter for Llogic .
For example, our two logic expressions ((#tor#f)and#?) and (#tor(#?and#?))
should be rewritten as #? and #t, respectively. You can assume that your rewrite
system will only be presented with correct Llogic logic expressions, so dont worry about
error messages.
2. Show your rewrite system steps that are performed for our two logic expressions given
above. For each step clearly show the left-hand side of the rule in the current logical
expression that you are rewriting.
3. (OPTIONAL - NOT GRADED) How would you prove the correctness of your rewrite
system? Hint: use induction.

Problem Regular Expressions

Write a regular expression for floating point numbers that you want to use in your new
programming language. You should allow numbers of the form 0.34, 221.5E20, and 1.0E-5,
2

but not .12, 33., 21.5E, or E30. Since this is not a total specification of all possible number
patterns, make reasonable assumptions for filling in the holes. Remember: you are a
language designer here.

Problem Regular Expressions

Describe the formal languages denoted by the following regular expressions using the English
language (e.g.: All strings over the alphabet . . . that have the property . . .):
1. (( | 0) 1*)*
2. (0|1)*0(0|1)(0|1)0

Problem Regular Expressions

Write a regular expression for the following languages.


1. All strings of as, bs, and cs that contain no as immediately following any bs.
2. All strings of as, bs, and cs that do not contain more than 2 bs and 2 as.

Problem Finite State Automaton (FSA)


1. Specify a DFA using a transition diagram and a formal FSA specification <S, s, F, T>
(see lecture 2) that recognizes the following language: All strings of 0s and 1s that,
when interpreted as a binary number, are divisible by 8. In other words, value(binary
number) modulo 8 = 0.
2. Specify a DFA using a transition diagram and a formal FSA specification <S, s, F, T>
that recognizes the following language: All strings of 0s and 1s that, when interpreted
as a binary number, are divisible by 5. In other words, value(binary number) modulo
5 = 0.