Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ub fn parse ( ts: Vec < &str > ) - > std::rc::Rc { / / TODO: Complete this function / * The lex function

ub fn parse(ts: 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 parse_exp(toks: &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 (+12)
let num = Regex::new(r"^\d+$").unwrap(); // Digits
let ops = Regex::new(r"^(\+|-|\*|\^)$").unwrap(); // Operators
let nexttok = toks[0];
match nexttok {
"+"=>{
expect(toks, nexttok); // This should remove the "+" from the front of toks
let arg1= parse_exp(toks); // We recursively parse the first arg of "+"
let arg2= parse_exp(toks); // and the same recursive parse of the second arg of "+"
match nexttok {
"+"=>{
return std::rc::Rc::new(PlusExp {
lhs: arg1,
rhs: arg2,
})
},
}
},
"("=>{
expect(toks, nexttok);
let op = toks[0];
if ops.is_match(op){// The item right after a paren should be an operator
expect(toks, op);
} else {
return std::rc::Rc::new(ErrorExp); // Return an error if not
}
let mut next = peek(toks,0); // 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 next_arg = parse_exp(toks);
args.push(next_arg);
next = peek(toks,0);
}
expect(toks,")");
if args.len()==0{
return std::rc::Rc::new(ErrorExp);
}
match op{
"+"=>{
if args.len()==1{
// Addition allows for unary addition, thus we can use 0 for the left hand side.
//(+1)->(+10)
return std::rc::Rc::new(PlusExp {
lhs: std::rc::Rc::new(LitExp{n: 0}),
rhs: std::rc::Rc::clone(&args[0]),
});
}
// For binary or more arguments, we use the arg 0 as our left hand side number and arg 1 as our right hand arg.
//(+12)-> args[0]=1 and args[1]=2
let mut ast = std::rc::Rc::new(PlusExp {
lhs: std::rc::Rc::clone(&args[0]),
rhs: std::rc::Rc::clone(&args[1]),
});
for arg in &args[2..args.len()]{
ast = std::rc::Rc::new(PlusExp {
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 (+(+12)3) would return the AST you parsed for (+12)
rhs: arg.to_owned(),
});
}
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::new(ErrorExp)
}
let ast = parse_exp(&mut toks);
if peek(&toks, 0)==""{
return ast;
} else {
return std::rc::Rc::new(ErrorExp);
}
}

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

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

What is the difference between Needs and GAP Analyses?

Answered: 1 week ago

Question

What are ERP suites? Are HCMSs part of ERPs?

Answered: 1 week ago