<commit_before>/* 		OBJECT-ORIENTED RECOGNIZER FOR SIMPLE EXPRESSIONS
 	  program ->  decls stmts end
      decls   ->  int idlist ';'
      idlist  ->  id [',' idlist ]
      stmts   ->  stmt [ stmts ]
      stmt    ->  assign ';'| cmpd | cond | loop
      assign  ->  id '=' expr
      cmpd    ->  '{' stmts '}'
      cond    ->  if '(' rexp ')' stmt [ else stmt ]
      loop    ->  for '(' [assign] ';' [rexp] ';' [assign] ')' stmt
	  rexp -> expr('<'|'>'|'=='|'!=')expr 
	  expr -> term [ ('+' | '-') expr ]
	  term -> factor [ ('*' | '/') term ]
	  factor -> int_lit | id | '(' expr ')'
*/

import java.lang.System;
import java.util.*;
public class Parser {
	public static void main(String[] args) {
		System.out.println("Enter an expression, end with \"end\"!\n");
		Lexer.lex();             //Start Parser
		new Program();          
		Code.output();
	}
}

class Program{
	Decls d;
	Stmts s;
	public Program() {
		d = new Decls();         //Declaration
     	s=  new Stmts();         //Statements
	}
}

class Decls{
	IdList i;
	public Decls(){
		if(Lexer.nextToken == Token.KEY_INT)     //If Key_INT next cursor and call IDlist
			Lexer.lex();
		i = new IdList();
	}
}

class IdList {
	IdList id;
	char v;
	public IdList() {
		if(Lexer.nextToken==Token.ID){
			v= Lexer.ident;
			Code.gen(Code.id(v,Lexer.nextToken));          //Map the variable
			Lexer.lex();								
			if(Lexer.nextToken == Token.SEMICOLON) {	  //If semicolon Lex and return
				Lexer.lex();   
				return;
			}
			if(Lexer.nextToken == Token.COMMA) {          //If comma, new IDList again
				Lexer.lex();
				id = new IdList();
			}
		}
	}
}

class Stmts{
	Stmnt s;
	Stmts ss;
	public Stmts(){
		s = new Stmnt();
		if(Lexer.nextToken == Token.KEY_END || Lexer.lex() == Token.KEY_END) { 
			Code.gen(Code.end());						//Generate return code
			return;
		}
		else if( Lexer.nextToken == Token.RIGHT_BRACE ) {     //Right brace when compound statement
			return;
		}
		else
			ss = new Stmts();								//Recursively call Statements until end
	}
}

class Stmnt{
	Assign a;
	Cond c;
	Cmpd cmpd;
	Loop l;
	public Stmnt(){
		switch(Lexer.nextToken) {              //Statement will be either
		case Token.ID:
			a = new Assign();                  // Assign or
			break;
		case Token.KEY_IF:                     // Cond or
			c= new Cond();
			break;
		case Token.KEY_FOR:                    //For or
			l=new Loop();
			break;
		case Token.LEFT_BRACE:                //Cmpnd
			Lexer.lex();
			cmpd = new Cmpd();
		default:
			break;
		}
	}
}

class Cmpd {
	Stmts st;
	public Cmpd() {
		st = new Stmts(); 					//Call Statements again to create Bracket indentation
	}
}

class Assign{ //assign  ->  id '=' expr ';'
	char id;
	Expr e;
	Factor f;
	char c;
	public Assign(){
		if(Lexer.nextToken==Token.ID){
			c = Lexer.ident;
			Lexer.lex();
			if(Lexer.nextToken == Token.ASSIGN_OP){
				Lexer.lex();
				e = new Expr();					//Expression will be called in right side of "=" operator
			}
		}
		Code.gen(Code.id(c,Token.ASSIGN_OP));			//Generate "store" Bytecode
	}
}

class Cond{
	Rexpr r;
	Stmnt s1,s2;
	int ptr1,ptr2;
	public Cond(){
		Lexer.lex();			
		Lexer.lex();		//Move to next Token after LeftBrace
		r=new Rexpr();
		ptr1=Code.getcodeptr();		//Save CodePtr to use later for adding imcmpgne "Bytecode number"
		Lexer.lex();
		s1=new Stmnt();				
		if(Lexer.nextToken != Token.KEY_END || Lexer.lex()!=Token.KEY_ELSE){	//	if else is called then goto "Bytecode number" is generated
			Code.gen(Code.condition(ptr1,true));
			return;
		}
		ptr2=Code.getcodeptr();				//Called to get the end statement for putting back in If compare Bytecode
		Code.gen(Code.condition(ptr1,false)); //number
		Lexer.lex();						
		s2=new Stmnt();						
		Code.gotofunc(ptr2);				//Goto statement will be called after stmts in If are generated
	}	
}

class Loop{
	Assign a1,a2;
	Stmnt s;
	Rexpr r;
	int cmpPtr,spacePtr, tempAssign;
	int resetCode, resetSpace;
	public Loop(){
		Lexer.lex();
		Lexer.lex();                      //Bring Cursor to initialization of for 
		if(Lexer.nextToken != Token.SEMICOLON)
			a1= new Assign();			//Assignment statement called if only semicolon as no code will be generated for ;
		spacePtr = Code.getSpacePtr();	
		Lexer.lex();
		r= new Rexpr();
		cmpPtr=Code.getcodeptr();
		resetSpace = Code.getSpacePtr();		//Get the place where to reset the spacePtr, ie After Imcpgne(Rexpr)
		resetCode  = Code.getcodeptr();			//	Get the arrayplace to reset codeptr
		Lexer.lex();
		if(Lexer.nextToken != Token.RIGHT_PAREN)
			a2=new Assign();
		tempAssign = Code.getSpacePtr();
		String [] assiArray = Code.assArrayWithReset(resetSpace, resetCode);	//Store assignment stmt in array and reset
		Lexer.lex();															// array and space ptrs
		s = new Stmnt();
		if(assiArray.length != 0)
			Code.generateAssign(assiArray);				//Restore the generated array after the statements after stmnt has 
		Code.gen(Code.genGoTo(spacePtr));				//executed and bring space and arrayPtrs up to speed
		if(spacePtr - tempAssign != 0)
			Code.gen(Code.condition(cmpPtr,true));			//Previous statement creates a goto for the for statement
	}													//Previous adds the proper line no to Ifcmpgne "Byte Space No"
}

class Rexpr{ //rexp -> expr('<'|'>'|'=='|'!=')expr
	Expr e1;
	Expr e2;
	char op;
	
