Question

Try to write a BNF like Grammar for each one of the following constructs: - positive...

Try to write a BNF like Grammar for each one of the following constructs:

- positive and negative integer numbers

- floating point numbers

- variable names.

Notice: Recursion is needed here (use right recursion):

here is an example (a grammar of two rules defines any positive integer) :

Note the length in digits is not defined in this grammar, it can go recursively for ever.

So we need to add another annotation (a semantic part).

<positive_Number> -> <digit> | <digit> <positive_Number> (rule-1)

<digit> -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9   (rule-2)

Now by parsing one char at a time, any positive number can be recognized and generated by programs.

I bet you can write a little code (automaton like) to do what compilers do.

Homework Answers

Answer #1

BNF for positive and negative numbers:

<posneg>-><sign><number>

<number>-><digit>|<digit><number>

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

<sign>->+|-

BNF for floating point numbers:

<float>-><number>.<number>

<number> -> <digit> | <digit> <number>

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

BNF for variable names:

<variable>-><char>|<char><alphanumeric>

<char>->_|A|B|....|Z|a|b|.....|z

<alphanumeric>->_|A|B|....|Z|a|b|.....|z|0|1|...|9

I don't understand the last part of the question. Please comment what you mean, I will update the answer accrdingly. Do you want a code for a compiler or just the parser? A parser parses a BNF grammar.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a BNF grammar for a new language called 4040. It should include the following rules:...
Write a BNF grammar for a new language called 4040. It should include the following rules: Variable definitions should start with var. There must be only single space after var. Variable names should come after the var and single space. Variable names should start with a letter or underscore(_). Starting with a digit is not allowed. Variable names can have digits after the first letter. There can be multiple variable names separated by comma. Between variables, spaces are allowed. There...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using,...
There are two ways to write loops: (1) iterative, like the for-loops we're used to using, and (2) recursive. Your prerequisite preparation for this course should have exposed you to both, although your working knowledge of recursive loops may not be as strong as that of iterative loops. Consider the following iterative function that prints an array of characters backward: #include <iostream> #include <cstring> // print an array backwards, where 'first' is the first index // of the array, and...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
1) Describe an example of each of the following that may be found of your kitchen:...
1) Describe an example of each of the following that may be found of your kitchen: Explain how your choice falls into this category, and if there is a chemical name or symbol for it, provide that as well. Provide a photo of your example with your ID card in it. a) a compound b) a heterogeneous mixture c) an element (symbol) Moving to the Caves… Lechuguilla Caves specifically. Check out this picture of crystals of gypsum left behind in...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT