Cse 450 2018 Sample Exam
Cse 450 2018 Sample Exam
Last Name:
• On one of the questions make a large slash through it, which indicates that it should
not be graded.
• If you start to answer a question and then change your mind, please cross out the
attempt and write DO NOT GRADE across it.
https://xkcd.com/303/
Question 1: Regular Expression Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
For each of the following regular expressions, indicate which of the strings can be gen-
erated?
(a) (20 points) 1*0+1?0
0
00
1010
000010
10
01110
1100
1
(b) (20 points) (ab*)+c?
ac
ababab
aaabbb
c
abcd
abbaabbcc
bbc
ab+c
Question 2: Construct Regular Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
(a) (20 points) Design a regular expression that will identify all literal integers where
the digits are in numerical order. Allow the empty string.
For example, 014457889 is allowed but 8953 is not.
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
(b) (20 points) Design a regular expression that will identify an integer value between
0 and 255. Your expression should identify all of these values with no extra leading
zeros and no additional characters.
Good examples:
• 200
• 36
• 2
• 0
• 255
Bad examples:
• -1
• 3.14
• 450
• 007
• 256
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
Question 3: Arrays and Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
Note: Both parts (a) and (b) read and write to the same memory at the bottom of the
page.
(a) (20 points) For the memory representation below, write in the row ”New Value”
any values that would change from executing the LMAOcode instruction. Memory
location 0 stores the start of the free memory.
AR_COPY a3 a5
AR_SET_SIZE a5 s1
AR_SET_IDX a5 s4 7
(b) (15 points) For the same memory representation below, write in the row ”New
Value” any values that would change from executing the series of ROFLcode
instructions.
LOAD 1 regD
LOAD regD regC
MEM_COPY regC regD
VAL_COPY 8 regA
SUB regA regC regB
(c) (5 points) Final value for registers after the above ROFLcode instructions:
regA:
regB:
regC:
regD:
Location 0 1 2 3 4 5 6 7 8
Old Value 116 2 114 113 1 107 ’c’ -9.5 3
New Value
Location 112 113 114 115 116 117 118 119 120 121
Old Value 7 1 3 0 0 0 0 0 0 0
New Value
Question 4: Construct Context Free Grammars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 points
Design a context free grammar for the following problems. Denote non-terminals with
single quotes.
Example CFG rule: S -> ’a’ B ’c’ The S nonterminal consists of a terminal a, fol-
lowed by a non-terminal B, followed by a terminal c.
(a) (10 points) All possible sets of matched parentheses.
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
(b) (1 point) All strings connsisting of a series of a’s followed by a series of b’s, where
there are always more a’s than b’s.
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
(c) (2 points) All possible palindromes (strings that read the same forward or back-
ward) over the alphabet {a, b, c}.
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
Question 5: Compile Good To Bad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
Given the source code, fill in the blanks in the Bad Code generated by a compiler.
HAI 1.450
IM IN YR LOOP
I HAS A array ITZ A YARN AN THAR IZ 10
array R "I’m"
IN array’Z QUOSHUNT OF WHATEVR 20 PUT GIMMEH
GTFO
VISIBLE array’Z 2
NOW IM OUTTA YR LOOP
KTHXBYE
OUT CHAR
JUMP
Question 6: Deterministic Finite Automata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
(a) (20 points) For the following Deterministic Finite Automata (DFA), write a Regu-
lar Expression that accepts the same language.
2 4
b a
c c
1 3 5
b b
(b) (5 points) Convert the above DFA to a table. Use a slash to denote a halt on
failure.
State a b c
1
2
3
4
5
(c) (15 points) Which strings does the DFA accept?
bb
babbcc
bbbbcc
babbabbabbcc
babbabbabbbb
babbabbb
babbbcc
babcc
Question 7: Pair Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
You want to implement a new pair literal in LOLcode that generates an pair of values
(called left and right) contained within less-than and greater-than operators and sepa-
rated by a comma. The types of left and right can be NUMBR literals, LETTR literals,
or other pair literals. However, the type of left and right must NOT match. Examples
of some legal pair literals are:
Write a set of Context-Free Grammar rules that capture the grammar for pair literal.
The only terminal symbols you may use are: LETTR LITERAL, NUMBR LITERAL,LESS THAN,
GREATER THAN and COMMA.
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
Question 8: Context Free Grammar Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
For each of the following Context Free Grammars (CFGs), indicate which of the strings
can be generated? Note: terminals are lowercase letters and non-terminals are uppercase
letters.
(a) (20 points)
S : A B
A : b | c | t
B : C A | B B
C : a | e | i | o | u
CAB
cat
toot
cucuc
tubui
betot
bewit
cattab
tettob
tebicot
(b) (20 points)
S : A S | B
A : h | k
B : A | w | C C
C : p | d
w
k
pd
khw
kkdd
kkhpd
hhkkhw
Question 9: Construct Regular Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
(a) (20 points) Design a regular expression that will match all positive even integers
(no leading zeros) and nothing more.
For example, 15048 and 9860 are matched but 04 and 123407 are not.
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
(b) (20 points) Design a regular expression that will identify strings with at least one
consecutive repeated letter. For simplicity, the language is restricted to the letters
a, b, and c.
Matching examples:
• aa
• abcbabba
• aabbccacbabb
• abb
Non-matching examples:
• a
• ababa
• abcbcbabca
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
Question 10: LMAOcode to ROFLcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
For each LMAOcode instruction below, convert to ROFLcode. For this page only, you
can not use the MEM COPY command.
(a) (10 points) VAL COPY s10 s40
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
(b) (10 points) RANDOM s9
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
(c) (10 points) AR GET SIZE a8 s99
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
(d) (10 points) AR SET IDX a19 2 ’\n’
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
..................................................................................
Question 11: New Context Free Grammar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
You want to implement a new array literal in LMAOcode that generates an array based
on a comma-separated series of one or more LETTR literals or one or more NUMBR
literals contained within ”[[” and ”]]”. Examples of some legal arrays are:
[[ 1, 2, 3, 4, 5 ]]
[[1,2,3,4,5,100]] No spaces are required!
[[ ’G’, ’o’, ’o’, ’d’, ’!’ ]] Character arrays are okay!
[ [ 42 ] ] Minimum one entry; spaces okay!
Write a set of CFG syntactic rules that capture the grammar for array literal. These
are the only tokens you may use:
LETTR LITERAL, NUMBR LITERAL,OPEN BRACKET, CLOSE BRACKET and COMMA.
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
......................................................................................
Question 12: Parse Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
Given the context free grammar:
S : A ’b’
A : ’a’ A ’a’
A : ’b’
Draw the parse tree (using the CFG above) associated for the string: aaabaaab
Question 13: NFA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
Create a Non-deterministic Finite State Machine for the following regular expression:
((ab)+(0*1))+
Question 14: DFA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 points
(a) Convert the above DFA to a table. Use a slash to denote a halt on failure.
State 0 1
A
B
C
D
E
(b) Which strings does the DFA accept?
0
000
1010
00010
10000000
010101000100
000100
0001000
LMAOcode Overview
Arguments
VAL: This argument uses a value; it can be a literal integer, a literal char, a label, or a scalar variable.
SCL: This argument must be a scalar variable (which will be written to)
ARR: This argument must be an array variable
Instructions
ADD [VAL:num1] [VAL:num2] [SCL:result] SUB [VAL:num1] [VAL:num2] [SCL:result]
MULT [VAL:num1] [VAL:num2] [SCL:result] DIV [VAL:num1] [VAL:num2] [SCL:result]
Apply the given math operation on num1 and num2, and place the answer in result.
VAL_COPY [VAL:from] [SCL:to] Copy the value from into the scalar variable to.
OUT_NUM [VAL:num] Output the number represented by the argument.
OUT_CHAR [VAL:char] Output the character represented by the argument.
Labels
A label is a string of alphanumeric characters, beginning with a letter that is used to reference a line number
elsewhere in the code. When a label is created, it must be placed at the beginning of a line of code and it must end
with a colon (‘:’). A label will typically be used to indicate the end point in a jump command.
ROFLcode Overview
ROFLcode (assembly) is very similar to LMAOcode (intermediate code), with a handful of changes
• The AR_* instructions are not available.
• Scalar variables are not available, but eight registers (regA through regH) take their place.
• Three new instructions are available that allow you to interact with memory. They are:
LOAD [VAL:from] [REG:to] Load the value in memory position from into register to.
STORE [REG:from] [VAL:to] Store the value in register from into memory position to.
MEM_COPY [VAL:from] [VAL:to] Copy the value in memory position from into memory position to.