Created
November 3, 2017 02:19
-
-
Save anonymous/4be8375eca2f1675c4897fed027e25b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Program 3, program3.y | |
Purpose: syntax analyzer (parser) file | |
@author Batyr Nuryyev | |
@date 10/27/2017 | |
*/ | |
%{ | |
#include <iostream> | |
#include <vector> | |
#include <typeinfo> | |
#include <FlexLexer.h> | |
#include "program3.hpp" | |
using namespace std; | |
/* | |
* These are declared in "main" so that we can pass values between | |
* the two portions of the program. | |
*/ | |
extern Node *tree; | |
extern yyFlexLexer scanner; | |
extern int yylineno; | |
/* | |
* Need to do this define, an "acceptable" hack to interface | |
* the C++ scanner with the C parser. | |
*/ | |
#define yylex() scanner.yylex() | |
// need the function prototype for the parser. | |
void yyerror(const char *, int, int); | |
void yyerror(const char *); | |
%} | |
/* | |
* Bison Declarations | |
* | |
*/ | |
%locations | |
%verbose | |
%error-verbose | |
%define parse.trace | |
%start start | |
%code { | |
#define YYLTYPE YYLTYPE | |
} | |
%union { | |
Node *ttype; | |
} | |
%token <ttype> ID NUMBER | |
%token NLL THIS VOID NEW READ PRINT | |
%token CLASS IF ELSE WHILE RETURN | |
%token INT | |
%type <ttype> program | |
%type <ttype> class_decl | |
%type <ttype> class_body | |
%type <ttype> simple_type | |
%type <ttype> type | |
%type <ttype> constructor_decl method_decl variable var_decl local_var_decl | |
%type <ttype> constructor_declmore method_declmore var_declmore local_var_declmore | |
%type <ttype> result_type | |
%type <ttype> mult_parameters | |
%type <ttype> parameter_list | |
%type <ttype> parameter | |
%type <ttype> block | |
%type <ttype> name | |
%type <ttype> empty_exp_in_brackets | |
%type <ttype> exprsn_in_brackets | |
%type <ttype> new_expression | |
%type <ttype> expression | |
%left LP RP LC LS RC RS DOT COMMA SEMICOLON | |
%left EQUALS NOTEQL LESSGRT MOREGRT | |
%nonassoc GREATER LESS | |
%left PLUS MINUS OROR /* shift-reduce errors are solved by this */ | |
%left MULT DIV REMAINDER ANDAND /* shift-reduce errors are solved by this */ | |
%precedence <string> NOT /* exponentiation */ | |
%% | |
start: | |
%empty | |
| program { | |
tree = $1; | |
} | |
; | |
program: | |
class_decl { | |
$$ = new Node; | |
$$->pushNonTerminal($1); | |
$$->setValProd("<Program> --> <ClassDeclaration>+"); | |
} | |
| program class_decl { | |
$$ = $1; | |
$$->pushNonTerminal($2); | |
} | |
class_decl: | |
CLASS ID class_body { | |
$$ = new Node; | |
$$->pushNonTerminal($3); | |
$$->setValProd("<ClassDeclaration> --> class identifier <ClassBody>"); | |
} | |
; | |
/* FIXME: Gotta add more grammar here */ | |
class_body: LC var_declmore constructor_declmore method_declmore RC { | |
$$ = new Node; | |
$$->pushNonTerminal($2); | |
$$->pushNonTerminal($3); | |
$$->pushNonTerminal($4); | |
$$->setValProd("<ClassBody> --> { <VarDeclaration>* <ConstructorDeclaration>* <MethodDeclaration>* }"); | |
cout << "1" << endl; | |
} | |
; | |
var_declmore: %empty { $$ = new Node; } | |
| var_declmore var_decl { | |
$$ = $1; | |
$$->pushNonTerminal($2); | |
cout << "blyat" << endl; | |
} | |
; | |
constructor_declmore: %empty { $$ = new Node; } | |
| constructor_declmore constructor_decl { | |
$$ = $1; | |
$$->pushNonTerminal($2); | |
cout << "DECLARATION OF CONST" << endl; | |
} | |
; | |
var_decl: variable SEMICOLON { | |
$$ = new Node; | |
$$->pushNonTerminal($1); | |
$$->setValProd("<VarDec> --> <Type> identifier ;"); | |
cout << "var_decl" << endl; | |
} | |
| type ID error { | |
yyerrok; | |
yyerror("Invalid character! Expected semicolon ...", | |
@3.first_line, | |
@3.first_column); | |
yyclearin; | |
} | |
; | |
variable: type ID { | |
$$ = new Node; | |
$$->pushNonTerminal($1); | |
} | |
; | |
constructor_decl: ID LP parameter_list RP block { | |
$$ = new Node; | |
$$->setValProd("<ConstructorDeclaration> --> identifier ( <ParameterList> ) <Block>"); | |
$$->pushNonTerminal($1); | |
$$->pushNonTerminal($3); | |
$$->pushNonTerminal($5); | |
} | |
| ID error parameter_list RP block { | |
yyerrok; | |
yyerror("Invalid character! Expected semicolon ...", | |
@2.first_line, | |
@2.first_column); | |
yyclearin; | |
} | |
; | |
result_type: | |
type { | |
$$ = new Node; | |
$$->setValProd("<ResultType> --> <Type>"); | |
$$->pushNonTerminal($1); | |
} | |
| VOID { | |
$$ = new Node; | |
$$->setValProd("<ResultType> --> void"); | |
} | |
; | |
parameter_list: | |
%empty { $$ = new Node; $$->setValProd("<ParameterList> --> epsilon"); } | |
| parameter mult_parameters { | |
$$ = new Node; | |
$$->setValProd("<ParameterList> --> <Parameter> <, <Parameter> >*"); | |
$$->pushNonTerminal($1); | |
$$->pushNonTerminal($2); | |
} | |
; | |
mult_parameters: { $$ = new Node; } | |
| mult_parameters COMMA parameter { | |
$$ = $1; | |
$$->pushNonTerminal($3); | |
} | |
; | |
parameter: | |
type ID { | |
$$ = new Node; | |
$$->setValProd("<Parameter> --> <Type> identifier"); | |
$$->pushNonTerminal($1); | |
cout << "param" << endl; | |
} | |
; | |
type: | |
simple_type { | |
$$ = new Node; | |
$$->setValProd("<Type> --> <SimpleType>"); | |
$$->pushNonTerminal($1); | |
cout << "simple_type" << endl; | |
} | |
| type LS RS { | |
$$ = new Node; | |
$$->pushNonTerminal($1); | |
$$->setValProd("<Type> --> <Type> [ ]"); | |
} | |
; | |
/* FIXME: figure out a way to put ID here ... */ | |
simple_type: | |
INT { | |
$$ = new Node; | |
$$->setValKeyw("int"); | |
$$->setValProd("<SimpleType> --> int"); | |
} | |
| ID { | |
$$ = new Node; | |
$$->setValProd("<SimpleType> --> identifier"); | |
} | |
; | |
local_var_declmore: | |
%empty { $$ = new Node; } | |
| local_var_declmore local_var_decl { | |
$$ = $1; | |
$$->pushNonTerminal($2); | |
cout << "more" << endl; | |
} | |
; | |
local_var_decl: | |
type ID SEMICOLON { | |
$$ = new Node; | |
$$->pushNonTerminal($1); | |
$$->setValProd("<LocalVarDeclaration> --> <Type> identifier ;"); | |
cout << "local_var_decl" << endl; | |
} | |
| type ID error { | |
yyerrok; | |
yyerror("Invalid character! Expected semicolon ...", | |
@3.first_line, | |
@3.first_column); | |
yyclearin; | |
} | |
; | |
block: | |
LC local_var_declmore RC { | |
$$ = new Node; | |
$$->setValProd("<Block> --> { <LocalVarDeclaration>* <Statement>* }"); | |
$$->pushNonTerminal($2); | |
} | |
; | |
method_declmore: | |
%empty { } | |
| method_decl { $$ = new Node; $$->pushNonTerminal($1); } | |
| method_decl method_declmore { | |
$$ = $2; | |
$$->pushNonTerminal($1); | |
} | |
; | |
method_decl: | |
result_type ID LP parameter_list RP block { | |
$$ = new Node; | |
$$->setValProd("<MethodDeclaration> --> <ResultType> identifier ( <ParameterList> ) <Block>"); | |
$$->pushNonTerminal($1); | |
cout << "BLYAT" << endl; | |
$$->pushNonTerminal($2); | |
$$->pushNonTerminal($4); | |
$$->pushNonTerminal($6); | |
} | |
; | |
%% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment