Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Recall * * Cambridge Polish Notation * * ( * * CPN * * ) , in which operators and their operands are enclosed in
Recall Cambridge Polish NotationCPN in which operators and their operands are enclosed in parenthesis.
For example, add is the CPN equivalent of
Expressions in CPN can be nested, so the following expression would be valid, and would result in
add add
Your must create a functioning CPN calculator which works with the functions listed in the FUNCTYPE enum in the provided code through the MINFUNC function all functions appearing after MINFUNC in the enum will be implemented in later tasks.
This calculator will serve as the core functionality for CILISP
You may want to check out the sample runs#sampleruns below better understand what will be implemented before reading further.
The initial grammar is as follows:
program :: sexpr EOL sexpr EOFT EOL EOFT
sexpr :: fexpr number QUIT
fexpr :: FUNC sexprsection
sexprsection :: sexprlist
sexprlist :: sexpr sexpr sexprlist
FUNC :: neg abs add sub
mult div remainder exp
exp 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 nonterminals sexpr and fexpr are shorthand:
sexpr means symbolic expression
fexpr means function expression
## DATA STRUCTURES
Next we'll discuss the structures provided in cilisphsrccilisph For a more intuitive understanding of the structures and their names defined here, note that AST is short for Abstract Syntax Tree As such, the structures discussed below are intended to house data in an abstract syntax tree.
### NUMBERS
The NUMTYPE enum and ASTNUMBER struct define everything necessary to house both numeric data types integers and floating point values in a single struct.
c
typedef enum numtype
INTTYPE,
DOUBLETYPE
NUMTYPE;
typedef struct
NUMTYPE type;
double value;
ASTNUMBER;
typedef ASTNUMBER RETVAL;
Note that even when an ASTNUMBERs type is INTTYPE 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 ASTNUMBER are welcome to do so:
c
typedef struct
NUMTYPE type;
union
double floatingpoint;
long integral;
value;
ASTNUMBER;
Each ASTNUMBER will house a member of the NUMTYPE enum denoting the type of number being stored alongside a numeric value.
The last line, which gives the ASTNUMBER struct a second name RETVAL exists exclusively for readability; when we evaluate a part of our syntax tree, the result will always be a number. We will use ASTNUMBERs to house numbers which are part of the syntax tree, and RETVALs to house the results of evaulation RETurned VALues
### 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 cilisphsrccilisph
c
typedef enum functype
NEGFUNC,
ABSFUNC,
ADDFUNC,
TODO complete the enum
CUSTOMFUNC
FUNCTYPE;
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 fexprs the data for said calls will be stored in an ASTFUNCTION:
c
typedef struct
FUNCTYPE func;
struct astnode opList;
ASTFUNCTION;
This struct stores the operator used in the function call a member of the FUNCTYPE enum and the operands for the function a linked list of struct astnodes which we will discuss in the GENERIC NODES#generics section below
Take note of the resolveFunc function
cilispcsrccilispc This function takes as input a function's name in string form and outputs the corresponding FUNCTYPE member. It will be used to assign value to FUNC tokens while lexing.
resolveFunc works because the array of function names funcNames in cilispcsrccilispc lists all functions in the same order as the FUNCTYPE enum. If either of these is edited, the other must also be edited to match. If they are not kept in syncresolveFunc will not work. CUSTOMFUNC should be left as the last element of resolveFunc for the same reason; userdefined 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started