Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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,

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

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

// when appropriate).

impl Scanner{

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

Scanner{

char_vec: input.clone(),

index: 0,

}

}

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

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 = 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 = 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:

-------------------------------- Punctuators -------------------------------- + -> Plus - -> Minus * -> Star / -> Slash = -> Equal ( -> LeftParen ) -> RightParen ; -> SemiColon , -> Comma { -> LeftBrace } -> RightBrace < -> Less ! -> Bang == -> EqualEqual -------------------------------- Tokens that store values -------------------------------- "abc" -> StringLiteral("abc") 123.4 -> NumberLiteral(123.4) some_var -> Identifier("some_var") -------------------------------- Reserved keywords -------------------------------- print -> Print var -> Var con -> Con if -> If else -> Else while -> While and -> And or -> Or 

Input_file1:

hello+there-how*are/you 

Output for the Input_file1:

Identifier("hello") Plus Identifier("there") Minus Identifier("how") Star Identifier("are") Slash Identifier("you") 

Input_file2:

print "Hello, world!"; var a = 123; con b = "onetwothree"; var c = 234; var d_123 = "d_123"; if a < c { print "OK"; } else { print "Not OK"; } while a < c { print "Still OK"; a = a + 1; } "@ny ch@racter$ @re g@me in a string !@#$%^&*()-" 

Output for the input_file2:

Print StringLiteral("Hello, world!") SemiColon Var Identifier("a") Equal NumberLiteral(123) SemiColon Con Identifier("b") Equal StringLiteral("onetwothree") SemiColon Var Identifier("c") Equal NumberLiteral(234) SemiColon Var Identifier("d_123") Equal StringLiteral("d_123") SemiColon If Identifier("a") Less Identifier("c") LeftBrace Print StringLiteral("OK") SemiColon RightBrace Else LeftBrace Print StringLiteral("Not OK") SemiColon RightBrace While Identifier("a") Less Identifier("c") LeftBrace Print StringLiteral("Still OK") SemiColon Identifier("a") Equal Identifier("a") Plus NumberLiteral(1) SemiColon RightBrace StringLiteral("@ny ch@racter$ @re g@me in a string !@#$%^&*()-") 

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 Management With Website Development Applications

Authors: Greg Riccardi

1st Edition

0201743876, 978-0201743876

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago