Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Recall * * Cambridge Polish Notation * * ( * * CPN * * ) , in which operators and their operands are enclosed in

Recall **Cambridge Polish Notation**(**CPN**), in which operators and their operands are enclosed in parenthesis.
For example, `(add 12)` is the **CPN** equivalent of `1+2`.
Expressions in **CPN** can be nested, so the following expression would be valid, and would result in `6`.
```
(add 1(add 23))
```
Your must create a functioning **CPN** calculator which works with the functions listed in the **FUNC_TYPE** enum in the provided code through the **MIN_FUNC** function (all functions appearing after **MIN_FUNC** in the enum will be implemented in later tasks.
This calculator will serve as the core functionality for **CI-LISP**.
You may want to check out the [sample runs](#sample-runs) below better understand what will be implemented before reading further.
The initial grammar is as follows:
```
program ::= s_expr EOL | s_expr EOFT | EOL | EOFT
s_expr ::= f_expr | number | QUIT
f_expr ::=( FUNC s_expr_section )
s_expr_section ::= s_expr_list |
s_expr_list ::= s_expr | s_expr s_expr_list
FUNC ::= neg | abs | add | sub |
mult | div | remainder | exp |
exp2| pow | log | sqrt |
cbrt | hypot | max | min
number ::= INT | DOUBLE
INT ::= optional +/-,
then some digits
DOUBLE ::= optional +/-,
then some digits,
then a decimal point,
then optionally some more digits
QUIT ::= quit
```
The non-terminals **s_expr** and **f_expr** are shorthand:
***s_expr** means *symbolic* expression
***f_expr** means *function* expression
## DATA STRUCTURES
Next we'll discuss the structures provided in [cilisp.h](../src/cilisp.h). For a more intuitive understanding of the structures and their names defined here, note that **AST** is short for *__A__bstract __S__yntax __T__ree*. As such, the structures discussed below are intended to house data in an abstract syntax tree.
### NUMBERS
The **NUM\_TYPE** enum and **AST\_NUMBER** struct define everything necessary to house both numeric data types (integers and floating point values) in a single struct.
```c
typedef enum num_type {
INT_TYPE,
DOUBLE_TYPE
} NUM_TYPE;
typedef struct {
NUM_TYPE type;
double value;
} AST_NUMBER;
typedef AST_NUMBER RET_VAL;
```
Note that even when an **AST\_NUMBER**'s type is **INT\_TYPE**, we will be storing it's actual value as a double under the hood. This is to make function evaluation a little less tedious; students wishing to instead use this alternative **AST\_NUMBER** are welcome to do so:
```c
typedef struct {
NUM_TYPE type;
union {
double floating_point;
long integral;
} value;
} AST_NUMBER;
```
Each **AST\_NUMBER** will house a member of the **NUM\_TYPE** enum (denoting the type of number being stored) alongside a numeric value.
The last line, which gives the **AST\_NUMBER** struct a second name (**RET\_VAL**), exists exclusively for readability; when we evaluate a part of our syntax tree, the result will always be a number. We will use **AST\_NUMBER**s to house numbers which are *part of* the syntax tree, and **RET\_VAL**s to house the results of evaulation (*__RET__urned __VAL__ues*).
### FUNCTIONS
The terms "operator" and "function" will be used interchangeably for our discussion here.
We'll start our discussion of functions with the enum and struct definitions in [cilisp.h](../src/cilisp.h).
```c
typedef enum func_type {
NEG_FUNC,
ABS_FUNC,
ADD_FUNC,
// TODO complete the enum
CUSTOM_FUNC
} FUNC_TYPE;
```
This enum will list all functions which will be implemented throughout the course of the project. Clearly, it isn't yet complete, hence the `TODO`. You'll add more functions to it as you work through this task, and through future tasks.
When function calls are parsed as **f_expr**s, the data for said calls will be stored in an **AST\_FUNCTION**:
```c
typedef struct {
FUNC_TYPE func;
struct ast_node *opList;
} AST_FUNCTION;
```
This struct stores the operator used in the function call (a member of the **FUNC\_TYPE** enum) and the operands for the function (a linked list of **struct ast\_node**s, which we will discuss in the [GENERIC NODES](#generics) section below).
Take note of the **resolveFunc** function
[cilisp.c](../src/cilisp.c). This function takes as input a function's name (in string form), and outputs the corresponding **FUNC\_TYPE** member. It will be used to assign value to **FUNC** tokens while lexing.
**resolveFunc** works because the array of function names (**funcNames**, in [cilisp.c](../src/cilisp.c)) lists all functions in the same order as the **FUNC\_TYPE** enum. If either of these is edited, the other must also be edited to match. If they are not kept in sync,**resolveFunc** will not work. **CUSTOM\_FUNC** should be left as the last element of **resolveFunc** for the same reason; user-defined functions will be implemented at the end of the project, and we'll know a function

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

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

Students also viewed these Databases questions

Question

What do you like most about the organization?

Answered: 1 week ago