Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is rust language. Here is my code. use std::env; // Included to access cmd

ID: 3588140 • Letter: T

Question

This is rust language.

Here is my code.

use std::env; // Included to access cmd line arguments within the program.

use std::fs::File; // Gives access to files. Open/Read/Write.

use std::io::prelude::*;// For common I/O tasks.

#[derive(Debug)]

enum Token {

Plus,

Minus,

Star,

Slash,

Equal,

LeftParen,

RightParen,

SemiColon,

Comma

}

// Add two fields to the following struct. One for holding

// a vector of characters, and another that is an index (usize)// into the vector.

#[derive(Debug)]

struct Scanner{

char_vec: Vec<char>,

index: usize,

}

// Implement the following methods for the Scanner struct:

// (1) A "new" method that creates a scanner from a String

// (2) A "get_next_token" method that returns an Option<Token>

// representing the next symbol in the vector (or None

// when appropriate).

impl Scanner{

pub fn new (input: &Vec<char>) -> Scanner {

Scanner{

char_vec: input.clone(),

index: 0,

}

}

pub fn get_next_token(&mut self) -> Option<Token>{

if self.index >= self.char_vec.len() {

return None;

}

while self.index < self.char_vec.len() {

let c = self.char_vec[self.index];

self.index += 1;

let v = match c {

'+' => Some(Token::Plus),

'-' => Some(Token::Minus),

'*' => Some(Token::Star),

'/' => Some(Token::Slash),

'=' => Some(Token::Equal),

'(' => Some(Token::LeftParen),

')' => Some(Token::RightParen),

';' => Some(Token::SemiColon),

',' => Some(Token::Comma),

chr => {

if chr.is_whitespace() {

continue

}

else {

return None

}

}

};

return v

}

return None

}

}

// The main function should do the following:

// (1) Read the contents of a file into a String. The name of

// the file is given as a program argument.

// (2) Create a scanner object with the String.

// (3) Use a "while let" loop to print all Tokens found in the

// String.

fn main() {

let args: Vec<String> = env::args().collect(); // Collects cmd arguments into a string Vector.

let file_name = arg_parse(&args); // Receives values from fn arg_parse().

// Opens file whose name is passed via cmd line.

let mut inputfile = File::open(file_name).expect("File Not Found.");

// Creates a new mutable string variable to hold the contents of file.

let mut contents = String::new();

// Passes reference to contents, file is read.

inputfile.read_to_string(&mut contents).expect("Something went wrong reading the file.");

// Creates a char Vector to hold the contents.

let char_vec: Vec<char> = contents.chars().collect();

let mut scanner = Scanner::new(&char_vec);

// Loop to print out all the tokens

while let Some(value) = scanner.get_next_token() {

println!("{:?}", value);

}

}

// Function for parsing cmd line arguments.

fn arg_parse(args: &[String]) -> (&str) {

let file_name = &args[1]; // Assigns the first cmd argument to a variable.

(file_name) // Variables passed back to Main().

}

The goal of my program is again to print out all of the tokens in a given file. My program will take one command line argument, and can be compiled and run with the command.

My program need to read the file and print all of the tokens that it finds. Specifically, my program should be able to detect the following tokens:

Input_file1:

Output for the Input_file1:

Input_file2:

Output for the input_file2:

Explanation / Answer

use std::env; // Included to access cmd line arguments within the program.

use std::fs::File; // Gives access to files. Open/Read/Write.

use std::io::prelude::*;// For common I/O tasks.

#[derive(Debug)]

enum Token {

Plus,

Minus,

Star,

Slash,

Equal,

LeftParen,

RightParen,

SemiColon,

Comma

}

// Add two fields to the following struct. One for holding

// a vector of characters, and another that is an index (usize)// into the vector.

#[derive(Debug)]

struct Scanner{

char_vec: Vec<char>,

index: usize,

}

// Implement the following methods for the Scanner struct:

// (1) A "new" method that creates a scanner from a String

// (2) A "get_next_token" method that returns an Option<Token>

// representing the next symbol in the vector (or None

// when appropriate).

impl Scanner{

pub fn new (input: &Vec<char>) -> Scanner {

Scanner{

char_vec: input.clone(),

index: 0,

}

}

pub fn get_next_token(&mut self) -> Option<Token>{

if self.index >= self.char_vec.len() {

return None;

}

while self.index < self.char_vec.len() {

let c = self.char_vec[self.index];

self.index += 1;

let v = match c {

'+' => Some(Token::Plus),

'-' => Some(Token::Minus),

'*' => Some(Token::Star),

'/' => Some(Token::Slash),

'=' => Some(Token::Equal),

'(' => Some(Token::LeftParen),

')' => Some(Token::RightParen),

';' => Some(Token::SemiColon),

',' => Some(Token::Comma),

chr => {

if chr.is_whitespace() {

continue

}

else {

return None

}

}

};

return v

}

return None

}

}

// The main function should do the following:

// (1) Read the contents of a file into a String. The name of

// the file is given as a program argument.

// (2) Create a scanner object with the String.

// (3) Use a "while let" loop to print all Tokens found in the

// String.

fn main() {

let args: Vec<String> = env::args().collect(); // Collects cmd arguments into a string Vector.

let file_name = arg_parse(&args); // Receives values from fn arg_parse().

// Opens file whose name is passed via cmd line.

let mut inputfile = File::open(file_name).expect("File Not Found.");

// Creates a new mutable string variable to hold the contents of file.

let mut contents = String::new();

inputfile.read_to_string(&mut contents).expect("Something went wrong reading the file.");

let char_vec: Vec<char> = contents.chars().collect();

let mut scanner = Scanner::new(&char_vec);

while let Some(value) = scanner.get_next_token() {

println!("{:?}", value);

}

}

fn arg_parse(args: &[String]) -> (&str) {

let file_name = &args[1];

(file_name) // Variables passed back to Main().

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote