Question: File Reading, EBNF, Regular Expressions, and Pythons re Module PYTHON 3 : Define a function named expand_re that takes one dict as an argument, representing

File Reading, EBNF, Regular Expressions, and Pythons re Module

PYTHON 3 : Define a function named expand_re that takes one dict as an argument, representing various names and their associated RE patterns; expand_re returns None, but mutates the dict by repeatedly replacing each name by its pattern, in all the other patterns. The names in patterns will always appear between #s. For example, if p is thedict {digit: r\d, integer: r[+-]?#digit##digit#*} then after calling expand_re(p), p is now the dict {'integer': '[+-]?(?:\\d)(?:\\d)*', 'digit': '\\d'}. Notice that digit remains the same (the raw string r\d prints as'\\d'), but each #digit# in integer has been replaced by its associated pattern and put inside a pair of parentheses prefaced by ?:. Hint: For every rule in the dictionary, substitute (see thesub function in re) all occurrences of its key (as a pattern, in the form #key#) by its associated value (always putting the value inside parentheses), in every rule in the dictionary. The order in which names are replaced by patterns is not important. Hint: I used re.compile for the #key# pattern (here no ^ or $ anchors!), and my function was 4 lines long (this number is not a requirement).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!