Untitled
raw download clone
TEXT
views 13
,
size 6365 b
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void yyerror();
extern FILE *yyin;
extern FILE *yyout;                                
extern int yylineno;

%}

%union
{
	float f;
	char *s;
}

%token INT FLOAT RELOP COMPOP HASH FROM AS ELIF ELSE 
%token IF IMPORT PRINT CLASS IN IS DEF FOR OPBR CLBR OPSQ CLSQ 
%token SETDEFAULT COMMENT OPCL CLCL US DQ DOT CO COLLON ID SPACE 
%token NEWLINE TAB INIT SELF RANGE EQUAL LAMBDA DICT ITEMS
 
%left '+''-'
%left '/''*' 

%%

START:      PRG
			| PRG NL START
		    | NL START
	        ;

NL:			NEWLINE 
			| TAB 
			;

PRG:        IMPORTSTMT
            | COMMENTSTMT	
		   	| CLASSDEF
			| SELF_STUFF
			| FUNCDEF
			| OBJECTCREATE
			| FUNCTIONCALL
			| FORSTMT
			| IFSTMT
			| PRINTSTMT 
			| VARDECL
			| LAMBDADEC
			| DICTIONARIESDEC
			| ITEMSMETHOD
			| SETDEFAULTMETHOD
			|
	        ;

IMPORTSTMT: IMPORT ID NL PRG // IMPORT KATI
		    | IMPORT ID AS ID NL PRG // IMPORT KATI AS KATI 
            | FROM ID IMPORT ID NL PRG // FROM KATI IMPORT KATI
		    | FROM ID IMPORT '*' NL PRG // FROM KATI IMPORT EVERYTHING * 
            ;

COMMENTSTMT: COMMENT NL PRG 
	        | COMMENT PRG
			| DQ DQ DQ MULTIPLECOMMENT DQ DQ DQ NL PRG
            ;

STRING:     ID 
            | ID STRING
            | INT STRING
            | INT
            | FLOAT STRING
            | FLOAT
            ;

MULTIPLECOMMENT: STRING MULTIPLECOMMENT
			| NL
			;

CLASSDEF:   CLASS CLASSNAME COLLON CLASSDEF
            | NL TAB CONSTRUCTOR NL PRG // CONSTRUCTOR SIGOURA TAB
			| NL TAB PRG // MPOREI NA EXOYME KATI MESA STHN CLASS (IF STATEMENT OR FUNCTION OR ANYTHING)
			| NL PRG // MPOREI NA MHN EXOUME KATI ALLO STHN CLASS
			;

CLASSNAME:  ID
            ;

CONSTRUCTOR: DEF INIT OPBR PARAMETER CLBR COLLON NL TAB SELF_STUFF  
			;

FUNCDEF:    DEF FNAME OPBR PARAMETER CLBR COLLON FUNCDEF
            | NEWLINE TAB PRG
			|
            ;

FNAME:      ID
            ;

SELF_STUFF: TAB SELF DOT ID EQUAL ID NL 
			;

// FOR SETDEFAULT PURPOSES 
SETDEFAULTPARAMETER: DQ PARAMETER DQ CO SETDEFAULTPARAMETER // dic1.setdefault('color', 'white')
			| DQ PARAMETER DQ
			| PARAMETER CO SETDEFAULTPARAMETER // dic1.setdefault('age', 2)
			| PARAMETER 
			;

PARAMETER:  ID
            | ID CO PARAMETER
            | SELF CO PARAMETER // FOR CONSTRUCTOR PARAMETER
			| FLOAT CO PARAMETER
			| INT CO PARAMETER
			| INT
			| FLOAT 
			| 
            ;

OBJECTCREATE: ID EQUAL CLASSNAME OPBR PARAMETER CLBR NL PRG
			;

FUNCTIONCALL: ID OPBR PARAMETER CLBR NL PRG
			| ID DOT ID OPBR PARAMETER CLBR NL PRG
			;

FORSTMT:	FOR ID IN ID COLLON NEWLINE TAB PRG
			| FOR ID IN RANGE OPBR PARAMETER CLBR COLLON NEWLINE TAB PRG
       		;

IFSTMT:		IF CONDEXP COLLON NEWLINE TAB PRG
            | ELIF CONDEXP COLLON NEWLINE TAB PRG
            | ELSE COLLON NEWLINE TAB PRG
            ;

CONDEXP:    INT RELOP INT
            | INT COMPOP INT
            | ID COMPOP ID
            | ID COMPOP INT
            | ID MATHOP ID
            | ID MATHOP INT
			| ID IN ID
			| INT IN ID
            ;

