Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #define DEBUG 0
- typedef struct AST_Tree{
- char *type;
- struct AST_Tree* child;
- struct AST_Tree* brother;
- char *value;
- char *notacao;
- }AST_Tree_node;
- typedef struct Table{
- char *name;
- char *type;
- char *paramsType;
- char *flag;
- struct Table* brother;
- struct Table* child;
- }Table_node;
- Table_node *head_table = NULL;
- AST_Tree_node *head = NULL;
- AST_Tree_node* insertNode(char* node);
- AST_Tree_node* insert_value_node(char* node, char* value);
- void insert_child(AST_Tree_node* father, AST_Tree_node* child);
- void insert_brother(AST_Tree_node* brother, AST_Tree_node* node);
- void printTree(AST_Tree_node* aux, int level);
- void printTerminal(AST_Tree_node *node);
- void printPoints(int n);
- int tolower(int c);
- void printTable(Table_node *node);
- Table_node* insertNode_Table(char* node);
- Table_node* insert_value_node_Table(char* name, char* type, char* paramsType, char* flag);
- void insert_child_Table(Table_node* father, Table_node* child);
- void insert_brother_Table(Table_node* brother, Table_node* node);
- Table_node* find_symbol(char* name);
- Table_node* find_symbol_Method(char *name);
- Table_node *check_later_func(char *name, char *type);
- int parse = 0;
- int flag=0;
- int aux2 = 0;
- int imprimir=0;
- int imprimir3=0;
- int yylex(void);
- void yyerror(const char *s);
- extern int n_linha;
- extern int n_coluna;
- extern char * yytext;
- extern int yyleng;
- Table_node *aux_node_table;
- AST_Tree_node *nodeAux;
- AST_Tree_node *nodeAux2;
- AST_Tree_node *nodeAux3;
- AST_Tree_node *nodeAux4;
- AST_Tree_node *nodeAux5;
- Table_node *nodeAux_Table;
- Table_node *nodeAux_Table2;
- Table_node *nodeAux_Table3;
- Table_node *nodeAux_Table4;
- Table_node *varDecl;
- Table_node *current_Method;
- AST_Tree_node *check_later;
- %}
- %union{
- char *str;
- struct AST_Tree *node_struct;
- }
- %token <str> ID STRLIT BOOLLIT DECLIT REALLIT
- %token PUBLIC BOOL INT DOUBLE SEMI COMMA PARSEINT OCURV OSQUARE CSQUARE CCURV ASSIGN AND OR EQ GEQ GT LEQ LT NEQ PLUS MINUS STAR DIV MOD NOT DOTLENGTH STATIC STRING VOID CLASS OBRACE CBRACE IF ELSE WHILE DO PRINT RETURN RESERVED
- %type <node_struct> Program FieldDecl_MethodDecl_SEMI FieldDecl CommaID MethodDecl MethodHeader FormalParams CommaTypeID MethodBody VarDecl_Statement VarDecl Type Statement Statement_aux Expr ExprNew Assignment MethodInvocation CommaExpr ParseArgs
- %left COMMA
- %right ASSIGN
- %left OR
- %left AND
- %left EQ NEQ
- %left LT GT LEQ GEQ
- %left MINUS PLUS
- %left STAR DIV MOD
- %right NOT
- %left OCURV OBRACE OSQUARE CCURV CBRACE CSQUARE
- %nonassoc ELSE
- %%
- Program: CLASS ID OBRACE FieldDecl_MethodDecl_SEMI CBRACE {if(flag == 0)
- {$$ = head = insertNode("Program");nodeAux = insert_value_node("Id", $2); insert_child($$, nodeAux);
- //head_table = insert_value_node_Table($2, "Class", "", "");
- //printf("\nHEAD TABLE TYPE: %s\n", head_table->type);
- head_table->name = $2;
- head_table->type = "Class";
- if($4!= NULL){
- insert_child($$, $4);
- }
- if(imprimir3==1){
- char s[5] = " - ";
- char *token;
- while(check_later != NULL){
- char type[50];
- char name[50];
- strcpy(name, check_later->child->value);
- strcpy(type, "(");
- AST_Tree_node *aux = check_later->child->brother;
- while(aux != NULL){
- token = strtok(aux->notacao, s);
- strcat(type, token);
- if(aux->brother !=NULL){
- strcat(type, ",");
- }
- aux = aux->brother;
- }
- strcat(type, ")\0");
- Table_node *method = check_later_func(name, type);
- printf("\nname: %s; type: %s; method->type: %s;\n", name, type, method->type);
- if(strcmp(method->type, "") == 0){
- printf("\n-----FDP PRA ISTO-----\n");
- }else{
- char call_type[10];
- strcpy(call_type, " - ");
- strcat(call_type, method->type);
- check_later->notacao = (char *)malloc(sizeof(call_type));
- strcpy(check_later->notacao, call_type);
- char methodID_type[50];
- strcpy(methodID_type, " - ");
- strcat(methodID_type, type);
- check_later->child->notacao = (char *)malloc(sizeof(methodID_type));
- strcpy(check_later->child->notacao, methodID_type);
- }
- check_later = check_later->brother;
- }
- }
- }
- }
- | CLASS ID OBRACE CBRACE {if(flag==0){
- $$ = head = insertNode("Program");
- nodeAux = insert_value_node("Id", $2);
- insert_child($$, nodeAux);head_table= insertNode_Table($2);
- aux_node_table=insertNode_Table($2);
- //head_table= insert_value_node_Table($2, "Class", "", "");
- head_table->name = $2;
- head_table->type = "Class";
- }
- }
- ;
- FieldDecl_MethodDecl_SEMI: FieldDecl_MethodDecl_SEMI FieldDecl {if(flag ==0){if($1 != NULL){$$ =$1;insert_brother($$, $2);}else{$$ = $2;}}}
- | FieldDecl_MethodDecl_SEMI MethodDecl {if(flag == 0){if($1 != NULL){$$=$1; insert_brother($$, $2);}else{$$ = $2;}}}
- | FieldDecl_MethodDecl_SEMI SEMI {if(flag == 0){if($1 != NULL){$$ = $1;}else{$$ = NULL;}}}
- | FieldDecl {if(flag ==0 ){$$ = $1;}}
- | MethodDecl {if(flag == 0){$$ = $1 ;}}
- | SEMI {if(flag == 0){$$ = NULL;}}
- ;
- FieldDecl: PUBLIC STATIC Type ID CommaID SEMI {if(flag ==0){
- int i=0;
- int length4 = strlen($3->type);
- char* lower4 = ( char* )malloc( length4 + 1 );
- if(strcmp($3->type, "Bool")==0){
- nodeAux_Table = insert_value_node_Table($4, "boolean", "", "");
- }else{
- lower4[length4] = 0;
- for(i = 0; i < length4; i++ )
- {
- lower4[i] = tolower( $3->type[i]);
- }
- nodeAux_Table = insert_value_node_Table($4, lower4, "", "");
- }
- insert_child_Table(head_table, nodeAux_Table);
- $$ = insertNode("FieldDecl");
- insert_child($$, $3);
- AST_Tree_node *nodeAuxaux = insert_value_node("Id", $4);
- insert_child($$, nodeAuxaux);
- nodeAux2 = $5;
- AST_Tree_node *node = nodeAux2;
- AST_Tree_node *copy;
- AST_Tree_node *copy2;
- while(node != NULL){
- if(strcmp($3->type, "Bool")==0){
- insert_child_Table(head_table, insert_value_node_Table(node->value, "boolean", "", ""));
- }else{
- insert_child_Table(head_table, insert_value_node_Table(node->value, lower4, "", ""));
- }
- copy = insertNode($3->type);
- copy2 = insert_value_node("Id", node->value);
- nodeAux3 = insertNode("FieldDecl");
- insert_brother($$, nodeAux3);
- insert_child(nodeAux3, copy);
- insert_child(nodeAux3, copy2);
- node = nodeAux2->brother;
- nodeAux2 = nodeAux2->brother;
- }
- }
- }
- | PUBLIC STATIC Type ID SEMI {if(flag == 0){
- int i=0;
- int length7 = strlen($3->type);
- char* lower7 = ( char* )malloc( length7 + 1 );
- $$ = insertNode("FieldDecl");
- insert_child($$, $3);
- nodeAux = insert_value_node("Id", $4);
- insert_child($$, nodeAux);
- if(strcmp($3->type, "Bool")==0){
- nodeAux_Table = insert_value_node_Table($4, "boolean", "", "");
- }else{
- lower7[length7] = 0;
- for(i = 0; i < length7; i++ )
- {
- lower7[i] = tolower( $3->type[i] );
- }
- nodeAux_Table = insert_value_node_Table($4, lower7, "", "");
- }
- insert_child_Table(head_table, nodeAux_Table);}
- }
- | error SEMI {flag = 1;$$=NULL;}
- ;
- CommaID: CommaID COMMA ID {if(flag == 0){$$ = $1 ; nodeAux = insert_value_node("Id", $3); insert_brother($$, nodeAux);}}
- | COMMA ID {if(flag == 0){$$ = insert_value_node("Id", $2);}}
- ;
- MethodDecl: PUBLIC STATIC MethodHeader MethodBody {if(flag ==0 ){
- $$ = insertNode("MethodDecl");
- insert_child($$, $3);
- insert_child($$, $4);
- if(strcmp(current_Method->type, "") != 0){
- insert_child_Table(head_table, current_Method);
- current_Method= insert_value_node_Table("", "", "", "");
- }
- }
- }
- ;
- MethodHeader: Type ID OCURV FormalParams CCURV {if(flag == 0){
- int i;
- $$ = insertNode("MethodHeader");
- insert_child($$, $1);
- nodeAux = insert_value_node("Id", $2);
- insert_child($$, nodeAux);
- insert_child($$, $4);
- nodeAux_Table = insertNode_Table($2);
- nodeAux_Table->type = $1->type;
- if(strcmp(nodeAux_Table->type, "Bool")==0){
- nodeAux_Table->type = "boolean";
- insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "boolean", "", ""));
- }else{
- int length2 = strlen(nodeAux_Table->type);
- char* lower2 = ( char* )malloc( length2 + 1 );
- lower2[length2] = 0;
- for(i = 0; i < length2; i++ )
- {
- lower2[i] = tolower( nodeAux_Table->type[i] );
- }
- nodeAux_Table->type = lower2;
- insert_child_Table(nodeAux_Table, insert_value_node_Table("return", lower2,"",""));
- }
- nodeAux4 = $4;
- char param[100];
- strcpy(param, "(");
- AST_Tree_node *node = nodeAux4->child;
- while(node!=NULL){
- if(strcmp(node->child->type, "Bool")==0){
- strcat(param, "boolean");
- insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "boolean", "", "param"));
- }else if(strcmp(node->child->type, "StringArray")==0){
- strcat(param, "String[]");
- insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "String[]", "", "param"));
- }else{
- int length = strlen(node->child->type);
- char* lower = ( char* )malloc( length + 1 );
- lower[length] = 0;
- for(i = 0; i < length; i++ )
- {
- lower[i] = tolower( node->child->type[i] );
- }
- strcat(param, lower);
- insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, lower, "", "param"));
- }
- if( node->brother != NULL){
- strcat(param, ",");
- }
- node = node->brother;
- }
- strcat(param, ")\0");
- nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
- strcpy(nodeAux_Table->paramsType, param);
- nodeAux_Table->flag = "";
- //insert_child_Table(head_table, nodeAux_Table);
- current_Method = nodeAux_Table;
- }
- }
- | Type ID OCURV CCURV {if(flag == 0){
- int i=0;
- $$ = insertNode("MethodHeader");
- insert_child($$, $1);
- nodeAux = insert_value_node("Id", $2);
- insert_child($$, nodeAux);
- nodeAux3=insertNode("MethodParams");
- insert_child($$,nodeAux3);
- nodeAux_Table = insertNode_Table($2);
- nodeAux_Table->type = $1->type;
- if(strcmp(nodeAux_Table->type, "Bool")==0){
- nodeAux_Table->type = "boolean";
- insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "boolean", "", ""));
- }else{
- int length2 = strlen(nodeAux_Table->type);
- char* lower2 = ( char* )malloc( length2 + 1 );
- lower2[length2] = 0;
- for(i = 0; i < length2; i++ )
- {
- lower2[i] = tolower( nodeAux_Table->type[i] );
- }
- nodeAux_Table->type = lower2;
- insert_child_Table(nodeAux_Table, insert_value_node_Table("return", lower2,"",""));
- }
- char param[100];
- strcpy(param, "(");
- strcat(param, ")\0");
- nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
- strcpy(nodeAux_Table->paramsType, param);
- nodeAux_Table->flag = "";
- //insert_child_Table(head_table, nodeAux_Table);
- current_Method = nodeAux_Table;
- }
- }
- | VOID ID OCURV FormalParams CCURV {if(flag == 0){
- int i=0;
- $$ = insertNode("MethodHeader");
- nodeAux = insertNode("Void");
- insert_child($$, nodeAux);
- nodeAux2 = insert_value_node("Id", $2);
- insert_child($$, nodeAux2);
- insert_child($$, $4);
- nodeAux_Table = insertNode_Table($2);
- nodeAux_Table->type = "void";
- insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "void","",""));
- nodeAux4 = $4;
- char param[100];
- strcpy(param, "(");
- AST_Tree_node *node = nodeAux4->child;
- while(node!=NULL){
- if(strcmp(node->child->type, "Bool")==0){
- strcat(param, "boolean");
- insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "boolean", "", "param"));
- }else if(strcmp(node->child->type, "StringArray")==0){
- strcat(param, "String[]");
- insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, "String[]", "", "param"));
- }else{
- int length = strlen(node->child->type);
- char* lower = ( char* )malloc( length + 1 );
- lower[length] = 0;
- for(i = 0; i < length; i++ )
- {
- lower[i] = tolower( node->child->type[i] );
- }
- strcat(param, lower);
- insert_child_Table(nodeAux_Table, insert_value_node_Table(node->child->brother->value, lower, "", "param"));
- }
- if( node->brother != NULL){
- strcat(param, ",");
- }
- node = node->brother;
- }
- strcat(param, ")\0");
- nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
- strcpy(nodeAux_Table->paramsType, param);
- nodeAux_Table->flag = "";
- current_Method = nodeAux_Table;
- }
- }
- | VOID ID OCURV CCURV {if(flag == 0){
- $$ = insertNode("MethodHeader");
- nodeAux = insertNode("Void");
- insert_child($$, nodeAux);
- nodeAux2 = insert_value_node("Id", $2);
- insert_child($$, nodeAux2);
- nodeAux3=insertNode("MethodParams");
- insert_child($$,nodeAux3);
- nodeAux_Table = insertNode_Table($2);
- nodeAux_Table->type = "void";
- insert_child_Table(nodeAux_Table, insert_value_node_Table("return", "void","",""));
- char param[100];
- strcpy(param, "(");
- strcat(param, ")\0");
- nodeAux_Table->paramsType =(char *)malloc(sizeof(param)+1);
- strcpy(nodeAux_Table->paramsType, param);
- nodeAux_Table->flag = "";
- //insert_child_Table(head_table, nodeAux_Table);
- current_Method = nodeAux_Table;
- }
- }
- ;
- FormalParams: Type ID CommaTypeID {if(flag == 0){
- $$ = insertNode("MethodParams");
- nodeAux2 = insertNode("ParamDecl");
- insert_child($$, nodeAux2);
- insert_child(nodeAux2, $1);
- nodeAux = insert_value_node("Id", $2);
- insert_child(nodeAux2, nodeAux);
- nodeAux2 = $3;
- AST_Tree_node *node = nodeAux2;
- AST_Tree_node *copy;
- AST_Tree_node *copy2;
- while(node!=NULL){
- copy = insertNode(node->type);
- copy2 = insert_value_node("Id", node->brother->value);
- nodeAux3 = insertNode("ParamDecl");
- insert_child($$, nodeAux3);
- insert_child(nodeAux3, copy);
- insert_child(nodeAux3, copy2);
- node = node->brother->brother;
- }
- }}
- | Type ID {if(flag == 0){$$ = insertNode("MethodParams"); nodeAux2 = insertNode("ParamDecl"); insert_child($$, nodeAux2); insert_child(nodeAux2, $1); nodeAux = insert_value_node("Id", $2); insert_child(nodeAux2, nodeAux);}}
- | STRING OSQUARE CSQUARE ID {if(flag == 0){$$ = insertNode("MethodParams"); nodeAux2 = insertNode("ParamDecl"); insert_child($$, nodeAux2); nodeAux = insertNode("StringArray"); insert_child(nodeAux2, nodeAux); nodeAux3 = insert_value_node("Id", $4); insert_child(nodeAux2, nodeAux3);}}
- ;
- CommaTypeID: COMMA Type ID {if(flag == 0){$$ = $2; nodeAux = insert_value_node("Id", $3); insert_brother($$, nodeAux);}}
- | CommaTypeID COMMA Type ID {if(flag ==0){$$ = $1; insert_brother($$, $3); nodeAux2 = insert_value_node("Id", $4); insert_brother($$, nodeAux2);}}
- ;
- MethodBody: OBRACE VarDecl_Statement CBRACE {if(flag == 0){$$ = insertNode("MethodBody"); if($2!=NULL){insert_child($$, $2);}}}
- | OBRACE CBRACE {if(flag == 0){$$ = insertNode("MethodBody");}}
- ;
- VarDecl_Statement: VarDecl {if(flag == 0){$$ = $1;}}
- | Statement {if(flag == 0){$$ = $1;}}
- | VarDecl_Statement VarDecl {if(flag == 0){if($1!=NULL){$$=$1;insert_brother($1,$2);}else{$$=$2;}}}
- | VarDecl_Statement Statement {if(flag == 0){if($1!=NULL){$$ = $1;if($2!=NULL){insert_brother($1,$2);}}else{$$=$2;}}}
- VarDecl: Type ID CommaID SEMI {if(flag == 0){
- int i = 0;
- int length5 = strlen($1->type);
- char* lower5 = ( char* )malloc( length5 + 1 );
- $$ = insertNode("VarDecl");
- insert_child($$,$1);
- nodeAux = insert_value_node("Id", $2);
- insert_child($$, nodeAux);
- nodeAux2 = $3;
- AST_Tree_node *node = nodeAux2;
- AST_Tree_node *copy;
- AST_Tree_node *copy2;
- if(strcmp($1->type, "Bool") == 0){
- if(strcmp(varDecl->type, "")==0){
- varDecl = insert_value_node_Table($2, "boolean", "", "");
- }else{
- insert_brother_Table(varDecl, insert_value_node_Table($2, "boolean", "", ""));
- }
- }else{
- lower5[length5] = 0;
- for(i = 0; i < length5; i++ )
- {
- lower5[i] = tolower( $1->type[i] );
- }
- if(strcmp(varDecl->type, "") == 0){
- varDecl = insert_value_node_Table($2, lower5, "", "");
- }else{
- insert_brother_Table(varDecl, insert_value_node_Table($2, lower5, "", ""));
- }
- }
- while(node != NULL){
- if(strcmp($1->type, "Bool") == 0){
- insert_brother_Table(varDecl, insert_value_node_Table(node->value, "boolean", "", ""));
- }else{
- insert_brother_Table(varDecl, insert_value_node_Table(node->value, lower5, "", ""));
- }
- copy = insertNode($1->type);
- copy2 = insert_value_node("Id", node->value);
- nodeAux3 = insertNode("VarDecl");
- insert_brother($$, nodeAux3);
- insert_child(nodeAux3, copy);
- insert_child(nodeAux3, copy2);
- node = nodeAux2->brother;
- nodeAux2 = nodeAux2->brother;
- }
- if(strcmp(varDecl->type, "")!=0){
- insert_child_Table(current_Method, varDecl);
- varDecl = insert_value_node_Table("", "", "", "");
- }
- }}
- | Type ID SEMI {if(flag == 0){
- int i = 0;
- int length6 = strlen($1->type);
- char* lower6 = ( char* )malloc( length6 + 1 );
- if(strcmp($1->type, "Bool") == 0){
- if(strcmp(varDecl->type, "")== 0){
- varDecl = insert_value_node_Table($2, "boolean", "", "");
- }else{
- insert_brother_Table(varDecl, insert_value_node_Table($2, "boolean", "", ""));
- }
- }else{
- lower6[length6] = 0;
- for(i = 0; i < length6; i++ )
- {
- lower6[i] = tolower( $1->type[i] );
- }
- if(strcmp(varDecl->type, "")==0){
- varDecl = insert_value_node_Table($2, lower6, "", "");
- }else{
- insert_brother_Table(varDecl, insert_value_node_Table($2, lower6, "", ""));
- }
- }
- if(strcmp(varDecl->type, "")!=0){
- insert_child_Table(current_Method, varDecl);
- varDecl = insert_value_node_Table("", "", "", "");
- }
- $$ = insertNode("VarDecl");
- insert_child($$, $1);
- nodeAux2 = insert_value_node("Id", $2);
- insert_child($$, nodeAux2);}
- }
- ;
- Type: BOOL {if(flag == 0){$$ = insertNode("Bool");}}
- | INT {if(flag == 0){$$ = insertNode("Int");}}
- | DOUBLE {if(flag == 0){$$ =insertNode("Double");}}
- ;
- Statement: OBRACE Statement_aux CBRACE {if(flag ==0){
- if($2!=NULL){
- if($2->brother!=NULL){
- nodeAux=insertNode("Block");
- insert_child(nodeAux,$2);
- $$=nodeAux;
- }
- else{
- $$=$2;
- }
- }
- else{
- $$=NULL;
- }
- }}
- | OBRACE CBRACE {if(flag==0){$$ = NULL;}}
- | IF OCURV Expr CCURV Statement {
- if(flag==0){
- $$=insertNode("If");
- insert_child($$,$3);
- nodeAux = $5;
- int num=0;
- if(nodeAux != NULL){
- AST_Tree_node *node = nodeAux;
- while(node!= NULL){
- node = node->brother;
- num++;
- }
- if(num==1){
- insert_child($$,nodeAux);
- nodeAux2 = insertNode("Block");
- insert_child($$, nodeAux2);
- }
- if(num>1){
- nodeAux2 = insertNode("Block");
- nodeAux3 = insertNode("Block");
- insert_child(nodeAux2, nodeAux);
- insert_child($$,nodeAux2);
- insert_brother(nodeAux2,nodeAux3);
- }
- }
- if(nodeAux==NULL)
- {
- nodeAux2 = insertNode("Block");
- nodeAux3 = insertNode("Block");
- insert_child($$, nodeAux2);
- insert_child($$, nodeAux3);
- }
- }
- }
- | IF OCURV Expr CCURV Statement ELSE Statement {
- if(flag==0){
- $$=insertNode("If");
- insert_child($$,$3);
- nodeAux = $5;
- int num=0, num2=0;
- if(nodeAux != NULL){
- AST_Tree_node *node = nodeAux;
- while(node!= NULL){
- node = node->brother;
- num++;
- }
- if(num==1){
- insert_child($$,nodeAux);
- }
- if(num>1){
- nodeAux2 = insertNode("Block");
- insert_child(nodeAux2, nodeAux);
- insert_child($$,nodeAux2);
- }
- }
- else if(nodeAux==NULL)
- {
- nodeAux2 = insertNode("Block");
- insert_child($$, nodeAux2);
- }
- if($7!=NULL)
- {
- AST_Tree_node *node2 = $7;
- while(node2!= NULL){
- node2 = node2->brother;
- num2++;
- }
- if(num2==1){
- insert_child($$,$7);
- }
- if(num2>1){
- nodeAux2 = insertNode("Block");
- insert_child(nodeAux2, $7);
- insert_child($$,nodeAux2);
- }
- }
- else if($7==NULL)
- {
- nodeAux2 = insertNode("Block");
- insert_child($$, nodeAux2);
- }
- }
- }
- | WHILE OCURV Expr CCURV Statement {
- if(flag==0){
- $$=insertNode("While");
- insert_child($$,$3);
- nodeAux = $5;
- int num=0;
- if(nodeAux != NULL){
- AST_Tree_node *node = nodeAux;
- while(node!= NULL){
- node = node->brother;
- num++;
- }
- if(num==1){
- insert_child($$,nodeAux);
- }
- if(num>1){
- nodeAux2 = insertNode("Block");
- insert_child(nodeAux2, nodeAux);
- insert_child($$,nodeAux2);
- }
- }
- if(nodeAux==NULL)
- {
- nodeAux2 = insertNode("Block");
- insert_child($$, nodeAux2);
- }
- }
- }
- | DO Statement WHILE OCURV Expr CCURV SEMI {
- if(flag==0){
- $$=insertNode("DoWhile");
- if($2==NULL)
- {
- nodeAux2 = insertNode("Block");
- insert_child($$, nodeAux2);
- }else{
- insert_child($$,$2);
- }
- nodeAux = $5;
- int num=0;
- if(nodeAux != NULL){
- AST_Tree_node *node = nodeAux;
- while(node!= NULL){
- node = node->brother;
- num++;
- }
- if(num==1){
- insert_child($$,nodeAux);
- }
- if(num>1){
- nodeAux2 = insertNode("Block");
- insert_child(nodeAux2, nodeAux);
- insert_child($$,nodeAux2);
- }
- }
- }
- }
- | PRINT OCURV Expr CCURV SEMI {
- if(flag==0){
- $$=insertNode("Print");
- insert_child($$,$3);
- }
- }
- | PRINT OCURV STRLIT CCURV SEMI {
- if(flag==0){
- $$=insertNode("Print");
- nodeAux = insert_value_node("StrLit", $3);
- if(imprimir3==1){
- nodeAux->notacao=" - String";
- }
- insert_child($$, nodeAux);
- }
- }
- | SEMI {
- if(flag==0){
- $$=NULL;
- }
- }
- | Assignment SEMI {
- if(flag==0){
- $$=$1;
- }
- }
- | MethodInvocation SEMI {
- if(flag==0){
- $$=$1;
- }
- }
- | ParseArgs SEMI {
- if(flag==0){
- $$=$1;
- }
- }
- | RETURN SEMI {
- if(flag==0){
- $$ = insertNode("Return");
- }
- }
- | RETURN Expr SEMI {
- if(flag==0){
- $$ = insertNode("Return");
- insert_child($$,$2);
- }
- }
- | error SEMI {flag = 1;$$=NULL;}
- ;
- Statement_aux: Statement {if(flag==0){$$=$1;}}
- | Statement_aux Statement {if(flag==0){if($1!=NULL){$$ = $1; insert_brother($1,$2);}else{$$=$2;}}}
- ;
- Expr: ExprNew {if(flag == 0){$$ = $1;}}
- | Assignment {if(flag == 0){$$ = $1;}}
- ;
- ExprNew: MethodInvocation {if(flag == 0){$$ = $1;}}
- | ParseArgs {if(flag == 0){$$ = $1;}if(imprimir3==1){$$->notacao=" - int";}}
- | ExprNew AND ExprNew {if(flag == 0){$$ = insertNode("And"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew OR ExprNew {if(flag == 0){$$ = insertNode("Or"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew EQ ExprNew {if(flag == 0){$$ = insertNode("Eq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew GEQ ExprNew {if(flag == 0){$$ = insertNode("Geq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew GT ExprNew {if(flag == 0){$$ = insertNode("Gt"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew LEQ ExprNew {if(flag == 0){$$ = insertNode("Leq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew LT ExprNew {if(flag == 0){$$ = insertNode("Lt"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew NEQ ExprNew {if(flag == 0){$$ = insertNode("Neq"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ExprNew PLUS ExprNew {if(flag == 0){$$ = insertNode("Add"); insert_child($$, $1); insert_child($$, $3);}if(imprimir3==1){
- $$->notacao=" - boolean";
- }}
- | ExprNew MINUS ExprNew {if(flag == 0){$$ = insertNode("Sub"); insert_child($$, $1); insert_child($$, $3);}}
- | ExprNew STAR ExprNew {if(flag == 0){$$ = insertNode("Mul"); insert_child($$, $1); insert_child($$, $3);}}
- | ExprNew DIV ExprNew {if(flag == 0){$$ = insertNode("Div"); insert_child($$, $1); insert_child($$, $3);}}
- | ExprNew MOD ExprNew {if(flag == 0){$$ = insertNode("Mod"); insert_child($$, $1); insert_child($$, $3);}}
- | PLUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Plus"); insert_child($$, $2);}}
- | MINUS ExprNew %prec NOT {if(flag == 0){$$ = insertNode("Minus"); insert_child($$, $2);}}
- | NOT ExprNew {if(flag == 0){$$ = insertNode("Not"); insert_child($$, $2);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | ID DOTLENGTH {if(flag == 0){$$ = insertNode("Length"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}
- if(imprimir3==1){
- Table_node *aux = find_symbol($1);
- if(strcmp(aux->type, "") == 0){
- printf("\n-----ERRO!-----\n");
- }else{
- char aux_string[20];
- strcpy(aux_string, " - ");
- strcat(aux_string, aux->type);
- strcat(aux_string, "\0");
- $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
- strcpy($$->child->notacao, aux_string);
- }
- $$->notacao=" - int";}
- }
- | ID {if(flag == 0){$$ = insert_value_node("Id", $1);}
- if(imprimir3==1){
- Table_node *aux = find_symbol($1);
- if(strcmp(aux->type, "") == 0){
- printf("\n-----ERRO!-----\n");
- }else{
- char aux_string[20];
- strcpy(aux_string, " - ");
- strcat(aux_string, aux->type);
- strcat(aux_string, "\0");
- $$->notacao=(char *)malloc(sizeof(aux_string)+1);
- strcpy($$->notacao, aux_string);
- }
- }
- }
- | OCURV Expr CCURV {if(flag == 0){$$ = $2;}}
- | BOOLLIT {if(flag == 0){$$ = insert_value_node("BoolLit", $1);}if(imprimir3==1){$$->notacao=" - boolean";}}
- | DECLIT {if(flag == 0){$$ = insert_value_node("DecLit", $1);}if(imprimir3==1){$$->notacao=" - int";}}
- | REALLIT {if(flag == 0){$$ = insert_value_node("RealLit", $1);}if(imprimir3==1){$$->notacao=" - double";}}
- | OCURV error CCURV {if(flag == 0){$$ = NULL;}}
- ;
- Assignment: ID ASSIGN Expr {if(flag == 0){$$ = insertNode("Assign"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}
- if(imprimir3==1){
- Table_node *aux = find_symbol($1);
- if(strcmp(aux->type, "") == 0){
- printf("\n-----ERRO!-----\n");
- }else{
- char aux_string[20];
- strcpy(aux_string, " - ");
- strcat(aux_string, aux->type);
- strcat(aux_string, "\0");
- $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
- strcpy($$->child->notacao, aux_string);
- }
- $$->notacao=(char *)malloc(sizeof($$->child->notacao)+1);
- strcpy($$->notacao, $$->child->notacao);
- }
- }
- ;
- MethodInvocation: ID OCURV CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);}
- if(imprimir3==1){
- Table_node *aux = find_symbol_Method($1);
- if(strcmp(aux->type, "") == 0){
- printf("\n-----ERRO!-----\n");
- if(strcmp(check_later->type, "") == 0){
- check_later = $$;
- }else{
- insert_brother(check_later, $$);
- }
- }else{
- char aux_string[20];
- strcpy(aux_string, " - ");
- strcat(aux_string, aux->type);
- strcat(aux_string, "\0");
- $$->notacao=(char *)malloc(sizeof(aux_string)+1);
- strcpy($$->notacao, aux_string);
- char aux_string2[20];
- strcpy(aux_string2, " - ");
- strcat(aux_string2, aux->paramsType);
- strcat(aux_string2, "\0");
- $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
- strcpy($$->child->notacao, aux_string2);
- }
- }
- }
- | ID OCURV Expr CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux); insert_child($$, $3);}
- if(imprimir3==1){
- Table_node *aux = find_symbol_Method($1);
- if(strcmp(aux->type, "") == 0){
- printf("\n-----ERRO!-----AQUI\n");
- if(strcmp(check_later->type, "") == 0){
- check_later = $$;
- }else{
- insert_brother(check_later, $$);
- }
- }else{
- char aux_string[20];
- strcpy(aux_string, " - ");
- strcat(aux_string, aux->type);
- strcat(aux_string, "\0");
- $$->notacao=(char *)malloc(sizeof(aux_string)+1);
- strcpy($$->notacao, aux_string);
- char aux_string2[20];
- strcpy(aux_string2, " - ");
- strcat(aux_string2, aux->paramsType);
- strcat(aux_string2, "\0");
- $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
- strcpy($$->child->notacao, aux_string2);
- }
- }
- }
- | ID OCURV Expr CommaExpr CCURV {if(flag == 0){$$ = insertNode("Call"); nodeAux = insert_value_node("Id", $1); insert_child($$, nodeAux);insert_child($$, $3); insert_child($$, $4);}
- if(imprimir3==1){
- printf("\nnome da funcao: %s", $1);
- Table_node *aux = find_symbol_Method($1);
- if(strcmp(aux->type, "") == 0){
- printf("\n-----ERRO!-----AQUI2\n");
- if(strcmp(check_later->type, "") == 0){
- check_later = $$;
- }else{
- insert_brother(check_later, $$);
- }
- }else{
- char aux_string[20];
- strcpy(aux_string, " - ");
- strcat(aux_string, aux->type);
- strcat(aux_string, "\0");
- $$->notacao=(char *)malloc(sizeof(aux_string)+1);
- strcpy($$->notacao, aux_string);
- char aux_string2[20];
- strcpy(aux_string2, " - ");
- strcat(aux_string2, aux->paramsType);
- strcat(aux_string2, "\0");
- $$->child->notacao=(char *)malloc(sizeof(aux_string2)+1);
- strcpy($$->child->notacao, aux_string2);
- }
- }
- }
- | ID OCURV error CCURV {flag = 1;$$=NULL;}
- ;
- CommaExpr: COMMA Expr {if(flag == 0){$$ = $2;}}
- | CommaExpr COMMA Expr {if(flag == 0){$$ = $1; insert_brother($$, $3);}}
- ;
- ParseArgs: PARSEINT OCURV ID OSQUARE Expr CSQUARE CCURV {if(flag == 0){$$ = insertNode("ParseArgs"); nodeAux = insert_value_node("Id", $3); insert_child($$, nodeAux); insert_child($$, $5);}
- if(imprimir3==1){
- Table_node *aux = find_symbol($3);
- if(strcmp(aux->type, "") == 0){
- printf("\n-----ERRO!-----\n");
- }else{
- char aux_string[20];
- strcpy(aux_string, " - ");
- strcat(aux_string, aux->type);
- strcat(aux_string, "\0");
- $$->child->notacao=(char *)malloc(sizeof(aux_string)+1);
- strcpy($$->child->notacao, aux_string);
- }
- }
- }
- | PARSEINT OCURV error CCURV {flag =1;$$=NULL;}
- ;
- %%
- void yyerror(const char* s){
- if(flag ==0){
- flag = 1;
- }
- printf("Line %d, col %d: %s: %s\n",n_linha,(int)(n_coluna - strlen(yytext)),s,yytext);
- }
- void insert_brother(AST_Tree_node* brother, AST_Tree_node* node)
- {
- AST_Tree_node* aux = brother;
- if(aux!=NULL && node!=NULL)
- {
- while(aux->brother != NULL)
- {
- aux = aux->brother;
- }
- aux->brother = node;
- }
- }
- void insert_brother_Table(Table_node* brother, Table_node* node)
- {
- Table_node* aux = brother;
- if(aux!=NULL && node!=NULL)
- {
- while(aux->brother != NULL)
- {
- aux = aux->brother;
- }
- aux->brother = node;
- }
- }
- void insert_child(AST_Tree_node* father, AST_Tree_node* child)
- {
- AST_Tree_node* temp = father->child;
- if(temp==NULL)
- {
- father->child = child;
- }
- else
- {
- insert_brother(father->child, child);
- }
- }
- void insert_child_Table(Table_node* father, Table_node* child)
- {
- Table_node* temp = father->child;
- if(temp==NULL)
- {
- father->child = child;
- }
- else
- {
- insert_brother_Table(father->child, child);
- }
- }
- AST_Tree_node* insert_value_node(char* node, char* value){
- AST_Tree_node* new_node = insertNode(node);
- new_node->value = value;
- return new_node;
- }
- Table_node* insert_value_node_Table(char* name, char* type, char* paramsType, char* flag){
- Table_node* new_node = insertNode_Table(name);
- new_node->type = type;
- new_node->paramsType = paramsType;
- new_node->flag=flag;
- return new_node;
- }
- AST_Tree_node* insertNode(char* node){
- AST_Tree_node* new_node;
- new_node = (AST_Tree_node *)(malloc(sizeof(AST_Tree_node)));
- if(new_node != NULL){
- new_node->child = NULL;
- new_node->brother = NULL;
- new_node->value = NULL;
- new_node->type = node;
- new_node->notacao=NULL;
- }
- return new_node;
- }
- Table_node* insertNode_Table(char* node){
- Table_node* new_node;
- new_node = (Table_node *)(malloc(sizeof(Table_node)));
- if(new_node != NULL){
- new_node->child = NULL;
- new_node->brother = NULL;
- new_node->name=node;
- new_node->type=NULL;
- new_node->paramsType=NULL;
- new_node->flag=NULL;
- }
- return new_node;
- }
- void printTree(AST_Tree_node *node, int level){
- printPoints(level);
- if(strcmp(node->type, "Id") == 0 || strcmp(node->type, "BoolLit") == 0 || strcmp(node->type, "DecLit") == 0 || strcmp(node->type, "RealLit") == 0 || strcmp(node->type, "StrLit") == 0 ){
- printTerminal(node);
- }
- else{
- if(imprimir3==1 && node->notacao!=NULL){
- printf("%s%s\n", node->type,node->notacao);
- }else{
- printf("%s\n", node->type);
- }
- }
- AST_Tree_node *child = node->child;
- if(child != NULL){
- printTree(child, level+1);
- while(child->brother != NULL){
- child = child->brother;
- printTree(child, level+1);
- }
- }
- }
- void printTerminal(AST_Tree_node *node){
- if(imprimir3==1 && node->notacao!=NULL){
- printf("%s(%s)%s\n", node->type, node->value, node->notacao);
- }else{
- printf("%s(%s)\n", node->type, node->value);
- }
- }
- void printPoints(int n){
- while(n > 0){
- printf("..");
- n--;
- }
- }
- void printTable(Table_node *node){
- int count =0;
- int flag2 = 0;
- printf("===== %s %s Symbol Table =====\n", node->type, node->name);
- if(node->child !=NULL){
- Table_node *aux = node->child;
- while(flag2 !=1){
- printf("%s\t%s\t%s\n", aux->name, aux->paramsType, aux->type);
- if(aux->child != NULL){
- count++;
- }
- if(aux->brother != NULL)
- {
- aux = aux->brother;
- }else{
- flag2 =1 ;
- }
- }
- }
- printf("\n");
- if( count != 0){
- Table_node *aux2 = node->child;
- while(count != 0){
- if(aux2->child != NULL){
- count--;
- Table_node *aux3 = aux2->child;
- printf("===== Method %s%s Symbol Table =====\n", aux2->name, aux2->paramsType);
- while(aux3 != NULL){
- if(strcmp(aux3->flag, "") == 0){
- printf("%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type);
- }else{
- printf("%s\t%s\t%s\t%s\n", aux3->name, aux3->paramsType, aux3->type, aux3->flag);
- }
- aux3 = aux3->brother;
- if(aux3==NULL){
- printf("\n");
- }
- }
- }
- aux2 = aux2->brother;
- }
- }
- }
- Table_node *check_later_func(char *name, char *type){
- if(head_table->child != NULL){
- Table_node* child_class = head_table->child;
- while(child_class !=NULL){
- //printf("\nfuncao: %s ; paramsType:%s; type:%s\n", child_class->name, child_class->paramsType, child_class->type);
- if(strcmp(child_class->paramsType, type) == 0){
- if(strcmp(child_class->name, name) == 0){
- return child_class;
- }
- }
- child_class = child_class->brother;
- }
- }
- Table_node *cannot_find_method = insert_value_node_Table("", "", "", "");
- return cannot_find_method;
- }
- Table_node* find_symbol_Method(char *name){
- if(head_table->child != NULL){
- Table_node* child_class = head_table->child;
- while(child_class !=NULL){
- //printf("\nfuncao: %s ; paramsType:%s; type:%s\n", child_class->name, child_class->paramsType, child_class->type);
- if(strcmp(child_class->paramsType, "") != 0){
- if(strcmp(child_class->name, name) == 0){
- return child_class;
- }
- }
- child_class = child_class->brother;
- }
- }
- Table_node *cannot_find_method = insert_value_node_Table("", "", "", "");
- return cannot_find_method;
- }
- Table_node* find_symbol(char* name){
- if(head_table->child != NULL){
- Table_node* child_class = head_table->child;
- while(child_class !=NULL){
- if(strcmp(child_class->name, name) == 0){
- return child_class;
- }
- child_class = child_class->brother;
- }
- }
- if(current_Method->child !=NULL){
- Table_node* child_Method = current_Method->child;
- while(child_Method !=NULL){
- if(strcmp(child_Method->name, name)==0){
- return child_Method;
- }
- child_Method = child_Method->brother;
- }
- }
- Table_node *cannot_find_symbol = insert_value_node_Table("", "", "", "");
- return cannot_find_symbol;
- }
- int main(int argc, char *argv[])
- {
- int imprimir2 =0;
- head_table= insert_value_node_Table("", "Class", "", "");
- varDecl= insert_value_node_Table("", "", "", "");
- current_Method= insert_value_node_Table("", "", "", "");
- check_later= insert_value_node("", "");
- if(argc>1){
- if(strcmp(argv[1],"-l")==0){
- imprimir=1;
- yylex();
- }
- else if(strcmp(argv[1],"-1")==0){
- yylex();
- }
- else if(strcmp(argv[1],"-2")==0){
- parse=1;
- yyparse();
- }else if(flag ==0 && strcmp(argv[1],"-t")==0){
- parse = 1;
- imprimir2 = 1;
- yyparse();
- }else if(strcmp(argv[1],"-s")==0){
- parse=1;
- imprimir3=1;
- yyparse();
- }
- }else{
- parse = 1;
- yyparse();
- }
- if(imprimir2 == 1 && flag == 0){
- printTree(head, 0);
- }
- if(imprimir3==1 && flag==0){
- printTable(head_table);
- printTree(head,0);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment