<commit_before>%{#include <memory>%}

%token	<string_val> WORD
%token  <string_val> BACKTIK

%token 	NOTOKEN GREAT NEWLINE GTFO LESS TOOGREAT TOOGREATAND PIPE AMPERSAND
%token GREATAND TAB SRC ANDAND ALIAS GETS 
%union	{
    char * string_val;
}

%{
/* STL (C++) Includes */
#include <algorithm>
#include <iostream>
#include <cstring>

/* C Includes */
#include <dirent.h>
#include <stdio.h>
#include <fcntl.h>
#include <regex.h>

/* File Includes */
#include "shell-readline.hpp"
#include "shell-utils.hpp"
#include "wildcard.hpp"

int yylex();  
int yyparse();

void yyerror(const char * s);
%}

%%
 /**
  * Lex: 
  * <reg. exp> <whitespace> <action>
  *
  * <reg. exp>: beginning of line up to first non-escaped white space.
  *
  * <action>: A single C statement. Multiple statements are wrapped in braces.
  */
goal:	
commands
;

commands: 
commands command 
| command
;

command:
simple_command
|
;

simple_command:	
pipe_list iomodifier_list background_optional NEWLINE {
    Command::currentCommand.execute();
}
| SRC WORD { reader.setFile(std::string($2)); }
| ALIAS WORD WORD {
	char * alias, * word, * equals;
	if (!(equals = strchr($2, '='))) {
		std::cerr<<"Invalid syntax: alias needs to be set!"<<std::endl;
	} else {	
		alias = strndup($2, strlen($2) - 1);
		/* word = WORD + length before '=' + 1 (for '='). */
		word  = strdup($3);

		Command::currentCommand.setAlias(alias, word);

		free(alias); free(word);
	}
}
| NEWLINE { Command::currentCommand.prompt(); }
| error NEWLINE { yyerrok; std::cout<<std::endl; Command::currentCommand.prompt(); }
;

command_and_args:
command_word argument_list {
    Command::currentCommand.insertSimpleCommand(Command::currentSimpleCommand);
}
;

argument_list:
argument_list argument
| /* can be empty */
;

argument:
WORD {
	std::string temp = tilde_expand(std::string($1));
	
	char * expand_upon_me = strndup(temp.c_str(), temp.size());
    wildcard_expand(expand_upon_me); free(expand_upon_me);
    std::string * array = Command::currentCommand.wc_collector.data();
    std::sort(array, array + Command::currentCommand.wc_collector.size(),
	      Comparator());

    for (auto && arg : Command::currentCommand.wc_collector) {<commit_msg>Fixed the sorting order in the bison file.<commit_after>%{#include <memory>%}

%token	<string_val> WORD
%token  <string_val> BACKTIK

%token 	NOTOKEN GREAT NEWLINE GTFO LESS TOOGREAT TOOGREATAND PIPE AMPERSAND
%token GREATAND TAB SRC ANDAND ALIAS GETS 
%union	{
    char * string_val;
}

%{
/* STL (C++) Includes */
#include <algorithm>
#include <iostream>
#include <cstring>

/* C Includes */
#include <dirent.h>
#include <stdio.h>
#include <fcntl.h>
#include <regex.h>

/* File Includes */
#include "shell-readline.hpp"
#include "shell-utils.hpp"
#include "wildcard.hpp"

int yylex();  
int yyparse();

void yyerror(const char * s);
%}

%%
 /**
  * Lex: 
  * <reg. exp> <whitespace> <action>
  *
  * <reg. exp>: beginning of line up to first non-escaped white space.
  *
  * <action>: A single C statement. Multiple statements are wrapped in braces.
  */
goal:	
commands
;

commands: 
commands command 
| command
;

command:
simple_command
|
;

simple_command:	
pipe_list iomodifier_list background_optional NEWLINE {
    Command::currentCommand.execute();
}
| SRC WORD { reader.setFile(std::string($2)); }
| ALIAS WORD WORD {
	char * alias, * word, * equals;
	if (!(equals = strchr($2, '='))) {
		std::cerr<<"Invalid syntax: alias needs to be set!"<<std::endl;
	} else {	
		alias = strndup($2, strlen($2) - 1);
		/* word = WORD + length before '=' + 1 (for '='). */
		word  = strdup($3);

		Command::currentCommand.setAlias(alias, word);

		free(alias); free(word);
	}
}
| NEWLINE { Command::currentCommand.prompt(); }
| error NEWLINE { yyerrok; std::cout<<std::endl; Command::currentCommand.prompt(); }
;

command_and_args:
command_word argument_list {
    Command::currentCommand.insertSimpleCommand(Command::currentSimpleCommand);
}
;

argument_list:
argument_list argument
| /* can be empty */
;

argument:
WORD {
	std::string temp = tilde_expand(std::string($1));
	
	char * expand_upon_me = strndup(temp.c_str(), temp.size());
    wildcard_expand(expand_upon_me); free(expand_upon_me);
    std::string * array = Command::currentCommand.wc_collector.data();

    for (auto && arg : Command::currentCommand.wc_collector) {
{'fraction_non_alphanumeric': 0.15799213509137172, 'fraction_numerical': 0.0037011334721258385, 'mean_word_length': 4.50828025477707, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1830982', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1547, 'n_words': 363}