0% found this document useful (0 votes)
82 views16 pages

Cse 450 2018 Sample Exam

The document contains a sample exam for a computer science course. It includes 6 questions related to regular expressions, context-free grammaries, arrays, memory, and finite automata. Students are instructed to answer 5 of the 6 questions, marking one as not graded by slashing through it. They should show their student ID when turning in the exam.

Uploaded by

Mehari Temesgen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views16 pages

Cse 450 2018 Sample Exam

The document contains a sample exam for a computer science course. It includes 6 questions related to regular expressions, context-free grammaries, arrays, memory, and finite automata. Students are instructed to answer 5 of the 6 questions, marking one as not graded by slashing through it. They should show their student ID when turning in the exam.

Uploaded by

Mehari Temesgen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

First Name:

Last Name:

PID (on your ID card):

MSU Net ID:

SAMPLE Exam for CSE 450 (2018)

Answer the questions in the spaces provided on the page. If you


run out of room for an answer, continue on the back of the page.

• DO NOT OPEN THE EXAM UNTIL TOLD TO DO SO

• You should answer only 5 of the 6 questions.

• 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.

• Have your ID ready when you turn in your exam.

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

command arg1 arg2 arg3


loop start 1:
10 s2
AR SET SIZE a1 s2
AR SET SIZE
AR SET IDX a3 0
AR SET IDX a3 1
AR SET IDX a3 2
a3 a1
s4
s5
VAL COPY 20 s6
DIV s5 s6 s7
AR GET IDX a1 s7 s8
AR SET IDX a1 s7
VAL COPY s4 s8
JUMP loop end 2
VAL COPY 2 s9

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:

<5, ’c’> Nice and simple pair literal!


<6, <’d’, 0>> Pairs are allowed in pairs!
<’y’, <<’o’, <’8’, 8>>, 9>> You can nest as deep as you like!

Examples of ILLEGAL pair literals are:

<> Pairs can’t be empty!


<7> Pairs must have a left and right value!
<’c’, ’y’> Left and right can’t have the same type!
<<1, ’K’>, <’G’, 7>> Can’t match types (both sides are pairs)!

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!

Examples if ILLEGAL array are:

[[ ]] Must have at least one entry!


[[ 1, 2, ’3’, 4]] Cannot mix types!
[[ 1, 2, , 4]] Cannot have an extra comma!
[[ 1, 2, 4,]] Cannot have a trailing (or leading) comma!
[[ 1 + 2, 3]] Cannot have expressions!

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.

TEST_* [VAL:num1] [VAL:num2] [SCL:result]


Options are: TEST_LESS, TEST_GTR, TEST_EQU, TEST_NEQU, TEST_LTE, or TEST_GTE
Compare num1 and num2. Set result argument to 0 or 1 based on if condition is false or true.

JUMP [VAL:line] Move the instruction pointer to line.


JUMP_IF_0 [VAL:test] [VAL:line] Move the instruction pointer to line if test is equal to zero.
JUMP_IF_N0 [VAL:test] [VAL:line] Move the instruction pointer to line if test is NOT equal to zero.

RANDOM [SCL:result] Generate random number 0 to 100 and store in result.


IN_CHAR [SCL:result] Retrieve a character from STDIN and store in result.

AR_GET_IDX [ARR:array] [VAL:index] [SCL:result]


Look up index in array and store its value as result.
AR_SET_IDX [ARR:array] [VAL:index] [VAL:value]
Loop up index in array and set its value to value.
AR_GET_SIZE [ARR:array] [SCL:result]
Look up the size of array and store it in result.
AR_SET_SIZE [ARR:array] [VAL: size]
Create an array with new_size space for elements
AR_COPY [ARR:array1] [ARR:array2]
Copy the contents and size of array1 into array2.

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.

You might also like