Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Design and implement the c-like language for the following simple calculator (interpreter). Language design (syntax and semantic) 1 Each line is a statement (a command)

Design and implement the c-like language for the following simple calculator (interpreter).

Language design (syntax and semantic)

1 Each line is a statement (a command) ended with ;.

Support the following statement (command)

1.1 end-statement to exit the program with a return code. For example, end(0);

1.2 assignment-statement using ":=" for assignment operator. For example, a := 1;

1.3 print-statement. For example, print(10); a := 3; print(a); print(a+10);

2. Symbol Table for variable name

2.1 Support a symbol table to handle many variable entries

2.2 The length of a variable-name can be up to 255 characters long

2.3 The data type of each variable is double

3. Arithmetic expression (+, -, *, /)

3.1 Support an arithmetic expression to handle (+, -, *, /) with double and parenthesis

3.2 Support the following math functions (mod, abs, sqrt, exp, exp10, log, log10, sin, cos, tan).

3.3 Support conditional expression (?: in C/C++).

The syntax of the conditional expression is as shown below.

( conditional ) ? ( expression1 ) : ( expression2 )

The conditional is true if its return value is not zero; otherwise it is false if it is zero.

For example, b := ( 1 + 1 ) ? (100) : (200) ; // b will be set to be 100.

4. Design and provide test cases for your language to be tested.

Test cases should be complete (or at least to test each case of the requirements shown above).

The sample code for the makefile:

{cslinux1:~/yacc/calc2} ls calc2.y Makefile {cslinux1:~/yacc/calc2} cat Makefile NAME = calc2 $(NAME): $(NAME).tab.c gcc $(NAME).tab.c -o $(NAME) $(NAME).tab.c: $(NAME).y bison -vd $(NAME).y {cslinux1:~/yacc/calc2} make bison -vd calc2.y gcc calc2.tab.c -o calc2 {cslinux1:~/yacc/calc2} ls calc2 calc2.tab.c calc2.tab.h calc2.y Makefile {cslinux1:~/yacc/calc2} ./calc2 Enter expression: 1+2 3 2*3 6 ^C // commentary 1. check two files calc2.y (yacc file) for calculator Makefile to make an executable 2. Listing of Makefile 3. compile and make the executable: calc2 4. run the program 1+2 => 3 2*3 => 6 Cntl-C or Cntl-D to terminate the program This is the code for the calulator:

// Aho Fig4-61.y %{ #include #include #include #include #include void yyerror(char *); #define YYSTYPE double %} %token NUMBER %left '+' '-' %left '*' '/' %right UMINUS %% lines : lines expr ' ' { printf("%g ", $2); } | lines ' ' | /* empty */ | error ' ' {yyerror("reenter previous line:");} ; expr : expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { $$ = $1 / $3; } | '(' expr ')' | '-' expr %prec UMINUS { $$ = - $2;} | NUMBER ; %% yylex() { int c; while (( c = getchar() ) == ' '); if (( c == ',') || (isdigit(c))) { ungetc(c, stdin); scanf("%lf", &yylval); return NUMBER; } return c; } main(){ printf("Enter expression: "); yyparse(); } void yyerror(char *s) {fprintf(stderr, "%s ",s);} int yywrap(){return 1;}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions