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 parenthesis. For example, ( add 1 2 )

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 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
LEXING
First, it is necessary to define all tokens and all non-terminals within the grammar. tokens (and non-terminals, called types by yacc), will be defined in cilisp.y in the definitions section. The provided token and type definitions are:
%union {
double dval;
int ival;
struct ast_node *astNode;
};
%token FUNC
%token INT
%token QUIT EOL EOFT
%type s_expr
Clearly, this is incomplete; you should know from the previous labs how to complete this definitions section, and we will not discuss it further here (but as always, questions are welcome).
The union contains all types that tokens and types will have. In this case, token and type values will all be stored as double, int or ast_node *.
The tokens defined by the yacc file must be lexed. As we know, the lex file is used to configure a lexer.
The provided cilisp.l is barely started. It has rules to tokenize and return some tokens, but not all of them. You must complete it.
Pay attention to the llog calls made for debugging purposes each time a token is created. Just like in the lab, these calls print to a log file src/bison-flex-outputs/flex_bison_log. Every rule in cilisp.l should include an llog call.
PARSING
The goal of the parser is to construct an abstract syntax tree after tokenization. Most of the productions in your grammar will have an equivalent production in cilisp.y, which is the configuration file for the parser.
The first set of productions in cilisp.y are for parsing programs, which serve as an entry point. The productions are provided, and should be changed cautiously if at all. Productions for s_expr ::= error and s_expr ::= QUIT are also provided.
program:
s_expr EOL {
ylog(program, s_expr EOL);
if ($1){
printRetVal(eval($1));
freeNode($1);
}
YYACCEPT;
}
| s_expr EOFT {
ylog(program, s_expr EOFT);
if ($1){
printRetVal(eval($1));
freeNode($1);
}
exit(EXIT_SUCCESS);
}
| EOL {
ylog(program, EOL);
YYACCEPT; // paranoic; main skips blank lines
}
| EOFT {
ylog(program, EOFT);
exit(EXIT_SUCCESS);
};
s_expr:
QUIT {
ylog(s_expr, QUIT);
exit(EXIT_SUCCESS);
}
| error {
ylog(s_expr, error);
yyerror("unexpected token");
$$ = NULL;
};
Note that every production has a ylog call; this function, like llog, prints to the log file stating what lexing and parsing steps are taken. Every production that you add should include a ylog file, as well, as the log will be a crucial tool in debugging the lexer and parser.
The rest of the yacc file is up to you.
The figure below (which can be found here) may help you visualize what needs to be done. Note that some reductions in the grammar are not illustrated in the figure.
figure_t1
You are strongly encouraged to illustrate prouductions like this as you go through the project, and to occasionally draw out entire syntax trees for test inputs.
Many of these productions will need to call the functions declared at the bottom of cilisp.h:
AST_NODE *createNumberNode(double value, NUM_TYPE type);
AST_NODE *createFunctionNode(FUNC_TYPE func, AST_NODE *opList);
AST_NODE *addExpressionToList(AST_NODE *newExpr, AST_NODE *exprList);
These functions are defined in cilisp.c; some of their definitions are partially completed already.
Whenever a function or value needs to be accessible by the lexer or parser, it mus
image text in transcribed

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

7.9 Determine how the final hiring decision is made.

Answered: 1 week ago