Skip to content

Commit

Permalink
working AST
Browse files Browse the repository at this point in the history
  • Loading branch information
bhavananarapareddy committed Apr 6, 2019
1 parent eeacc47 commit 6a999ae
Show file tree
Hide file tree
Showing 29 changed files with 893 additions and 34 deletions.
Binary file modified .vscode/ipch/1040019817a43567/driver.ipch
Binary file not shown.
Binary file modified .vscode/ipch/1ff17bd5f4f3264a/key.ipch
Binary file not shown.
Binary file added .vscode/ipch/5f455833527d977d/mmap_address.bin
Binary file not shown.
Binary file added .vscode/ipch/5f455833527d977d/semantic.ipch
Binary file not shown.
Binary file modified .vscode/ipch/9819c54e5eb90701/AST.ipch
Binary file not shown.
Binary file modified .vscode/ipch/c1487d9c51588b0e/parser.ipch
Binary file not shown.
671 changes: 671 additions & 0 deletions AST (copy).c

Large diffs are not rendered by default.

152 changes: 120 additions & 32 deletions AST.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,47 @@
#include "ASTDef.h"

ASTNodeIt* newNonLeafNode(TAG taginf, tokenInfo *ti, ASTNodeIt* input1, ASTNodeIt* input2, ASTNodeIt* input3){
input1->next=input2;
input2->next=input3;
ASTNodeIt* k=input1;


if(input1!=NULL)
{

while(k->next!=NULL)
{
k=k->next;
}
k->next=input2;
// input2->next=NULL;
}
if(input2!=NULL)
{
while(k->next!=NULL)
{
k=k->next;
}
k->next=input3;
// input3->next=NULL;
}
if(input3!=NULL)
{
while(k->next!=NULL)
{
k=k->next;
}
k->next=NULL;
}
ASTNodeIt* final_node=(ASTNodeIt*)malloc(sizeof(ASTNodeIt));

final_node->node = (ASTNode*)malloc(sizeof(ASTNode));
final_node->node->is_leaf=0;

final_node->node->u.n=(nonLeaf*)malloc(sizeof(nonLeaf));
final_node->node->u.n->tag_info=taginf;
final_node->node->u.n->leaf_symbol=ti;
final_node->node->u.n->children=input1;
final_node->node->is_leaf=0;
final_node->node->parent=NULL;
final_node->next=NULL;
ASTNodeIt* temp=input1;
while(temp!=NULL){
temp->node->parent = final_node;
Expand All @@ -32,39 +65,42 @@ ASTNodeIt* ChildrenList(ASTNodeIt* input1, ASTNodeIt* input2){
ASTNodeIt* newLeafNode(tokenInfo *ti){
ASTNodeIt* final_node = (ASTNodeIt*)malloc(sizeof(ASTNodeIt));
final_node->node = (ASTNode*)malloc(sizeof(ASTNode));
final_node->node->parent=NULL;
final_node->node->is_leaf=1;
final_node->node->u.l=(Leaf*)malloc(sizeof(Leaf));
final_node->node->u.l->leaf_symbol=ti;
final_node->next=NULL;
return final_node;
}

void freeChildren(treeNodeIt *temp){
static int arr[23]= {TK_FUNID, TK_ID, TK_INT, TK_REAL, TK_RECORDID, TK_FIELDID, TK_GLOBAL, TK_ASSIGNOP, TK_NUM, TK_RNUM, TK_MUL, TK_DIV, TK_MINUS, TK_PLUS, TK_NOT, TK_AND, TK_OR, TK_LT, TK_LE, TK_GT, TK_GE, TK_EQ, TK_NE};
treeNodeIt *freetemp;
while(temp!=NULL){
int flag=0;
if(temp->t->is_leaf==1){
for(int i=0; i<24; i++){
if(temp->t->treeNode_type.l->leaf_symbol->tokenName==arr[i]){
temp=temp->next;
flag=1;
break;
}
}
if(flag==1){
continue;
}
}
if(temp->t->is_leaf==1){
free(temp->t->treeNode_type.l->leaf_symbol);
free(temp->t->treeNode_type.l);
}else{
free(temp->t->treeNode_type.n);
}
free(temp->t);
freetemp=temp;
temp=temp->next;
free(freetemp);
}
// static int arr[23]= {TK_FUNID, TK_ID, TK_INT, TK_REAL, TK_RECORDID, TK_FIELDID, TK_GLOBAL, TK_ASSIGNOP, TK_NUM, TK_RNUM, TK_MUL, TK_DIV, TK_MINUS, TK_PLUS, TK_NOT, TK_AND, TK_OR, TK_LT, TK_LE, TK_GT, TK_GE, TK_EQ, TK_NE};
// treeNodeIt *freetemp;
// while(temp!=NULL){
// int flag=0;
// if(temp->t->is_leaf==1){
// for(int i=0; i<24; i++){
// if(temp->t->treeNode_type.l->leaf_symbol->tokenName==arr[i]){
// temp=temp->next;
// flag=1;
// break;
// }
// }
// if(flag==1){
// continue;
// }
// }
// if(temp->t->is_leaf==1){
// free(temp->t->treeNode_type.l->leaf_symbol);
// free(temp->t->treeNode_type.l);
// }else{
// free(temp->t->treeNode_type.n);
// }
// free(temp->t);
// freetemp=temp;
// temp=temp->next;
// free(freetemp);
// }
}

ASTNodeIt* semanticRuleExecute(treeNodeIt *t, int rule_no){
Expand Down Expand Up @@ -101,7 +137,10 @@ ASTNodeIt* semanticRuleExecute(treeNodeIt *t, int rule_no){
//function.node=newNode(TAG_FUNCTION, LeafNode(TK_FUNID),new Node(TAG_INPUT_PARS, NULL, input_par.node),new Node(TAG_OUTPUT_PARS, NULL, output_par.node), stmts.node)
case 4:{
treeNodeIt *temp = t->t->treeNode_type.n->children;
ASTNodeIt* n = newNonLeafNode(TAG_FUNCTION, temp->t->treeNode_type.l->leaf_symbol, newNonLeafNode(TAG_INPUT_PARS, NULL, temp->next->node, NULL, NULL), newNonLeafNode(TAG_OUTPUT_PARS, NULL, temp->next->next->node,NULL, NULL), temp->next->next->next->next->node);
ASTNodeIt* n = newNonLeafNode(TAG_FUNCTION, temp->t->treeNode_type.l->leaf_symbol,
newNonLeafNode(TAG_INPUT_PARS, NULL, temp->next->node, NULL, NULL),
newNonLeafNode(TAG_OUTPUT_PARS, NULL, temp->next->next->node,NULL, NULL),
temp->next->next->next->next->node);
freeChildren(temp);
return n;
}
Expand Down Expand Up @@ -315,7 +354,7 @@ ASTNodeIt* semanticRuleExecute(treeNodeIt *t, int rule_no){
case 36 :
{
treeNodeIt *temp = t->t->treeNode_type.n->children;
ASTNodeIt* n = newNonLeafNode(TAG_ASSIGNMENT_STMT, temp->t->treeNode_type.l->leaf_symbol, temp->node, temp->next->next->node, NULL);
ASTNodeIt* n = newNonLeafNode(TAG_ASSIGNMENT_STMT, temp->next->t->treeNode_type.l->leaf_symbol, temp->node, temp->next->next->node, NULL);
freeChildren(temp);
return n;
}
Expand Down Expand Up @@ -599,6 +638,7 @@ ASTNodeIt* semanticRuleExecute(treeNodeIt *t, int rule_no){

ASTNodeIt* makeAbstractSyntaxTree(treeNodeIt *root){
treeNodeIt *temp = root;

while(1){
while(temp->t->is_leaf==0){
temp = temp->t->treeNode_type.n->children;
Expand All @@ -618,11 +658,59 @@ ASTNodeIt* makeAbstractSyntaxTree(treeNodeIt *root){
//Get ASTNode and free the subsequent nodes
if(temp->t->treeNode_type.n->children->t->is_leaf==0){
temp->node = semanticRuleExecute(temp, temp->t->treeNode_type.n->children->t->treeNode_type.n->rule_no);


}else{
temp->node = semanticRuleExecute(temp, temp->t->treeNode_type.n->children->t->treeNode_type.l->rule_no);
}
}
//iterate
temp = temp->next;
}
}

void printAST(ASTNodeIt* root)
{
ASTNodeIt *temp = root;
ASTNodeIt* temp_child;
while(1)
{
while(temp->node->is_leaf==0){
temp_child=temp->node->u.n->children;
if(temp_child==NULL)
{
printf("nonleaf with no children: node tag: %d \n",temp->node->u.n->tag_info);

if(temp->next==NULL)
break;

temp=temp->next;
}
else
{
temp=temp->node->u.n->children;
}


}
// if (temp==NULL) break;
if(temp->node->is_leaf==1)
printf("leaf node:\t\t token: %d\n",temp->node->u.l->leaf_symbol->tokenName);
while(temp->next==NULL){
temp=temp->node->parent;
if(temp==NULL){
printf("root reached");
return;
}
if(temp->node->u.n->leaf_symbol!=NULL)
printf("nonleaf node:\t\t token: %d tag: %d \n",temp->node->u.n->leaf_symbol->tokenName,temp->node->u.n->tag_info);
else
{
printf("nonleaf node:\t\t token: not stored tag: %d \n",temp->node->u.n->tag_info);
}

}
temp=temp->next;
}

}
1 change: 1 addition & 0 deletions AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ ASTNodeIt* newNonLeafNode (TAG taginf, tokenInfo *ti, ASTNodeIt* input1, ASTNode
ASTNodeIt* ChildrenList (ASTNodeIt* input1, ASTNodeIt* input2);
ASTNodeIt* newLeafNode(tokenInfo *ti);
void freeChildren(treeNodeIt *temp);
void printAST(ASTNodeIt* root);
//Nochestrell
Binary file modified AST.h.gch
Binary file not shown.
Binary file modified AST.o
Binary file not shown.
Binary file modified ASTDef.h.gch
Binary file not shown.
3 changes: 2 additions & 1 deletion driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ int main(int argc, char *argv[]){
ComputeFirstAndFollowSets();
createParseTable();
treeNodeIt* t = parseInputSourceCode("testcase1.txt");
//printParseTree(t, argv[2]);
// printParseTree(t, argv[2]);
ASTNodeIt *plgiveerror = makeAbstractSyntaxTree(t);
printAST(plgiveerror);
printf("\n");
}else if(option==4){

Expand Down
Binary file modified driver.o
Binary file not shown.
Binary file modified grammar.h.gch
Binary file not shown.
Binary file modified grammarDef.h.gch
Binary file not shown.
Binary file modified key.h.gch
Binary file not shown.
Binary file modified keyDef.h.gch
Binary file not shown.
Binary file modified lexer.h.gch
Binary file not shown.
Binary file modified lexerDef.h.gch
Binary file not shown.
83 changes: 83 additions & 0 deletions parse1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
_sumN 4 TK_FUNID -------------- function Yes --------------
input 4 TK_INPUT -------------- input_par Yes --------------
parameter 4 TK_PARAMETER -------------- input_par Yes --------------
list 4 TK_LIST -------------- input_par Yes --------------
[ 4 TK_SQL -------------- input_par Yes --------------
int 4 TK_INT -------------- primitiveDatatype Yes --------------
-------------- 4 -------------- -------------- dataType No primitiveDatatype
-------------- 4 -------------- -------------- parameter_list No dataType
d5cc34 4 TK_ID -------------- parameter_list Yes --------------
eps 4 eps -------------- remaining_list Yes --------------
-------------- 4 -------------- -------------- parameter_list No remaining_list
-------------- 4 -------------- -------------- input_par No parameter_list
] 4 TK_SQR -------------- input_par Yes --------------
-------------- 4 -------------- -------------- function No input_par
output 5 TK_OUTPUT -------------- output_par Yes --------------
parameter 5 TK_PARAMETER -------------- output_par Yes --------------
list 5 TK_LIST -------------- output_par Yes --------------
[ 5 TK_SQL -------------- output_par Yes --------------
real 5 TK_REAL -------------- primitiveDatatype Yes --------------
-------------- 5 -------------- -------------- dataType No primitiveDatatype
-------------- 5 -------------- -------------- parameter_list No dataType
d3 5 TK_ID -------------- parameter_list Yes --------------
eps 5 eps -------------- remaining_list Yes --------------
-------------- 5 -------------- -------------- parameter_list No remaining_list
-------------- 5 -------------- -------------- output_par No parameter_list
] 5 TK_SQR -------------- output_par Yes --------------
-------------- 4 -------------- -------------- function No output_par
; 5 TK_SEM -------------- function Yes --------------
eps 6 eps -------------- typeDefinitions Yes --------------
-------------- 6 -------------- -------------- stmts No typeDefinitions
type 6 TK_TYPE -------------- declaration Yes --------------
int 6 TK_INT -------------- primitiveDatatype Yes --------------
-------------- 6 -------------- -------------- dataType No primitiveDatatype
-------------- 6 -------------- -------------- declaration No dataType
: 6 TK_COLON -------------- declaration Yes --------------
b5b567 6 TK_ID -------------- declaration Yes --------------
eps 6 eps -------------- global_or_not Yes --------------
-------------- 6 -------------- -------------- declaration No global_or_not
; 6 TK_SEM -------------- declaration Yes --------------
-------------- 6 -------------- -------------- declarations No declaration
eps 7 eps -------------- declarations Yes --------------
-------------- 6 -------------- -------------- declarations No declarations
-------------- 6 -------------- -------------- stmts No declarations
eps 7 eps -------------- otherStmts Yes --------------
-------------- 6 -------------- -------------- stmts No otherStmts
return 7 TK_RETURN -------------- returnStmt Yes --------------
eps 7 eps -------------- optionalReturn Yes --------------
-------------- 7 -------------- -------------- returnStmt No optionalReturn
; 7 TK_SEM -------------- returnStmt Yes --------------
-------------- 6 -------------- -------------- stmts No returnStmt
-------------- 4 -------------- -------------- function No stmts
end 8 TK_END -------------- function Yes --------------
-------------- 4 -------------- -------------- otherFunctions No function
eps 10 eps -------------- otherFunctions Yes --------------
-------------- 4 -------------- -------------- otherFunctions No otherFunctions
-------------- 4 -------------- -------------- program No otherFunctions
_main 10 TK_MAIN -------------- mainFunction Yes --------------
eps 11 eps -------------- typeDefinitions Yes --------------
-------------- 11 -------------- -------------- stmts No typeDefinitions
type 11 TK_TYPE -------------- declaration Yes --------------
int 11 TK_INT -------------- primitiveDatatype Yes --------------
-------------- 11 -------------- -------------- dataType No primitiveDatatype
-------------- 11 -------------- -------------- declaration No dataType
: 11 TK_COLON -------------- declaration Yes --------------
b3 11 TK_ID -------------- declaration Yes --------------
eps 11 eps -------------- global_or_not Yes --------------
-------------- 11 -------------- -------------- declaration No global_or_not
; 11 TK_SEM -------------- declaration Yes --------------
-------------- 11 -------------- -------------- declarations No declaration
eps 12 eps -------------- declarations Yes --------------
-------------- 11 -------------- -------------- declarations No declarations
-------------- 11 -------------- -------------- stmts No declarations
eps 12 eps -------------- otherStmts Yes --------------
-------------- 11 -------------- -------------- stmts No otherStmts
return 12 TK_RETURN -------------- returnStmt Yes --------------
eps 12 eps -------------- optionalReturn Yes --------------
-------------- 12 -------------- -------------- returnStmt No optionalReturn
; 12 TK_SEM -------------- returnStmt Yes --------------
-------------- 11 -------------- -------------- stmts No returnStmt
-------------- 10 -------------- -------------- mainFunction No stmts
end 13 TK_END -------------- mainFunction Yes --------------
-------------- 4 -------------- -------------- program No mainFunction
-------------- 4 -------------- -------------- Root node No program
Binary file modified parser.h.gch
Binary file not shown.
Binary file modified parser.o
Binary file not shown.
Binary file modified parserDef.h.gch
Binary file not shown.
3 changes: 2 additions & 1 deletion rules.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Rule 0: program --> otherFunctions mainFunction
Rule 1: mainFunction --> TK_MAIN stmts TK_END
Rule 2: otherFunctions -->function otherFunctions
Rule 3: otherFunctions --> eps
Expand Down Expand Up @@ -83,4 +84,4 @@ Rule 82: optionalReturn --> TK_SQL idList TK_SQR
Rule 83: optionalReturn --> eps
Rule 84: idList --> TK_ID more_ids
Rule 85: more_ids --> TK_COMMA idList
Rule 86: more_ids --> eps
Rule 86: more_ids --> eps
Binary file modified stack.h.gch
Binary file not shown.
Binary file modified stackDef.h.gch
Binary file not shown.
Binary file modified stage1exe
Binary file not shown.
14 changes: 14 additions & 0 deletions test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%Test Case 1
%Following function computes the sum of user defined real numbers
%The variable d3 maintains the sum of values
_sumN input parameter list [int d5cc34]
output parameter list[real d3];
type int : b5b567;
return;
end

_main
type int :b3;
return;
end

0 comments on commit 6a999ae

Please sign in to comment.