	public Rexpr(){
		e1 = new Expr();			
		int token = Lexer.nextToken;
		if ( token == Token.GREATER_OP || token == Token.LESSER_OP || token == Token.EQ_OP || token == Token.NOT_EQ){
			Lexer.lex();
			e2 = new Expr();<commit_msg>Complete<commit_after>/* 		OBJECT-ORIENTED RECOGNIZER FOR SIMPLE EXPRESSIONS
 	  program ->  decls stmts end
      decls   ->  int idlist ';'
      idlist  ->  id [',' idlist ]
      stmts   ->  stmt [ stmts ]
      stmt    ->  assign ';'| cmpd | cond | loop
      assign  ->  id '=' expr
      cmpd    ->  '{' stmts '}'
      cond    ->  if '(' rexp ')' stmt [ else stmt ]
      loop    ->  for '(' [assign] ';' [rexp] ';' [assign] ')' stmt
	  rexp -> expr('<'|'>'|'=='|'!=')expr 
	  expr -> term [ ('+' | '-') expr ]
	  term -> factor [ ('*' | '/') term ]
	  factor -> int_lit | id | '(' expr ')'
*/

import java.lang.System;
import java.util.*;
public class Parser {
	public static void main(String[] args) {
		System.out.println("Enter an expression, end with \"end\"!\n");
		Lexer.lex();             //Start Parser
		new Program();          
		Code.output();
	}
}

class Program{
	Decls d;
	Stmts s;
	public Program() {
		d = new Decls();         //Declaration
     	s=  new Stmts();         //Statements
	}
}

class Decls{
	IdList i;
	public Decls(){
		if(Lexer.nextToken == Token.KEY_INT)     //If Key_INT next cursor and call IDlist
			Lexer.lex();
		i = new IdList();
	}
}

class IdList {
	IdList id;
	char v;
	public IdList() {
		if(Lexer.nextToken==Token.ID){
			v= Lexer.ident;
			Code.gen(Code.id(v,Lexer.nextToken));          //Map the variable
			Lexer.lex();								
			if(Lexer.nextToken == Token.SEMICOLON) {	  //If semicolon Lex and return
				Lexer.lex();   
				return;
			}
			if(Lexer.nextToken == Token.COMMA) {          //If comma, new IDList again
				Lexer.lex();
				id = new IdList();
			}
		}
	}
}

class Stmts{
	Stmt s;
	Stmts ss;
	public Stmts(){
		s = new Stmt();
		if(Lexer.nextToken == Token.KEY_END || Lexer.lex() == Token.KEY_END) { 
			Code.gen(Code.end());						//Generate return code
			return;
		}
		else if( Lexer.nextToken == Token.RIGHT_BRACE ) {     //Right brace when compound statement
			return;
		}
		else
			ss = new Stmts();								//Recursively call Statements until end
	}
}

class Stmt{
	Assign a;
	Cond c;
	Cmpd cmpd;
	Loop l;
	public Stmt(){
		switch(Lexer.nextToken) {              //Statement will be either
		case Token.ID:
			a = new Assign();                  // Assign or
			break;
		case Token.KEY_IF:                     // Cond or
			c= new Cond(); 
			break;
		case Token.KEY_FOR:                    //For or
			l=new Loop();
			break;
		case Token.LEFT_BRACE:                //Cmpnd
			Lexer.lex();
			cmpd = new Cmpd();
		default:
			break;
		}
	}
}

class Cmpd {
	Stmts st;
	public Cmpd() {
		st = new Stmts(); 					//Call Statements again to create Bracket indentation
	}
}

class Assign{ //assign  ->  id '=' expr ';'
	char id;
	Expr e;
	Factor f;
	char c;
	public Assign(){
		if(Lexer.nextToken==Token.ID){
			c = Lexer.ident;
			Lexer.lex();
			if(Lexer.nextToken == Token.ASSIGN_OP){
				Lexer.lex();
				e = new Expr();					//Expression will be called in right side of "=" operator
			}
		}
		Code.gen(Code.id(c,Token.ASSIGN_OP));			//Generate "store" Bytecode
	}
}

class Cond{
	Rexpr r;
	Stmt s1,s2;
	int ptr1,ptr2;
	public Cond(){
		Lexer.lex();			
		Lexer.lex();		//Move to next Token after LeftBrace
		r=new Rexpr();
		ptr1=Code.getcodeptr();		//Save CodePtr to use later for adding imcmpgne "Bytecode number"
		Lexer.lex();
		s1=new Stmt();				
		if(Lexer.nextToken != Token.KEY_END && Lexer.lex()!=Token.KEY_ELSE){	//	if else is called then goto "Bytecode number" is generated
			Code.gen(Code.condition(ptr1,true));
			return;
		}
		ptr2=Code.getcodeptr();				//Called to get the end statement for putting back in If compare Bytecode
		Code.gen(Code.condition(ptr1,false)); //number
		Lexer.lex();						
		s2=new Stmt();						
		Code.gotofunc(ptr2);				//Goto statement will be called after stmts in If are generated
	}	
}

class Loop{
	Assign a1,a2;
	Stmt s;
	Rexpr r;
	int cmpPtr,spacePtr, tempAssign;
	int resetCode, resetSpace;
	public Loop(){
		Lexer.lex();
		Lexer.lex();                      //Bring Cursor to initialization of for 
		if(Lexer.nextToken != Token.SEMICOLON)
			a1= new Assign();			//Assignment statement called if only semicolon as no code will be generated for ;
		spacePtr = Code.getSpacePtr();	
		Lexer.lex();
		r= new Rexpr();
		cmpPtr=Code.getcodeptr();
		resetSpace = Code.getSpacePtr();		//Get the place where to reset the spacePtr, ie After Imcpgne(Rexpr)
		resetCode  = Code.getcodeptr();			//	Get the arrayplace to reset codeptr
		Lexer.lex();
		if(Lexer.nextToken != Token.RIGHT_PAREN)
			a2=new Assign();
		tempAssign = Code.getSpacePtr();
		String [] assiArray = Code.assArrayWithReset(resetSpace, resetCode);	//Store assignment stmt in array and reset
		Lexer.lex();															// array and space ptrs
		s = new Stmt();
		if(assiArray.length != 0)
			Code.generateAssign(assiArray);				//Restore the generated array after the statements after Stmt has 
		Code.gen(Code.genGoTo(spacePtr));				//executed and bring space and arrayPtrs up to speed
		if(spacePtr - tempAssign != 0)
			Code.gen(Code.condition(cmpPtr,true));			//Previous statement creates a goto for the for statement
	}													//Previous adds the proper line no to Ifcmpgne "Byte Space No"
}

class Rexpr{ //rexp -> expr('<'|'>'|'=='|'!=')expr
	Expr e1;
	Expr e2;
	char op;
	
	public Rexpr(){
		e1 = new Expr();			
		int token = Lexer.nextToken;
		if ( token == Token.GREATER_OP || token == Token.LESSER_OP || token == Token.EQ_OP || token == Token.NOT_EQ){
			Lexer.lex();
			e2 = new Expr();
{'fraction_non_alphanumeric': 0.1458750682935713, 'fraction_numerical': 0.0038244399927153524, 'mean_word_length': 2.520192307692308, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 37, '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': '1405494', 'n_tokens_mistral': 4557, 'n_tokens_neox': 3999, 'n_words': 1087}