Answered step by step
Verified Expert Solution
Question
1 Approved Answer
ub fn parse ( ts: Vec < &str > ) - > std::rc::Rc { / / TODO: Complete this function / * The lex function
ub fn parsets: Vec&str std::rc::Rc
TODO: Complete this function
The lex function is responsible for breaking down the input expression into tokens. This is the first step of parsing where you identify the individual components of the expression
The parse function is where you convert the tokenized input into an abstract syntax tree AST Think about how you can recursively build the tree by combining expressions based on the tokens
let mut toks ts;
pub fn parseexptoks: &mut Vec&str std::rc::Rc
This function should recursively parse an expression based on the tokens
Consider how each type of expression PlusExp MinusExp, etc. should be parsed differently
Consider the following example to parse
let num Regex::newrd$unwrap; Digits
let ops Regex::newr$unwrap; Operators
let nexttok toks;
match nexttok
expecttoks nexttok; This should remove the from the front of toks
let arg parseexptoks; We recursively parse the first arg of
let arg parseexptoks; and the same recursive parse of the second arg of
match nexttok
return std::rc::Rc::newPlusExp
lhs: arg
rhs: arg
expecttoks nexttok;
let op toks;
if ops.ismatchop The item right after a paren should be an operator
expecttoks op;
else
return std::rc::Rc::newErrorExp; Return an error if not
let mut next peektoks; This will not remove the item at the front of toks
let mut args: Vec vec!; A vector to hold args within the parens
while next
Add the args until we see a right hand paren
let nextarg parseexptoks;
args.pushnextarg;
next peektoks;
expecttoks;
if args.len
return std::rc::Rc::newErrorExp;
match op
if args.len
Addition allows for unary addition, thus we can use for the left hand side.
return std::rc::Rc::newPlusExp
lhs: std::rc::Rc::newLitExpn:
rhs: std::rc::Rc::clone&args
;
For binary or more arguments, we use the arg as our left hand side number and arg as our right hand arg.
args and args
let mut ast std::rc::Rc::newPlusExp
lhs: std::rc::Rc::clone&args
rhs: std::rc::Rc::clone&args
;
for arg in &argsargs.len
ast std::rc::Rc::newPlusExp
lhs: ast, Since we only allow for binary ASTs, we only allow for binary additions, thus our left hand side would be our previously calculated addition. In this case would return the AST you parsed for
rhs: arg.toowned
;
return ast;
TODO: complete this match case
Consider the possibility that you don't match on an op such as above and you don't see an open paren
std::rc::Rc::newErrorExp
let ast parseexp&mut toks;
if peek&toks,
return ast;
else
return std::rc::Rc::newErrorExp;
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