Question

In: Computer Science

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;

        }

Solutions

Expert Solution

1.

BNF Grammar for

a. positive and negative integer numbers

<number> ::= <num> | - <num>

<num> ::= <digit> | <num> <digit>

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

Here, a number can be simply a number or it can be a negative number(preceded by -)

Num can be a digit or a num followed by digit

A digit can be 0 or 1 or 2.... or 9

b. Floating point numbers

<real> ::= <num>.<fraction>

<num> ::= <digit> | <num> <digit>

<fraction> ::= <digit> | <digit> <fraction>

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

A floating point number can a num followed by . (dot) followed by fractional part

Num can be a digit or a num followed by digit

A digit can be 0 or 1 or 2.... or 9

A fractional part can a digit or a digit followed by a fraction

c. Variable names in C

<var> ::= <letter> | <letter> <word>

<word> ::= <letter> | <digit> | <letter><word> | <digit><word>

<letter> ::= a |b|c|.....|y|z|_

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

Note: According to the policy, in case of multiple questions only 1st ques is mandatory. So, done that only as per the time permits.


Related Solutions

Here are two sets of BNF grammars specifying the syntax of a language: <exp> ::= <term>...
Here are two sets of BNF grammars specifying the syntax of a language: <exp> ::= <term> | <term> + <exp> | <term> - <exp> <term> ::= <op> | <op> * <term> <op> ::= <int> | <id> | (<exp>) ********************************************* <exp> ::= <term> | <term> + <exp> | <term> - <exp> | (<exp>) <term> ::= <op> | <op> * <term> <op> ::= <int> | <id> <int> will lead to non-terminals of a digit or digit sequence '123' etc. <id> will lead...
For the following grammars, write the leftmost derivation for the strings given with each. Next to...
For the following grammars, write the leftmost derivation for the strings given with each. Next to each derivation step, write the number of the rule used. Do not combine steps. grammar (6 rules), Σ = {0, 1}: S -> A1B A -> 0A | ε B -> 0B | 1B | ε strings (3): 1, 0011, 01010 grammar (6 rules), Σ = { +, x, a, (, ) } E -> E + T | T T -> T x...
Draw parse trees for nine strings in question one, using their grammars. For the following grammars,...
Draw parse trees for nine strings in question one, using their grammars. For the following grammars, write the leftmost derivation for the strings given with each. Next to each derivation step, write the number of the rule used. Do not combine steps.   grammar (3 rules), Σ = {a, b}: S -> aSbS | bSaS | ε strings (3): ab, baab, bbaa grammar (6 rules), Σ = {0, 1}: S -> A1B A -> 0A | ε B -> 0B |...
Write the BNF for mini Java language based of the following information: Data Types Integer Int...
Write the BNF for mini Java language based of the following information: Data Types Integer Int Long Double Boolean Char References Complex Data Structures Arrays int v[30]; Classes member variables class Name { int a; char b; char name[25]; } Methods Return data type Primitive data type Void Method Name Parameter list Could be empty Statement Block { Variable declarations Executable Statements } Program Variable Declarations Class Definitions Methods Only one method named "main" but must have one method named...
The home work is to do a survey comparing the following three programming language: COBOL ,...
The home work is to do a survey comparing the following three programming language: COBOL , FOTRAN 77, and PASCAL regarding: 1-   Character set. 2-   Reserved(key) words and special symbols. 3-   Simple data types and data structures used. 4-   Control structures used (selection and loop structures) 5-   File structure used.
For the following exercises, write the first eight terms of the sequence.
For the following exercises, write the first eight terms of the sequence.
For the following exercises, write the first eight terms of the sequence.
For the following exercises, write the first eight terms of the sequence.
For the following exercises, write an explicit formula for each sequence.
For the following exercises, write an explicit formula for each sequence.
For the following exercises, write the first four terms of the sequence.
For the following exercises, write the first four terms of the sequence.
For the following exercises, write the vector shown in component form.
For the following exercises, write the vector shown in component form. 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT