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