Question

In: Computer Science

1-Give a BNF grammar (do not use EBNF) for the language that generates the set of...

1-Give a BNF grammar (do not use EBNF) for the language that generates the set of all strings consisting of the keyword begin, followed by zero or more statements with a semicolon after each one, followed by the keyword end. Use the non-terminal for statements, and do not give productions for it.

2-Give a BNF grammar (do not use EBNF) for the language that generates the set of all strings consisting of the keyword begin, followed by one or more statements with a semicolon after each one, followed by the keyword end. Use the non-terminal for statements, and do not give productions for it.

3-Give a BNF grammar (do not use EBNF) for the set of all strings consisting of zero or more as, with a comma between each a and the next. (There should be no comma before the first or after the last.)

4-Give a BNF grammar (do not use EBNF) for the set of all strings consisting of an open bracket (the symbol [) followed by a list of zero or more digits separated by commas, followed by a closing bracket (the symbol ]).

Solutions

Expert Solution

1)
A sentence is a noun
phrase, a verb, and a
noun phrase.   
<S> ::= <NP> <V> <NP>
A noun phrase is an
article and a noun.   
<NP> ::= <A> <N>    
A verb is            
<V> ::= eats | loves | hates
An article is
<A> ::= a | the
A noun is   
<N> ::= dog | cat | rat
grammar consists of four parts:
-the set of terminals is the atomic
symbols that make up the language
- the set of productions: tree-building rules that define
possible children for each nonterminal
- the start symbol: the nonterminal that forms the root
of any parse tree for the grammar.
-Such grammars are sometimes called
context-free grammars is left-hand
side of each production is one nonterminal
-We can use any production for a given
nonterminal to decide what children to give
it, without looking at the rest of the tree.
-If you take CS you will see one way of
expressing CFG’s
S → aSb | X
X → cX | ∈.

2)
-Give a BNF grammar for the set of all strings consisting of the keyword begin,
followed by one or more statements with a semicolon after each one, followed by the
keyword end. Use the non-terminal <statement> for statements, and do not give
productions for <statement>.
-Using your grammar in #1, the syntax tree for the string
begin <statement> ; <statement> ; <statement> ; end.
Backus-Naur form (BNF) is a formal notation for encoding grammars intended many programming languages, protocols or formats have a BNF description in their specification.
Every rule in Backus-Naur form has the following structure:
name ::= expansion
The symbol ::= means "may expand into" and "may be replaced with."
In some texts, a name is also called a non-terminal symbol.

Example:
<expr> ::= <term> "+" <expr>
| <term>

<term> ::= <factor> "*" <term>
| <factor>

<factor> ::= "(" <expr> ")"
| <const>

<const> ::= integer.
3)
-The set of all strings consisting of zero or moreas.
-The set of all strings consisting of an uppercase letter followed by zero ormore additional characters, each of which is either an uppercase letter or oneof the digits0through9.
-The set of all strings consisting of one or more.
-The set of all strings consisting of one or more digits.
-The set of all strings consisting of zero or moreas with a semicolon aftereach one.
-The set of all strings consisting of the keywordbegin, followed by zero ormore statements with a semicolon after each one, followed by the keywordend. Use the non-terminal<statement>for statements, and do not giveproductions for it.
4)
-The set of all strings consisting of one or moreas with a semicolon after eachone.
-The set of all strings consisting of the keywordbegin, followed by one ormore statements with a semicolon after each one, followed by the keywordend. Use the non-terminal<statement>for statements, and do not giveproductions for it.
-The set of all strings consisting of one or moreas, with a comma betweeneachaand the next.
-The set of all strings consisting of an open bracket followedby a list of one or more digits separated by commas, followed by a closingbracket.
-The set of all strings consisting of zero or moreas, with a comma betweeneachaand the next.
-The set of all strings consisting of an open bracket followedby a list of zero or more digits separated by commas, followed by a closing bracket.


Related Solutions

Q1)   Convert the following set of BNF rules to a single EBNF rule. <E> --> <E>...
Q1)   Convert the following set of BNF rules to a single EBNF rule. <E> --> <E> + <T> | <E> - <T> | <T> Q2)   Briefly explain how the expected type and actual type of <expr>             in the following two BNF rules are determined: <assign> --> <var> = <expr> (Rule 1) <expr> --> <var> + <var> (Rule 2)
Compare the language generated by Grammar #2 to the language generated by Grammar #1. How do...
Compare the language generated by Grammar #2 to the language generated by Grammar #1. How do the two languages compare to each other? (Is one language a proper subset of the other? Does each language contain a string that the other one does not? Do both grammars generate the very same language?) grammar #1 where S is the start symbol   S → tV | iC | iV | i C → tV V → iC | iV | i Grammar...
Give a grammar that generates each of the following languages: (1) L = {a2n cm bm...
Give a grammar that generates each of the following languages: (1) L = {a2n cm bm | m ≥ 0, n ≥ 0} (2) L = {an b2m cn | m ≥ 0, n ≥ 0} (3) L = {an bm | n+m is even} (4) L = {an bm | n ≤ m+3} (5) The complement of the language L = {anbn | n ≥ 0}
Construct an NPDA that accepts the language defined by the given grammar and give the language...
Construct an NPDA that accepts the language defined by the given grammar and give the language in a formal expression (including a regular expression). S -> AA|a, A-> SA|ab
a) Give 5 good examples of Regular language. b) Give 5 good examples of Regular Grammar.
a) Give 5 good examples of Regular language. b) Give 5 good examples of Regular Grammar.
Exercises: Home Work to do : 1) write BNF grammars for the following sententials: lexemes (not...
Exercises: Home Work to do : 1) write BNF grammars for the following sententials: lexemes (not tokens) - positive and negative integer numbers - floating point numbers - variable names in C ------------------------------------ 2) check (recognize) if this code can derive from the given grammar ? - swith (tatal) { case total > 1000 : cout<<"expensive"; break; case total = 1000 : cout<<"exact"; break; case total < 1000 : cout<<"cheap"; break; }
Add the result envidented by the python compiler. use python language. 1(a) A Give the output...
Add the result envidented by the python compiler. use python language. 1(a) A Give the output for the array([ 0, 1, 8, 27, 64, 125, 216, 343, 512, 729]) a. a[:6:2] = -1000 (ii) a[ : :-1] ) b. Display the values of 1D array using for loop?
 c. Write a code to print a 3D array?
 d. How to print the transpose of a 2D array?
 e Write a function to sort the array row and coloumn wise ?...
Exercise 1 JAVA LANGUAGE The system generates a random number from a given range, say 1...
Exercise 1 JAVA LANGUAGE The system generates a random number from a given range, say 1 to 100. The user is prompted to enter their given number in a displayed dialogue box. The computer then tells if the entered number matches the guesses number or it is higher/lower than the generated number. The game continues under the user guessing the number. Throw an exception if user does enter an integer – have user reenter
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs in the data base are one-way. Question: Write a program that can answer if there is a path between any to vertices. For the vertex pairs use this as your input example: AL...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data...
Language: Java or C (NO OTHER LANGUAGE) Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.) Have a unit test implemented in main(). And comment every code. Show examples from the executions. Assume that the edges defined by the vertex pairs are two-way. Question: First step: write a program based on DFS which can answer questions of the type: "Find the a path from X to Y" Which should result in a list of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT