Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE READ CAREFULLY Be sure you\'ve developed a BNF grammar that describes the

ID: 3881748 • Letter: P

Question

PLEASE READ CAREFULLY

Be sure you've developed a BNF grammar that describes the language of phone numbers according to the Practice BNF item. Let's call this the US phone number grammar.

One variation in how we write down US phone numbers is that we sometimes leave off the parentheses around the area code and instead use a dash after it: 757-555-0000 for example. .

Many businesses have adopted the practice of using "." (period) instead of "-" (dash) between the number groups: 757.555.0000 for example.

US phone numbers consist to 10 digits. Telephone numbers in other countries may consist of up to 15 digits.

By convention, international telephone numbers begin with a plus sign (+) followed by a 1, 2, or 3-digit country code. The US country code is "1". So the example US phone number would be written as  +1757.555.0000 in this international style.

THE CHALLENGE

1. (2 pts) Write down a BNF grammar for US phone numbers that use the dash after the area code instead of parentheses around it.

2. (3 pts) Write down a BNF grammar for US phone number written in the international style.

3. (10 pts) Write down a grammar for international phone numbers in countries other than the US. Assume that no phone number is smaller than than 7 digits, and no number group is smaller than 2 digits.

4. (15 pts) Write a program that allows the user to input a character string from the keyboard, and tell the user if that string complies with the US phone number grammar. Your program should output that string, followed by "YES" if the string is acceptable, and "NO" if it is not. So, an example output from this program could be:

(757)555-1234 YES

Your program must give the correct (Yes/No) answer when presented with the following phone numbers:

757-555-0025

(757)555-1234

75-555-0000

757.555.1234

757-043-1234

057-555-1234

057-MH5-1234

OPTIONAL: If you get your program working and want to challenge yourself further, make the following change: When the program outputs "NO" have it also output a reason why the string is unacceptable.

THE EVIDENCE

For #1, #2, #3, you must submit

a WORD document into which you have typed your grammars

OR

Your grammars written by hand on paper. You must bear down on the pencil or pen so it's dark enough to be legible, and write LARGE and CLEARLY ENOUGH for me to read it. Then either scan or take photo(s) of your solutions. If I am unable to read your submission, I will not grade it. So please check the readability before you submit.

For #4 (program):you must submit:

code - file save, or copy/paste into Notepad from your IDE or editor. I must be able to open or copy/paste the code from your submission to run it myself.

Runs of your program showing it correctly handled the test cases - file save from your IDE or OS of choice; a screen shot also is acceptable

Explanation / Answer

Solution:

     BNF(Backus-Naur Form) is a notation for writing context-free grammars that allows for the use of arbitrary characters, as well as improving readability. It was invented by John Backus and improved by Peter Naur in the late 1950s and early 1960s. BNF, or some variation on it, is used to specify the syntax of many programming languages.

In BNF, nonterminals are enclosed in angle brackets: <digit>. Terminals are not. If it is not clear which symbols are terminals, then terminals may be enclosed in quotes. Our arrow is replaced by “::=”, and vertical bars (|) are used the same way we have.

For example, here is a BNF production specifiying what a digit is:

<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

Some versions would allow the quotes to be left off, above.

Here is a BNF grammar for a phone number. It allows things like  757-555-0000 and (757)555-1234. The above <digit> production should be regarded as part of this grammar. The start symbol is <phone-number>.

<phone-number> ::= <area-code> <7-dig> | <7-dig>

<area-code> ::= "(" <digit> <digit> <digit> ")"

<7-dig> ::= <digit> <digit> <digit> "-" <digit> <digit> <digit> <digit>

<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

2.Write down a BNF grammar for US phone number written in the international style.

Solution:

Here is a BNF grammar for a phone number. It allows things like  +1757.555.0000. The above <digit> production should be regarded as part of this grammar. The start symbol is <phone-number>.

<phone-number> ::= “+” <num><area-code> <7-dig> | <7-dig>

<num> ::= 1

<area-code> ::= "(" <digit> <digit> <digit> ")"

<7-dig> ::= <digit> <digit> <digit> "-" <digit> <digit> <digit> <digit>

<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

3. Write down a grammar for international phone numbers in countries other than the US. Assume that no phone number is smaller than than 7 digits, and no number group is smaller than 2 digits.

<phone-number> ::= “+” <digit><area-code> <7-dig> | <7-dig>

<area-code> ::= "(" <digit> <digit> <digit> ")"

<7-dig> ::= <digit> <digit> <digit> "." <digit> <digit> <digit> <digit>

<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

4. Write a program that allows the user to input a character string from the keyboard, and tell the user if that string complies with the US phone number grammar. Your program should output that string, followed by "YES" if the string is acceptable, and "NO" if it is not. So, an example output from this program could be:

057-MH5-1234

<area-code> ::= "(" <digit> <digit> <digit> ")"

<3-letter> ::= A | B | C |D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z                  

<4-dig> ::= <digit> <digit> <digit> <digit>

<digit> ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote