<commit_before>%{
#include "LAG-Util.h"

using namespace std;

const int MAX_STR = 256;

symbol_table st; 

string pipeActive;

bool fetch_var_ST( symbol_table& st, string nameVar, Type* typeVar );
string gen_temp( Type t );
string gen_temp_declaration();
void gen_code_attribution_without_index( Attribute* SS, Attribute& lvalue, const Attribute& rvalue );
void gen_code_bin_ops( Attribute* SS, const Attribute& S1, const Attribute& S2, const Attribute& S3 );
void gen_code_for( Attribute* SS, const Attribute& index, const Attribute& initial, const Attribute& end, const Attribute& cmds );
void gen_code_if( Attribute *SS, const Attribute& expr, const Attribute& cmdsThen );
void gen_code_if_else( Attribute *SS, const Attribute& expr, const Attribute& cmdsThen, const Attribute& else_ifs, const Attribute& cmdsElse );
void gen_code_main( Attribute* SS, const Attribute& cmds );
void gen_code_print( Attribute* SS, const Attribute& cmds, const Attribute& expr );
void gen_var_declaration( Attribute* SS, const Attribute& typeVar, const Attribute& id );
void insert_var_ST( symbol_table& st, string nameVar, Type typeVar );
Type result_type( Type a, string op, Type b );
void err( string msg );

#define YYSTYPE Attribute

int yylex();
int yyparse();
void yyerror(const char*);

%}

%start START

%token _CTE_INT _CTE_CHAR _CTE_DOUBLE _CTE_STRING _ID _CTE_TRUE _CTE_FALSE _X
%token _INT _CHAR _BOOL _DOUBLE _FLOAT _STRING _VOID
%token _GLOBAL _ARRAY _MATRIX
%token _OF_SIZE _BY
%token _REFERENCE _COPY
%token _LOAD _INPUT _OUTPUT
%token _EXECUTE_FUNCTION _WITH
%token _IF _EXECUTE _ELSE _ELSE_IF
%token _WHILE _REPEAT _DO
%token _FOR _FROM _TO _DO_FOR
%token _CASE _CASE_EQUALS _CASE_NOT
%token _INTERVAL_FROM _FILTER _FIRST_N _LAST_N _SORT _FOR_EACH
%token _PRINT _THIS
%token _MOD
%token _GT _LT _ET _DF _GE _LE _OR _AND _NOT
%token _STARTING_UP _END_OF_FILE 
%token _CRESCENT _DECRESCENT
%token _CRITERION _MERGE _SPLIT

%left _OR
%left _AND
%left _NOT
%nonassoc _ET _DF _GT _GE _LT _LE
%left _MOD
%left '+' '-'
%left '*' '/'

%% // END OF INCLUDES //

START : LIST_VAR FUNCTIONS MAIN 
    { cout << "#include <stdio.h>\n"
               "#include <stdlib.h>\n"
               "#include <string.h>\n\n"
            << $1.c << $2.c << $3.c << endl; }
      ;
      
START : LIST_VAR FUNCTIONS { cout << $1.c << "\n\n" << $2.c << "\n\n" << endl; }
      ;

MAIN : _STARTING_UP COMMANDS _END_OF_FILE { gen_code_main( &$$, $2 ); }
     ;

FUNCTIONS : FUNCTION FUNCTIONS 
          | 
          ;

FUNCTION : _LOAD _ID _INPUT ARGUMENTS _OUTPUT TYPE _ID BLOCK 
         | _LOAD _ID _INPUT ARGUMENTS _OUTPUT _VOID BLOCK
     ;

ARGUMENTS : ARGUMENT ',' ARGUMENTS 
      | ARGUMENT
      ;

ARGUMENT : TYPE _COPY _ID     
     | TYPE _REFERENCE _ID  
     | _VOID        
     ;

BLOCK : '{' COMMANDS '}' { $$ = $2; }
      ;

COMMANDS : COMMAND COMMANDS { $$.c = $1.c + "\n" + $2.c; }
         | PIPE ';' COMMANDS { $$.c = $1.c + "\n" + $3.c; }
         | COMMAND_COMMA ';' COMMANDS { $$.c = $1.c + "\n" + $3.c; }
         | {$$.c = "";}
         ;

COMMAND_COMMA : CALL_FUNCTION
        | VAR   
        | ATR     
        | PRINT
        | CMD_DOWHILE
        ;

COMMAND : CMD_IF
        | CMD_WHILE 
        | CMD_FOR
        | CMD_SWITCH
        ;

COMMAND_TO_PIPE : COMMAND
        | COMMAND_COMMA
        ;

PRINT : _PRINT EXPR_PRINT
	  { $$ = $2; }
      ;

EXPR_PRINT : EXPR_PRINT _THIS E
			{ gen_code_print( &$$, $1, $3 ); }
		   | { $$ = Attribute(); }
		   ;

CMD_IF : _IF E _EXECUTE BLOCK 
         { gen_code_if( &$$, $2, $4 ); }
       | _IF E _EXECUTE BLOCK ELSE_IFS _ELSE BLOCK
         { gen_code_if_else( &$$, $2, $4, $5, $7 ); cout << "BLAGH" << '\n'; }
       ;

ELSE_IFS : _ELSE_IF E _EXECUTE BLOCK ELSE_IFS
         | { $$ = Attribute(); }
         ;

CMD_WHILE : _WHILE E _REPEAT BLOCK
          ;

CMD_DOWHILE : _DO BLOCK _WHILE E
            ;

CMD_FOR : _FOR INDEX_FOR _FROM E _TO E _EXECUTE BLOCK
		{ gen_code_for( &$$, $2, $4, $6, $8 ); }
        ;

INDEX_FOR : _ID
		  { insert_var_ST( st, $1.v, Type("<integer>") );}
		  ;

CMD_SWITCH : _CASE _ID SIWTCH_BLOCK 
           ;
<commit_msg>Woops, removed unnecessary debug code<commit_after>%{
#include "LAG-Util.h"

using namespace std;

const int MAX_STR = 256;

symbol_table st; 

string pipeActive;

bool fetch_var_ST( symbol_table& st, string nameVar, Type* typeVar );
string gen_temp( Type t );
string gen_temp_declaration();
void gen_code_attribution_without_index( Attribute* SS, Attribute& lvalue, const Attribute& rvalue );
void gen_code_bin_ops( Attribute* SS, const Attribute& S1, const Attribute& S2, const Attribute& S3 );
void gen_code_for( Attribute* SS, const Attribute& index, const Attribute& initial, const Attribute& end, const Attribute& cmds );
void gen_code_if( Attribute *SS, const Attribute& expr, const Attribute& cmdsThen );
void gen_code_if_else( Attribute *SS, const Attribute& expr, const Attribute& cmdsThen, const Attribute& else_ifs, const Attribute& cmdsElse );
void gen_code_main( Attribute* SS, const Attribute& cmds );
void gen_code_print( Attribute* SS, const Attribute& cmds, const Attribute& expr );
void gen_var_declaration( Attribute* SS, const Attribute& typeVar, const Attribute& id );
void insert_var_ST( symbol_table& st, string nameVar, Type typeVar );
Type result_type( Type a, string op, Type b );
void err( string msg );

#define YYSTYPE Attribute

int yylex();
int yyparse();
void yyerror(const char*);

%}

%start START

%token _CTE_INT _CTE_CHAR _CTE_DOUBLE _CTE_STRING _ID _CTE_TRUE _CTE_FALSE _X
%token _INT _CHAR _BOOL _DOUBLE _FLOAT _STRING _VOID
%token _GLOBAL _ARRAY _MATRIX
%token _OF_SIZE _BY
%token _REFERENCE _COPY
%token _LOAD _INPUT _OUTPUT
%token _EXECUTE_FUNCTION _WITH
%token _IF _EXECUTE _ELSE _ELSE_IF
%token _WHILE _REPEAT _DO
%token _FOR _FROM _TO _DO_FOR
%token _CASE _CASE_EQUALS _CASE_NOT
%token _INTERVAL_FROM _FILTER _FIRST_N _LAST_N _SORT _FOR_EACH
%token _PRINT _THIS
%token _MOD
%token _GT _LT _ET _DF _GE _LE _OR _AND _NOT
%token _STARTING_UP _END_OF_FILE 
%token _CRESCENT _DECRESCENT
%token _CRITERION _MERGE _SPLIT

