CS152 EXAM 2

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/62

encourage image

There's no tags or description

Looks like no tags are added yet.

63 Terms

1
New cards

What does the regex [a-z]+ read as?

If input is one or more lower case characters

2
New cards

Executable machine code is

Machine Specific

3
New cards

“Compile once, run everywhere” can be achieved when the target language is

Bytecode

4
New cards

What are the tokens from the following: Falseif ~> scanner ~>

Identifier Falseif

5
New cards

What are the tokens from the following: ** ~> scanner ~>

Exp

6
New cards

What are the tokens from the following: * * ~> scanner ~>

Times, Times

7
New cards

What expression do we need for lower case identifiers?

first = 5

second = 6

total = first + second

[a-z]+

8
New cards

What expression do we need for the ‘+’ identifier?

first = 5

second = 6

total = first + second

\+

9
New cards

We are implementing a scanner for a language where the string "or" denotes a logical operator. To match  the string "or" corresponding to the OR token, which of the following regular expressions do we need?

or

10
New cards
11
New cards

For the regex 10*1, which strings of the following match?

11, 101, 1001, 10, 100, 1000001

11, 101, 1001, 1000001

12
New cards

Write a regex that matches both grey and gray.

gr[ae]y OR grey | gray

13
New cards

Write a regex for exactly 3 digits.

[0-9]{3}

14
New cards

Write a regex for exactly 5 lowercase letters.

[a-z]{5}

15
New cards
<p>What does the expression 5 + 8.3 × 12 evaluate to according to the parse tree?</p>

What does the expression 5 + 8.3 × 12 evaluate to according to the parse tree?

104.6

16
New cards
<p>What does the expression 5 + 8.3 × 12 evaluate to according to the parse tree?</p>

What does the expression 5 + 8.3 × 12 evaluate to according to the parse tree?

159.6

17
New cards

Is the following grammar ambiguous?

<expr> → <expr> PLUS <expr> | <term>
<term> → <term> TIMES <term> | <number>

<number> → INT | FLOAT

Yes, it is cascading, but doesn’t specify associativity

18
New cards

Is the following grammar ambiguous?

<expr> → <expr> PLUS <term> | <expr> MINUS <term> | <term>
<term> → <term> TIMES <term> | <number>

<number> → INT | FLOAT

Yes, there’s no associativity for multiplication

19
New cards

Based on the following grammar, what does 12 - 5 + 3 evaluate to?

<expr> → <expr> PLUS <term> | <expr> MINUS <term> | <term>
<term> → <term> TIMES <number> | <number>

<number> → INT | FLOAT

10; Evaluate the leftmost expression first (12-5), then evaluate the resulting expression ((12-5)+3)

20
New cards

Consider the following grammer and identify the start symbol, all nonterminals, all terminals, and the # of possible requests.

<request> → <action><target>
<action> → SEND | SAVE
<target> → FILE | MESSAGE

start symbol: <request>

requests: 2 × 2 = 4

nonterminals: <action>, <target>, <request>

terminals: SEND, SAVE, FILE, MESSAGE

21
New cards

Create a BNF grammar that generates one or more mneu items separated by commas. For example, this is allowed:

PIZZA PIZZA COMMA SALAD PIZZA COMMA SALAD COMMA SOUP

But this should not be allowed:

COMMA PIZZA

PIZZA COMMA

<list> → <list> COMMA <item> | <item>
<item> → PIZZA

22
New cards

Rewrite the following grammar so that SHIFT has higher precedence than MERGE using the separate nonterminal <term>.


<expr> → <expr> MERGE <expr> | <expr> SHIFT <expr> | ATOM

<expr> → <expr> MERGE <term> | <term>

<term> → <term> SHIFT <atom> | <atom>

<atom → ATOM


** if a term has higher precedence, it goes deeper into the recursion

23
New cards

Design a BNF grammar for a simple search language with AND, OR, and parenthesis such that the following combinations can be made:

TERM AND TERM

TERM OR TERM

TERM OR TERM AND TERM

LPAREN TERM OR TERM RPAREN AND TERM

<expr> → <expr> OR <term> | <term>

<term> → <term> AND <factor> | <factor>

<factor> → TERM | LPAREN <expr> RPAREN

24
New cards

What is the correct EBNF for the following term?

<expr> → <expr> ADD_OP <term> | <term>

<expr> → <term> {ADD_OP <term>}

25
New cards

What is the correct EBNF for the following term?

<term> → <term> TIMES <number> | <number>

<term> → <number> {TIMES <number>}

26
New cards
<p>Which diagram is valid in the attached image?</p>

Which diagram is valid in the attached image?

B

27
New cards

Convert the following BNF into EBNF:

<playlist> → <playlist> NEXT <song> | <song>

<playlist> → <song> {NEXT <song>}

28
New cards

Convert the following BNF into EBNF:

<command> → RUN <program> | RUN <program> WITH <options>

<command> → RUN <program> [WITH <options>]

29
New cards

Convert the following BNF to EBNF:

<expr> → <value> | <expr> JOIN <value>
<value> → ITEM

<expr> → <value> {JOIN <value>}
<value> → ITEM

30
New cards

