Hw10 solutions (UCB CS61A@2021 Fall)
BNF
Q1: Grouping and Pipes
In this question, you will add support for grouping and piping.
Recall that grouping allows for an entire regular expression to be treated as a single unit, and piping allows for a pattern to match an expression on either side. Combined, these will let us create patterns which match multiple strings!
Define the
group
andpipe
expressions in your grammar.
- A
group
consists of anyregex
expression surrounded by parentheses (()
).- A
pipe
operator consists of aregex
expression, followed by a pipe (|
) character, and lastly followed by anotherregex
expression.For example,
r"apples"
would match exactly the phrase “apples” in an input. If we wanted our pattern from before to match “oranges” as well, we could expand ourrstring
to do so using groupings and pipes:r"(apples)|(oranges)"
.