Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8. Assume that we represent dollar amounts in the following way: $ number. numbe

ID: 3680411 • Letter: 8

Question

8. Assume that we represent dollar amounts in the following way:

$ number. numberCR

The dollar sign and the dollar value must be present. The cents part (including both decimal point and the number) and the CR ( which stands for Credit and is how businesspeople represent negative numbers) are both optional , and the numbers is a variable –length sequence of one or more decimal digits. An example of the legal dollars amount includes $995, $99CR, $199.95, and $500.000CR.

a) Write BNF grammar for the dollar amount just described.

b) Modify your gramma so that the cents parts is no longer an arbitrary long sequence of digits but is the exactly two digits , no more and no less.

c) Using your gramma from either exercise 8a or 8b , show a parse tree for $19.95CR.

Explanation / Answer

Use the below code for BNF Grammer for the dollar amount :

/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL
%%
calclist: /* nothing */ matches at beginning of input
| calclist exp EOL { printf("= %d ", $1); } EOL is end of an expression
;
exp: factor default $$ = $1
| exp ADD factor { $$ = $1 + $3; }
| exp SUB factor { $$ = $1 - $3; }
;
factor: term default $$ = $1
| factor MUL term { $$ = $1 * $3; }
| factor DIV term { $$ = $1 / $3; }
;
term: NUMBER default $$ = $1
| ABS term { $$ = $2 >= 0? $2 : - $2; }
;
%%
main(int argc, char **argv)
{
yyparse();
}
yyerror(char *s)
{
fprintf(stderr, "error: %s ", s);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote