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 parenthesis. For example, ( add 1 2 )
Recall Cambridge Polish Notation CPN 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 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
LEXING
First, it is necessary to define all tokens and all nonterminals within the grammar. tokens and nonterminals, 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 astnode astNode;
;
token FUNC
token INT
token QUIT EOL EOFT
type sexpr
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 astnode
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 srcbisonflexoutputsflexbisonlog 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 sexpr :: error and sexpr :: QUIT are also provided.
program:
sexpr EOL
ylogprogram sexpr EOL;
if $
printRetValeval$;
freeNode$;
YYACCEPT;
sexpr EOFT
ylogprogram sexpr EOFT;
if $
printRetValeval$;
freeNode$;
exitEXITSUCCESS;
EOL
ylogprogram EOL;
YYACCEPT; paranoic; main skips blank lines
EOFT
ylogprogram EOFT;
exitEXITSUCCESS;
;
sexpr:
QUIT
ylogsexpr, QUIT;
exitEXITSUCCESS;
error
ylogsexpr, error;
yyerrorunexpected 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.
figuret
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:
ASTNODE createNumberNodedouble value, NUMTYPE type;
ASTNODE createFunctionNodeFUNCTYPE func, ASTNODE opList;
ASTNODE addExpressionToListASTNODE newExpr ASTNODE 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
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