CMSC330_10/27 - Parsing

CMSC 330 — Fall 2026 · October 27, 2026 · 194 players · Max 13 pts

Questions & Results

Q1 of 13Multiple ChoiceAccuracy45s · 1 pt

In the compiler front end (Source → Scanner → ? → Parser → AST), what does the scanner produce?

A An abstract syntax tree
3
B A parse tree
5
C A stream of tokens
150
✓ Correct
D Machine code
2
160 / 194 answered · 94% correct
Q2 of 13Multiple ChoiceAccuracy60s · 1 pt

Using the lecture's tokenize function (with re_num = "[0-9]+"), what does this return?

tokenize "12+3*4"
A [Tok_Num "1"; Tok_Num "2"; Tok_Add; Tok_Num "3"; Tok_Mult; Tok_Num "4"; Tok_END]
22
B [Tok_Num "12"; Tok_Add; Tok_Num "3"; Tok_Mult; Tok_Num "4"; Tok_END]
148
✓ Correct
C [Tok_Num "12"; Tok_Add; Tok_Num "3"; Tok_Mult; Tok_Num "4"]
5
D Add (Num 12, Mult (Num 3, Num 4))
175 / 194 answered · 85% correct
Q3 of 13Multiple ChoiceAccuracy60s · 1 pt

The lecture's token type is:

type token = Tok_Num of string | Tok_Add | Tok_Mult | Tok_END

What happens when we evaluate tokenize "2 - 3"?

A It returns [Tok_Num "2"; Tok_Num "3"; Tok_END], skipping the -
9
B It returns [Tok_Num "2"; Tok_Num "-3"; Tok_END]
29
C It raises ParseError
34
D It raises IllegalExpression, because no regex matches -
114
✓ Correct
186 / 194 answered · 61% correct
Q4 of 13Multiple ChoiceAccuracy60s · 1 pt
let tokens = [Tok_Num "2"; Tok_Add; Tok_Num "3"; Tok_END]
let x = lookahead tokens

What is the value of x?

A 2
5
B Tok_Num
2
C Tok_Num "2"
177
✓ Correct
D [Tok_Add; Tok_Num "3"; Tok_END]
8
192 / 194 answered · 92% correct
Q5 of 13Multiple ChoiceAccuracy60s · 1 pt
let tokens = [Tok_Num "2"; Tok_Add; Tok_Num "3"; Tok_END]
let x = match_token tokens Tok_Add

What is the value of x?

A [Tok_Num "3"; Tok_END]
12
B It raises an exception
155
✓ Correct
C Tok_Add
15
D [Tok_Add; Tok_Num "3"; Tok_END]
12
194 / 194 answered · 80% correct
Q6 of 13Multiple ChoiceAccuracy60s · 1 pt
let tokens = [Tok_Num "2"; Tok_Add; Tok_Num "3"; Tok_END]
let x = match_token tokens (Tok_Num "2")

What is the value of x?

A 2
4
B It raises an exception
13
C Tok_Num "2"
5
D [Tok_Add; Tok_Num "3"; Tok_END]
172
✓ Correct
194 / 194 answered · 89% correct
Q7 of 13Multiple ChoiceAccuracy75s · 1 pt

The lecture's recursive descent parser implements:

S → A + S | A
A → B * A | B
B → 0 | 1 | … | 9

What does parse "2*3+4" return?

A Mult (Num 2, Add (Num 3, Num 4))
16
B Add (Num 2, Mult (Num 3, Num 4))
14
C Mult (Add (Num 2, Num 3), Num 4)
6
D Add (Mult (Num 2, Num 3), Num 4)
158
✓ Correct
194 / 194 answered · 81% correct
Q8 of 13Multiple ChoiceAccuracy75s · 1 pt

The lecture's recursive descent parser implements:

S → A + S | A
A → B * A | B
B → 0 | 1 | … | 9

What does parse "1 + 2 + 3" return?

A Add (Num 1, Add (Num 2, Num 3))
165
✓ Correct
B Add (Add (Num 1, Num 2), Num 3)
22
C Add (Num 1, Num 2)
1
D It raises ParseError
5
193 / 194 answered · 85% correct
Q9 of 13Multiple ChoiceAccuracy60s · 1 pt

Using the lecture's parser for

S → A + S | A
A → B * A | B
B → 0 | 1 | … | 9

what happens when we evaluate parse "2+"?

A It returns Num 2
2
B It returns Add (Num 2, Num 0)
C It raises ParseError: parse_B sees Tok_END where a number is expected
191
✓ Correct
D It raises IllegalExpression from tokenize
1
194 / 194 answered · 98% correct
Q10 of 13Multiple ChoiceAccuracy75s · 1 pt
let rec parse_S tokens =
  let e1, t1 = parse_A tokens in
  match lookahead t1 with
  | Tok_Add ->
    let t2 = match_token t1 Tok_Add in
    let e2, t3 = parse_S t2 in
    (Add (e1, e2), t3)
  | _ -> (e1, t1)

What does parse_S (tokenize "2 3") return?

A It raises ParseError
17
B (Num 2, [Tok_Num "3"; Tok_END])
118
✓ Correct
C (Num 23, [Tok_END])
37
D (Num 3, [Tok_END])
19
191 / 194 answered · 62% correct
Q11 of 13Multiple ChoiceAccuracy45s · 1 pt

For which grammar does a straightforward recursive descent parser loop forever?

A S → aS | ε
11
B S → Sa | ε
173
✓ Correct
C S → aSa | bSb | x
4
D S → xyz | abc
4
192 / 194 answered · 90% correct
Q12 of 13Multiple ChoiceAccuracy60s · 1 pt

Which grammar is the result of eliminating left recursion from

S → Sa | Sb | c
A S → aS | bS | c
1
B S → Lc, L → aL | bL | ε
C S → cS | a | b
1
D S → cL, L → aL | bL | ε
✓ Correct
2 / 194 answered · 0% correct
Q13 of 13Multiple ChoiceAccuracy60s · 1 pt

Why can't a predictive parser with one token of lookahead (LL(1)) parse this grammar as written?

S → ab | ac
A Both productions start with a, so the lookahead cannot choose between them
177
✓ Correct
B The grammar is left recursive
2
C The grammar is ambiguous
7
D The grammar has no ε production
4
190 / 194 answered · 93% correct

Score Distribution

4
15
57
88
30
0–1
2–3
4–5
6–7
8–9
10–11
12–13

Score (points) →

Players
194
Average Score
9.8
High Score
12
Max Possible
13