%left _OR
%left _AND
%left _NOT
%nonassoc _ET _DF _GT _GE _LT _LE
%left _MOD
%left '+' '-'
%left '*' '/'

%% // END OF INCLUDES //

START : LIST_VAR FUNCTIONS MAIN 
    { cout << "#include <stdio.h>\n"
               "#include <stdlib.h>\n"
               "#include <string.h>\n\n"
            << $1.c << $2.c << $3.c << endl; }
      ;
      
START : LIST_VAR FUNCTIONS { cout << $1.c << "\n\n" << $2.c << "\n\n" << endl; }
      ;

MAIN : _STARTING_UP COMMANDS _END_OF_FILE { gen_code_main( &$$, $2 ); }
     ;

FUNCTIONS : FUNCTION FUNCTIONS 
          | 
          ;

FUNCTION : _LOAD _ID _INPUT ARGUMENTS _OUTPUT TYPE _ID BLOCK 
         | _LOAD _ID _INPUT ARGUMENTS _OUTPUT _VOID BLOCK
     ;

ARGUMENTS : ARGUMENT ',' ARGUMENTS 
      | ARGUMENT
      ;

ARGUMENT : TYPE _COPY _ID     
     | TYPE _REFERENCE _ID  
     | _VOID        
     ;

BLOCK : '{' COMMANDS '}' { $$ = $2; }
      ;

COMMANDS : COMMAND COMMANDS { $$.c = $1.c + "\n" + $2.c; }
         | PIPE ';' COMMANDS { $$.c = $1.c + "\n" + $3.c; }
         | COMMAND_COMMA ';' COMMANDS { $$.c = $1.c + "\n" + $3.c; }
         | {$$.c = "";}
         ;

COMMAND_COMMA : CALL_FUNCTION
        | VAR   
        | ATR     
        | PRINT
        | CMD_DOWHILE
        ;

COMMAND : CMD_IF
        | CMD_WHILE 
        | CMD_FOR
        | CMD_SWITCH
        ;

COMMAND_TO_PIPE : COMMAND
        | COMMAND_COMMA
        ;

PRINT : _PRINT EXPR_PRINT
	  { $$ = $2; }
      ;

EXPR_PRINT : EXPR_PRINT _THIS E
			{ gen_code_print( &$$, $1, $3 ); }
		   | { $$ = Attribute(); }
		   ;

CMD_IF : _IF E _EXECUTE BLOCK 
         { gen_code_if( &$$, $2, $4 ); }
       | _IF E _EXECUTE BLOCK ELSE_IFS _ELSE BLOCK
         { gen_code_if_else( &$$, $2, $4, $5, $7 );}
       ;

ELSE_IFS : _ELSE_IF E _EXECUTE BLOCK ELSE_IFS
         | { $$ = Attribute(); }
         ;

CMD_WHILE : _WHILE E _REPEAT BLOCK
          ;

CMD_DOWHILE : _DO BLOCK _WHILE E
            ;

CMD_FOR : _FOR INDEX_FOR _FROM E _TO E _EXECUTE BLOCK
		{ gen_code_for( &$$, $2, $4, $6, $8 ); }
        ;

INDEX_FOR : _ID
		  { insert_var_ST( st, $1.v, Type("<integer>") );}
		  ;

CMD_SWITCH : _CASE _ID SIWTCH_BLOCK 
           ;

{'fraction_non_alphanumeric': 0.11828349136883054, 'fraction_numerical': 0.008023340627279359, 'mean_word_length': 2.8265116279069766, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 11, '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': '2910133', 'n_tokens_mistral': 3193, 'n_tokens_neox': 3064, 'n_words': 859}