Hw10 题解 (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
groupandpipeexpressions in your grammar.
- A
groupconsists of anyregexexpression surrounded by parentheses (()).- A
pipeoperator consists of aregexexpression, followed by a pipe (|) character, and lastly followed by anotherregexexpression.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 ourrstringto do so using groupings and pipes:r"(apples)|(oranges)".