Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

Explain the importance of Human Resource Management

Answered: 1 week ago

Question

Discuss the scope of Human Resource Management

Answered: 1 week ago

Question

Discuss the different types of leadership

Answered: 1 week ago