Commandline Arguments and Opening Files in UC Riverside’s CS152 Compiler Design, Part 2.

The first step in building a new compiler is to grab the input from a text file using command line arguments. If there are no arguments, it prompts the user to provide an input file, and if too many are provided, it displays an error message. Once the file name is retrieved, it’s opened using the file system and then read into memory. If successful, it prints "Success, we opened the file successfully." This sets the stage for future videos on Lexing. πŸš€ #CompilerDesign #RustProgramming

Hello everyone uh when we last left off we created a we created a small R project and we ran that uh we ran a little hello world program as well as we counted from 1 to 10. So right now we’re going to get started on the rust the rust compiler itself so let’s create a new project uh call a new compiler so compil it uh we just create a new project and we created a little hello Pro World program so let’s go into our main file.

Setting Up Command Line Arguments and Parsing File Names πŸ“‚

The first step in creating a compiler is the ability to take in an input file, i.e., an input text file and then processing its contents. We will start by setting up the command-line arguments to get the file name.

Reading Command Line Arguments

We’ll now read the command line arguments from the standard environment.

| Conditional Statement  | Action                             |
| ----------------------- | ---------------------------------- |
| `ug.length equals 0`    | Return "Please provide an input file"       |
| `logs.length > 1`       | Return "Too many command line arguments"    |

From there, we are going to get the file name from the command line and set it equal to the first command line argument.

Sample Command:

$ go run

Demonstrating Command Line Arguments πŸ“

We’ll demonstrate how command-line arguments get passed in as parameters using the Cargo command. We’ll explore different scenarios to showcase using command-line arguments in various combinations.

Scenario 1: Single Argument

If the Cargo command is followed by a single argument, it will be considered the input file path.

Scenario 2: Two Arguments

If we provide two arguments, the first argument is the path, and the second argument is the input file.

Opening a File πŸ“„

We’ll demonstrate how to open a file and read in its contents. We will be using the standard FS library to read the file contents into a string.

Reading File Contents

We’ll use the FS read_to_string function to read in the contents of the file. This function returns a result type, which can be either a good result (Okay) or an error.

| Result Type  | Description                                       |
| ------------ | ------------------------------------------------- |
| `Ok`         | Successfully reads from the file                  |
| `Error`      | Fails to open the file, returns an error message   |

In case of an OK result, the file contents will be printed. However, in an Error case, the error message will be displayed.

Sample Command:

$ cargo run not_a_file.txt

Success in Opening the File πŸŽ‰

After executing the Caro run command, we successfully opened and read the contents of the file, demonstrating the correctness of the process. The file was opened and read into the string as expected.

We can further manipulate this to work with any file, allowing us to open and process different files successfully.

This is all for this video. In the next video, we’ll be going over Lexing.

Key Takeaways:

  • Command-line arguments play a crucial role in providing input parameters to a program.
  • The ability to parse file names and open files is essential in the development of a compiler.
  • Demonstrating various scenarios and file opening processes is vital to understanding compiler design.

About the Author

About the Channel:

Share the Post: