In: Computer Science
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; }
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.