Skip to content

geeknerd/osh-parser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Alternative Parser for osh

PA1 comes with ugly broken parser. This is an alternative implementation you can use.

Just mention you've used this library by Yutaka Tsutano instead in your README.txt.

1 Usage

Read main.cpp (usage) and command.hpp (data structure).

1.1 Parsing

parse_command_string() returns a vector of parsed commands (defined in command.hpp) from a line of text input from the terminal.

// 1. Get a line from the terminal.
std::string input_line = "echo hello world | sort | uniq > test.txt";

// 2. Parse.
std::vector<shell_command> shell_commands = parse_command_string(input_line);

This is basically all you need to do for parsing. You just need to execute the commands listed in shell_commands vector to implement the assignment.

1.2 Printing the Parsed Commands

It is helpful to print the parsed commands when implementing the execution part of the shell. You can do so with the following code:

std::cout << "-------------------------\n";
for (const auto& cmd : shell_commands) {
    std::cout << cmd;
    std::cout << "-------------------------\n";
}

The result of parsing echo hello world | sort | uniq > test.txt:

-------------------------
cmd: echo
arg: hello
arg: world
cin_file:
cin_mode: term
cout_file:
cout_mode: pipe
next_mode: any
-------------------------
cmd: sort
cin_file:
cin_mode: pipe
cout_file:
cout_mode: pipe
next_mode: any
-------------------------
cmd: uniq
cin_file:
cin_mode: pipe
cout_file: test.txt
cout_mode: file
next_mode: any
-------------------------

About

Alternative Parser for osh

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 97.8%
  • Makefile 2.2%