Question: PYTHON REGULAR EXPRESSIONS 3. (7 pts) EBNF allows us to name rules and then build complex descriptions whose right-hand sides use these names. But Regular

PYTHON REGULAR EXPRESSIONS

PYTHON REGULAR EXPRESSIONS 3. (7 pts) EBNF allows us to name rules

3. (7 pts) EBNF allows us to name rules and then build complex descriptions whose right-hand sides use these names. But Regular Expression (RE) patterns are not named, so they cannot contain the names of other patterns. It would be useful to have named REs and use their names in other REs. In this problem we will represent named RE patterns by using a dict (whose keys are the names and whose associated values are RE patterns that can contain names), and then repeatedly replace the names by their RE patterns, to produce complicated RE patterns that contains no names Define a function named expand re that takes one dict as an argument, representing various names and their associated RE pattems; expand re returns None, but mutates the dict by repeatedly replacing each name by its pattern, in all the other patterns. The names in pattems will always appear between #s. For example, if p is the dict {digit: r'\d', integer : r' [+1digit##digit#*'} then after calling expand.re (p), pis now the dict 'integer': ' [+-1? (?:\\d) (?:\\d)*', 'digit': '\\d'). Notice that digit remains the same (the raw string r'\d' prints as 'way, 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 the sub function in re) all occurrences of its key (as a pattern, in the form #ke ) 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 #keyt pattern (here no ^ oranchors!), and my function was 4 lines long (this number is not a requirement). 3. (7 pts) EBNF allows us to name rules and then build complex descriptions whose right-hand sides use these names. But Regular Expression (RE) patterns are not named, so they cannot contain the names of other patterns. It would be useful to have named REs and use their names in other REs. In this problem we will represent named RE patterns by using a dict (whose keys are the names and whose associated values are RE patterns that can contain names), and then repeatedly replace the names by their RE patterns, to produce complicated RE patterns that contains no names Define a function named expand re that takes one dict as an argument, representing various names and their associated RE pattems; expand re returns None, but mutates the dict by repeatedly replacing each name by its pattern, in all the other patterns. The names in pattems will always appear between #s. For example, if p is the dict {digit: r'\d', integer : r' [+1digit##digit#*'} then after calling expand.re (p), pis now the dict 'integer': ' [+-1? (?:\\d) (?:\\d)*', 'digit': '\\d'). Notice that digit remains the same (the raw string r'\d' prints as 'way, 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 the sub function in re) all occurrences of its key (as a pattern, in the form #ke ) 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 #keyt pattern (here no ^ oranchors!), 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!