Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
3.9 kB
2
Indexable
Never
%{
	#include <stdio.h>
	#include <stdlib.h>
	#include "cgen.h"
	#include <string.h>
	
	extern int yylex(void);
	extern int lineNum;
%}

%union
{
	char* str;
	int int_num;
	double float_num;
}

/** KEYWORDS **/

%token <str> KW_INTEGER;
%token <str> KW_FOR;
%token <str> KW_BREAK;
%token <str> KW_DEF;
%token <str> KW_ENDCOMP;
%token <str> KW_SCALAR;
%token <str> KW_CONST;
%token <str> KW_IN;
%token <str> KW_CONTINUE;
%token <str> KW_NOT;
%token <str> KW_ENDDEF;
%token <str> KW_OF;
%token <str> KW_MAIN;
%token <str> KW_STRING;
%token <str> KW_BOOLEAN;
%token <str> KW_FALSE;
%token <str> KW_TRUE;
%token <str> KW_IF;
%token <str> KW_ELSE;
%token <str> KW_ENDIF;
%token <str> KW_ENDFOR;
%token <str> KW_WHILE;
%token <str> KW_ENDWHILE;
%token <str> KW_AND;
%token <str> KW_OR;
%token <str> KW_RETURN;
%token <str> KW_COMP;

/** DELIMETERS **/

%token <str> DEL_LEFTBLOCK
%token <str> DEL_RIGHTBLOCK
%token <str> DEL_ASSIGN
%token <str> DEL_SEMICOLON
%token <str> DEL_COMMA
%token <str> DEL_RIGHTPAR
%token <str> DEL_LEFTPAR
%token <str> DEL_DOUBLEDOT
%token <str> DEL_DOT

/** OPERATORS **/

%token <str> OP_PLUS ;
%token <str> OP_MINUS;
%token <str> OP_MULT ;
%token <str> OP_BACKSLASH
%token <str> OP_MODULO
%token <str> OP_POWER
%token <str> OP_EQUAL
%token <str> OP_UNEQUAL
%token <str> OP_SMALLER
%token <str> OP_SMALLEREQUAL
%token <str> OP_BIGGER
%token <str> OP_BIGGEREQUAL
%token <str> OP_ASSIGN
%token <str> OP_ASSIGNPLUS
%token <str> OP_ASSIGNMINUS;
%token <str> OP_ASSIGNMULT;
%token <str> OP_ASSIGNBACKSLASH;
%token <str> OP_ASSIGNMODULO;
				
/* Other tokens */
%token <str> STRING
%token <str> IDENTIFIER
%token <int_num> INTEGER
%token <float_num> FLOAT

%left DEL_DOT DEL_LEFTPAR DEL_RIGHTPAR DEL_LEFTBLOCK DEL_RIGHTBLOCK
%right OP_POWER
%left OP_MULT OP_BACKSLASH OP_MODULO
%left OP_PLUS OP_MINUS 
%left OP_SMALLER OP_SMALLEREQUAL OP_BIGGER OP_BIGGEREQUAL 
%left OP_EQUAL OP_UNEQUAL
%right KW_NOT 
%left KW_AND
%left KW_OR
%right OP_ASSIGN OP_ASSIGNPLUS OP_ASSIGNMINUS OP_ASSIGNMULT OP_ASSIGNBACKSLASH OP_ASSIGNMODULO
 
 
%start compiler

%type <str> declaration_list
%type <str> data_type_declaration
%type <str> const_decl
%type <str> constant_declaration
%type <str> num_integer
%type <str> num_float 


%%
/*=========================GRAMMAR RULES==========================*/



compiler:
    declaration_list {
    if (yyerror_count == 0) {
      printf("\n\n");
      puts(c_prologue); 
      printf("/* program*/ \n\n");
      printf("%s\n\n", $1);
    }
  }
;


declaration_list: 
	constant_declaration {$$=template("\n\n %s*/", $1);}
	
	
data_type_declaration:
	KW_INTEGER {$$ = "int";}
	|KW_STRING {$$ = "char*";}
	|KW_SCALAR {$$ = "double";}
	|KW_BOOLEAN {$$ = "int";}
	|IDENTIFIER {$$ = $1;};


num_integer:
	INTEGER {$$ = template("%d", $1);}
	| OP_PLUS INTEGER {$$ = template("+%d", $2);}
	| OP_MINUS INTEGER {$$ = template("-%d", $2);};

	
num_float:
	FLOAT {$$ = template("%f", $1);}
	| OP_PLUS FLOAT {$$ = template("+%f", $2);}
	| OP_MINUS FLOAT {$$ = template("-%f", $2);};

	
/*-----------------------------------------------------------------------------------------*/

constant_declaration:
  %empty{$$=" ";}
  |const_decl {$$ = $1;};
    

const_decl:
	KW_CONST IDENTIFIER OP_ASSIGN num_integer DEL_DOUBLEDOT data_type_declaration DEL_SEMICOLON {$$ = template("const %s %s = %d;", $6, $2, $4);}
	|KW_CONST IDENTIFIER OP_ASSIGN num_float DEL_DOUBLEDOT data_type_declaration DEL_SEMICOLON {$$ = template("const %s %s = %f;", $6, $2, $4);}
	|KW_CONST IDENTIFIER OP_ASSIGN STRING DEL_DOUBLEDOT data_type_declaration DEL_SEMICOLON {$$ = template("const %s %s = %s;", $6, $2, $4);}
;

		
/*-----------------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------------*/



%%


 /* Epilogue */

int main () {
  if ( yyparse() != 0 )
    printf("\nRejected!\n");
}