Does the following satisfy the condition that parsers must be able to distinguish between choices in a rule?

<factor> → LPAREN <expr> RPAREN | POSNUM

Yes, LPAREN and POSNUM are distinct

31
New cards

Does the following satisfy the condition that parsers cannot have a token beginning the optional part also come after the optional part?


<special> → NUMBER [<special>] NUMBER | NUMBER

No,

32
New cards

In the following, which tokens belong to First(<expr>)?

<expr> -> OPENPAREN OPERATOR <operands> CLOSEPAREN | POSNUM

First(<expr>) = {OPENPAREN, POSNUM}

33
New cards

In the following, which tokens belong to Follow(<operands>)?

<expr> -> OPENPAREN OPERATOR <operands> CLOSEPAREN | POSNUM

Follow(<operands>) = {CLOSEPAREN}

34
New cards

For the following, find FIRST(<expr>).

<expr> → <application> | POSNUM
<application> → OPENPAREN OPERATOR <operands> CLOSEPAREN
<operands> → <expr> [<operands>]

FIRST(<expr>) = {OPENPAREN, POSNUM}

35
New cards

For the following, find FIRST(<operands>).

<expr> → <application> | POSNUM
<application> → OPENPAREN OPERATOR <operands> CLOSEPAREN
<operands> → <expr> [<operands>]

FIRST(<operands>) = FIRST(<expr>) = {FIRST(<application>), POSNUM} = {OPENPAREN, POSNUM}

36
New cards

For the following, find FOLLOW(<operands>).

<expr> → <application> | POSNUM
<application> → OPENPAREN OPERATOR <operands> CLOSEPAREN
<operands> → <expr> [<operands>]

FOLLOW(<operands>) = {CLOSEPAREN}

37
New cards

The list [1,2,3] can be written as which of the following?

1:2:3:[]

38
New cards

What list does [5,8…20] evaluate to?

[5, 8, 11, 14, 17, 20]

39
New cards

What is z in the following pattern matched list?

xs = [1,2,3,4]
x:y:z = xs

[3,4]

40
New cards

For the following swap function, what does swap Hello evaluate to?

swap :: [a] → [a]
swap [] = []

swap [x] = [x]

swap(first:second:rest) = second:first:rest

eHllo

41
New cards

Given the following function, what is the result of testLazy (length [1…]) 90?

testLazy :: a → a → a

testLazy _ x = x

90

42
New cards

Given the following function, what is the result of testLazy (infinite 1) 5?

testLazy :: a → a → a

testLazy _ x = x

infinite x = infinite (x+1)

5

43
New cards

What gets printed after the following runs?

def test(first, second):

return second


def infinite(x):

return infinite(x+1)


result = test(infinite(1), 5)

print (result)

An error occurs

44
New cards

There are 3 implementations of a function: using pattern matching, guards, and an if/then/else clause. Which is most idiomatic?

Pattern matching

45
New cards

Consider the following Haskell function:

mystery []  = Nothing
mystery [x] = Nothing
mystery (_:x:_) = Just x

What does the following expression evaluate to?

mystery [9..19]

Just 10

46
New cards

Consider the following Haskell function definition:

mystery x y = if x then [y] else []

What is the inferred type declaration of the function mystery?

mystery :: Bool -> a -> [a]

47
New cards

Determine the type of the following: [1,2,3]

Num a => [a]

48
New cards

Determine the type of the following: \x → x * x

Num a => a → a

49
New cards

What is the value of map (*3) [1, 3…6]?

[3, 9, 15]

50
New cards

What is the value of map (>10) [5, 20, 4, 5, 23]?

[False, True, False, False, True]

51
New cards

What is the value of filter (> 5) [6, 1, 2, 3, 9]

[6, 9]

52
New cards

What is the length of the list resulting from zipWith (\x y → x*x + y*y) [1, 2] [4..200]

2

53
New cards

What is the resulting list from zipWith (\x y → x*x + y*y) [1, 2] [4..200]

[17, 29]

54
New cards

What is foldl (-) 10 [1,2,3]?

4

55
New cards

What is foldr (-) 10 [1,2,3]?

-8

56
New cards

What is foldl1 (-) [4, 3, 2, 1]?

-2

57
New cards

Given the following function, what is the result of ‘1 + (‘S’ “Go Spartans!”)’?

index :: Eq a => a -> [a] -> Maybe Int

index _ [] = Nothing

index y (x:xs)

| y == x = Just 0

| otherwise = let indexRest = index y xs

in case indexRest of

Nothing -> Nothing

Just i -> Just (1 + i)

An error

58
New cards

Evaluate the following: foldr (-) 10 [1, 2, 3]

(1 - (2 - (3 - 10))) = -8

59
New cards

What is the general type of addOne x = x + 1?

Num a => a → a

60
New cards

What is the general type of applyIncrement f x = f (x + 1)?

Num a => (a → b) → a → b

61
New cards

What is the general type of same (x, y) = x == y?

Eq a => (a, a) → Bool

62
New cards

What is the general type of twice f x = f (f x)?

(a → a) → a → a

63
New cards

What is the general type of pairWithList x = (x, [x])?

a → (a, [a])