Question
This is rust language.
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; use std::fs::File; use std::io::Read; #[derive(Debug)] enum Token { // Punctuators Plus, Minus, Star, Slash, Equal, LeftParen, RightParen, SemiColon, Comma, LeftBrace, RightBrace, Less, Bang, EqualEqual, // Tokens that store values StringLiteral(String), NumberLiteral(f32), Identifier(String), //Reserved keywords Print, Var, Con, If, Else, While, And, Or, } #[derive(Debug)] struct Scanner { chars: Vec, index: usize, } impl Scanner { fn new(s: String) -> Scanner { Scanner { chars: s.chars().collect(), index: 0, } } fn get_next_token(&mut self) -> Option { while self.index Some(Token::Minus), '*' => Some(Token::Star), '/' => Some(Token::Slash), '(' => Some(Token::LeftParen), ')' => Some(Token::RightParen), ';' => Some(Token::SemiColon), ',' => Some(Token::Comma), '{' => Some(Token::LeftBrace), '}' => Some(Token::RightBrace), '