PRINTSTMT:  PRINT OPBR INT CLBR NL PRG   { printf("\nPRINTING << %f >>\n", $<f>3); }// PRINT VARIABLE/NUMBER
            | PRINT OPBR DQ STRING DQ CLBR NL PRG  { printf("\nPRINTING << %s >>\n", $<s>4); }// PRINT TEXT  
			| PRINT OPBR CONDEXP CLBR NL PRG // PRINT OPERATIONS BETWEEN VARIABLES
			| PRINT OPBR ID ITEMS CLBR NL PRG // PRINTING ID DOT ITEMS()
			| PRINT OPBR FLOAT CLBR NL PRG   { printf("\nPRINTING << %f >>\n", $<f>3); }// PRINT VARIABLE/NUMBER
			| PRINT OPBR ID CLBR NL PRG // PRINT VARIABLE/NUMBER
            ;

MATHOP:     '+'
			| '-'
			| '/'
			| '*'
			;

OPERATIONS:  	INT
			| FLOAT
			| OPERATIONS '+' OPERATIONS           { $<f>$ = $<f>1 + $<f>3; }
			| OPERATIONS '*' OPERATIONS           { $<f>$ = $<f>1 * $<f>3; }
			| OPERATIONS '-' OPERATIONS           { $<f>$ = $<f>1 - $<f>3; }
			| OPERATIONS '/' OPERATIONS           { if ($<f>3 == 0) { printf("\nCannot divide by zero\n"); exit(1);} else $<f>$ = $<f>1 / $<f>3; }
			;

VARDECL:    NUMBERDECL NL PRG 
            | STRINGDECL NL PRG
            ;

STRINGDECL: ID EQUAL STRINGNAME 
            ;

STRINGNAME: DQ STRING DQ
			;

NUMBERDECL: ID EQUAL OPERATIONS { printf("\nVariable %s = %f\n", $<s>1,$<f>3); }
		  	| ID EQUAL OPERATOR
            ;

OPERATOR:   ID
			| ID MATHOP OPERATOR
			| INT
			| FLOAT  
			;

LAMBDADEC: 	ID EQUAL LAMBDAOPERATIONS NL PRG // X = LAMBDAOPERATIONS
			;


// LAMBDA X,Y(ARA PARAMETERS TOULAXISTON 1 ME , ANAMESA) : OPERATOR
LAMBDAOPERATIONS: LAMBDA PARAMETER COLLON OPERATOR 
			;


DICTIONARIESDEC: ID EQUAL OPCL DICTPARAMETER CLCL NL PRG // ONE LINE DICTIONARY
			| ID EQUAL DICT OPBR OPCL DICTPARAMETER CLCL CLBR NL PRG // ONE LINE DICTIONARY USING DICT METHOD
			// MULTIPLE LINES IS PRETTY TRICKY
			| ID EQUAL OPCL NL DICTMULTIPLELINES NL PRG
			;

DICTMULTIPLELINES: DICTPARAMETER NL
			| NL CLCL
			;

DICTPARAMETER:  APOSTROPHESTRING COLLON STRING CO DICTPARAMETER // FOR 'STRING':STRING, ... DICTPARAMETER FOR RECURSION
			| APOSTROPHESTRING COLLON STRING // FOR 'STRING':STRING
			| APOSTROPHESTRING COLLON APOSTROPHESTRING CO DICTPARAMETER // FOR 'STRING':'STRING', ... DICTPARAMETER FOR RECURSION
			| APOSTROPHESTRING COLLON APOSTROPHESTRING // FOR 'STRING':'STRING'
			| STRING COLLON STRING CO DICTPARAMETER // FOR STRING:STRING, ...
			| STRING COLLON APOSTROPHESTRING CO DICTPARAMETER // FOR STRING:'STRING', ...
			| STRING COLLON STRING // FOR STRING:STRING
			| STRING COLLON APOSTROPHESTRING // FOR STRING:'STRING'
			| // FOR EMPTY DICTIONARY
			;

APOSTROPHESTRING: DQ STRING DQ
			;

// VAR = DICTIONARY_NAME . ITEMS()
ITEMSMETHOD: ID EQUAL ID ITEMS NL PRG
			;

SETDEFAULTMETHOD: ID EQUAL ID SETDEFAULT OPBR SETDEFAULTPARAMETER CLBR NL PRG
			;


%%


int main(int argc, char *argv[]){
	
    yyin = fopen(argv[1], "r");

	if(yyin == NULL)
	{
		printf("Error with the file!\n");
		exit(EXIT_FAILURE);
	}

	yyparse();
	
	printf("\nParsing is successfully complete!\n");

    return 0;
		
	
}

void yyerror()
{
	printf("\nSyntax Error in Line: %d\n", yylineno);
	exit(0); // if error found exit the program
}
close fullscreen
Login or Register to edit or fork this paste. It's free.