Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void yyerror(char *s);
- int yylex(void);
- int flagError = 0;
- extern int flag;
- typedef struct node {
- char * type;
- struct node * nodeSon;
- struct node * nodeBrother;
- } node;
- node * nodeAux=NULL;
- node * temp_list=NULL;
- node * insertNode(char * type, node * nodeSon) {
- node * auxNode = (node *)malloc(sizeof(node));
- auxNode->type = type;
- auxNode->nodeSon = nodeSon;
- auxNode->nodeBrother = NULL;
- return auxNode;
- }
- void printPoints(int n_points) {
- int i;
- for(i=0; i<n_points; i++)
- printf(".");
- }
- void printTree(node * node, int points) {
- while(node) {
- if (strcmp(node->type, "NULL")!= 0) {
- printPoints(points);
- printf("%s\n", node->type);
- }
- printTree(node->nodeSon, points+2);
- node = node->nodeBrother;
- }
- }
- node * insertBrother(node *oldB, node *newB){
- node *aux = oldB;
- while(aux->nodeBrother != NULL){
- aux = aux->nodeBrother;
- }
- aux->nodeBrother = newB;
- return oldB;
- }
- %}
- %union{
- struct node * node;
- char * id;
- }
- %token <id> ID
- %token <id> INTLIT
- %token <id> REALLIT
- %token <id> STRLIT
- %type <node> Program
- %type <node> Declarations
- %type <node> VarDeclaration
- %type <node> FuncDeclaration
- %type <node> VarSpec
- %type <node> VarSpecAux
- %type <node> Type
- %type <node> FuncBody
- %type <node> VarsAndStatements
- %type <node> Parameters
- %type <node> ParametersAux
- %type <node> Statement
- %type <node> Expr
- %type <node> FuncInvocation
- %token SEMICOLON BLANKID PACKAGE RETURN AND STAR COMMA DIV ASSIGN EQ GE GT LBRACE LE LPAR LSQ LT MINUS MOD NE NOT OR PLUS RBRACE RPAR RSQ ELSE FOR IF VAR INT FLOAT32 BOOL STRING PRINT PARSEINT FUNC CMDARGS RESERVED ESCSEQ
- %left COMMA
- %right ASSIGN
- %left OR
- %left AND
- %left EQ NE
- %left LE LT GT GE
- %left PLUS MINUS
- %left STAR DIV MOD
- %right NOT
- %left RPAR LPAR
- %%
- Program:
- PACKAGE ID SEMICOLON Declarations {$$ = insertNode("Program", $4); if(flagError==0 && flag ==2)printTree($$, 0);}
- ;
- Declarations: %empty {$$ = insertNode("NULL", NULL);
- }
- | Declarations VarDeclaration SEMICOLON {$$ = insertBrother($1, $2);
- }
- | Declarations FuncDeclaration SEMICOLON {nodeAux = insertNode("FuncDecl", $2);
- $$ = insertBrother($1, nodeAux);
- }
- ;
- VarDeclaration:
- VAR VarSpec {$$ = $2;
- }
- | VAR LPAR VarSpec SEMICOLON RPAR {$$ = $3;
- }
- ;
- VarSpec:
- ID Type {char strAux[1024];
- sprintf(strAux, "Id(%s)", $1);
- nodeAux = insertNode(strdup(strAux), NULL);
- insertBrother($2, nodeAux);
- $$ = insertNode("VarDecl", $2);
- }
- | ID VarSpecAux Type {char strAux[1024];
- sprintf(strAux, "Id(%s)", $1);
- nodeAux = insertNode(strdup(strAux), NULL);
- insertBrother($3, nodeAux);
- $$ = insertNode("VarDecl", $3);
- $$->nodeBrother = $2;
- temp_list = $$;
- while(temp_list) {
- temp_list->nodeSon->type = $3->type;
- temp_list = temp_list->nodeBrother;
- }
- }
- ;
- VarSpecAux:
- COMMA ID {char strAux[1024];
- sprintf(strAux, "Id(%s)", $2);
- nodeAux = insertNode(NULL, NULL);
- insertBrother(nodeAux, insertNode(strdup(strAux), NULL));
- $$ = insertNode("VarDecl", nodeAux);
- }
- | VarSpecAux COMMA ID {
- char strAux[1024];
- sprintf(strAux, "Id(%s)", $3);
- nodeAux = insertNode(NULL, NULL);
- insertBrother(nodeAux, insertNode(strdup(strAux), NULL));
- temp_list = $$;
- while(temp_list->nodeBrother) {
- temp_list = temp_list->nodeBrother;
- }
- temp_list->nodeBrother = insertNode("VarDecl", nodeAux);
- }
- ;
- Type:
- INT {$$ = insertNode("Int", NULL);}
- | FLOAT32 {$$ = insertNode("Float32", NULL);}
- | BOOL {$$ = insertNode("Bool", NULL);}
- | STRING {$$ = insertNode("String", NULL);}
- ;
- FuncDeclaration:
- FUNC ID LPAR RPAR FuncBody {char strAux[1024];
- sprintf(strAux, "Id(%s)", $2);
- nodeAux = insertNode(strdup(strAux), NULL);
- $$ = insertNode("FuncHeader", nodeAux);
- insertBrother(nodeAux, insertNode("FuncParams", NULL));
- insertBrother($$, $5);
- }
- | FUNC ID LPAR Parameters RPAR FuncBody {char strAux[1024];
- sprintf(strAux, "Id(%s)", $2);
- nodeAux = insertNode(strdup(strAux), NULL);
- $$ = insertNode("FuncHeader", nodeAux);
- insertBrother(nodeAux, insertNode("FuncParams", $4));
- insertBrother($$, $6);
- }
- | FUNC ID LPAR RPAR Type FuncBody {char strAux[1024];
- sprintf(strAux, "Id(%s)", $2);
- nodeAux = insertNode(strdup(strAux), NULL);
- insertBrother(nodeAux, $5);
- insertBrother(nodeAux, insertNode("FuncParams", NULL));
- $$ = insertNode("FuncHeader", nodeAux);
- insertBrother($$, $6);
- }
- | FUNC ID LPAR Parameters RPAR Type FuncBody {char strAux[1024];
- sprintf(strAux, "Id(%s)", $2);
- nodeAux = insertNode(strdup(strAux), NULL);
- insertBrother(nodeAux, $6);
- insertBrother(nodeAux, insertNode("FuncParams", $4));
- $$ = insertNode("FuncHeader", nodeAux);
- insertBrother($$, $7);
- }
- ;
- Parameters:
- ID Type {char strAux[1024];
- sprintf(strAux, "Id(%s)", $1);
- nodeAux = insertBrother($2, insertNode(strdup(strAux), NULL));
- $$ = insertNode("ParamDecl", nodeAux);
- }
- | ID Type ParametersAux {
- char strAux[1024];
- sprintf(strAux, "Id(%s)", $1);
- nodeAux = insertNode(strdup(strAux), NULL);
- insertBrother($2, nodeAux);
- // inserir no inicio da lista
- $$ = insertNode("ParamDecl", $2);
- $$->nodeBrother = $3;
- }
- ;
- ParametersAux:
- COMMA ID Type {char strAux[1024];
- sprintf(strAux, "Id(%s)", $2);
- nodeAux = insertNode(strdup(strAux), NULL);
- insertBrother($3, nodeAux);
- $$ = insertNode("ParamDecl", $3);
- }
- | ParametersAux COMMA ID Type {char strAux[1024];
- sprintf(strAux, "Id(%s)", $3);
- nodeAux = insertNode(strdup(strAux), NULL);
- insertBrother($4, nodeAux);
- nodeAux = insertNode("ParamDecl", $4);
- temp_list = $$;
- while(temp_list->nodeBrother) {
- temp_list = temp_list->nodeBrother;
- }
- temp_list->nodeBrother = nodeAux;
- }
- ;
- FuncBody:
- LBRACE VarsAndStatements RBRACE {$$ = insertNode("FuncBody", $2);}
- ;
- VarsAndStatements: %empty {$$ = insertNode("NULL", NULL);}
- | VarsAndStatements SEMICOLON {$$ = $1;}
- | VarsAndStatements VarDeclaration SEMICOLON {$$ = insertBrother($$, $2);}
- | VarsAndStatements Statement SEMICOLON {$$ = insertNode("FuncBody", $2);}
- ;
- Statement:
- ID ASSIGN Expr {}
- | LBRACE StatementAux RBRACE {}
- | IF Expr LBRACE StatementAux RBRACE ELSE LBRACE StatementAux RBRACE {}
- | IF Expr LBRACE StatementAux RBRACE {}
- | FOR LBRACE StatementAux RBRACE {}
- | FOR Expr LBRACE StatementAux RBRACE {}
- | RETURN {}
- | RETURN Expr {}
- | FuncInvocation {$$ = $1;}
- | ParseArgs {}
- | PRINT LPAR Expr RPAR {}
- | PRINT LPAR STRLIT RPAR {}
- | error {}
- ;
- StatementAux: %empty {}
- | Statement SEMICOLON StatementAux {}
- ;
- ParseArgs:
- ID COMMA BLANKID ASSIGN PARSEINT LPAR CMDARGS LSQ Expr RSQ RPAR {}
- | ID COMMA BLANKID ASSIGN PARSEINT LPAR error RPAR {}
- ;
- FuncInvocation:
- ID LPAR RPAR {char strAux[1024];
- sprintf(strAux, "Id(%s)", $1);
- $$ = insertNode(strdup(strAux), NULL);}
- | ID LPAR Expr FuncInvocationAux RPAR {}
- | ID LPAR Expr RPAR {}
- | ID LPAR error RPAR {}
- ;
- FuncInvocationAux:
- COMMA Expr {}
- | COMMA Expr FuncInvocationAux {}
- ;
- Expr:
- Expr OR Expr {$$ = insertNode("Or", $1);
- insertBrother($1, $3);}
- | Expr AND Expr {$$ = insertNode("And", $1);
- insertBrother($1, $3);}
- | Expr LT Expr {$$ = insertNode("Lt", $1);
- insertBrother($1, $3);}
- | Expr GT Expr {$$ = insertNode("Gt", $1);
- insertBrother($1, $3);}
- | Expr EQ Expr {$$ = insertNode("Eq", $1);
- insertBrother($1, $3);}
- | Expr NE Expr {$$ = insertNode("Ne", $1);
- insertBrother($1, $3);}
- | Expr LE Expr {$$ = insertNode("Le", $1);
- insertBrother($1, $3);}
- | Expr GE Expr {$$ = insertNode("Ge", $1);
- insertBrother($1, $3);}
- | Expr PLUS Expr {$$ = insertNode("Add", $1);
- insertBrother($1, $3);}
- | Expr MINUS Expr {$$ = insertNode("Sub", $1);
- insertBrother($1, $3);}
- | Expr STAR Expr {$$ = insertNode("Star", $1);
- insertBrother($1, $3);}
- | Expr DIV Expr {$$ = insertNode("Div", $1);
- insertBrother($1, $3);}
- | Expr MOD Expr {$$ = insertNode("Mod", $1);
- insertBrother($1, $3);}
- | NOT Expr {$$ = insertNode("Add", $2);}
- | Expr MINUS %prec NOT {$$ = insertNode("Minus", $1);}
- | Expr PLUS %prec NOT {$$ = insertNode("Plus", $1);}
- | INTLIT {char strAux[1024];
- sprintf(strAux, "INTLIT(%s)", $1);
- $$ = insertNode(strdup(strAux), NULL);}
- | REALLIT {char strAux[1024];
- sprintf(strAux, "REALLIT(%s)", $1);
- $$ = insertNode(strdup(strAux), NULL);}
- | ID {char strAux[1024];
- sprintf(strAux, "Id(%s)", $1);
- $$ = insertNode(strdup(strAux), NULL);}
- | FuncInvocation {$$ = $1;}
- | LPAR Expr RPAR {$$ = $2;}
- | LPAR error RPAR {$$ = insertNode("NULL", NULL);flagError = 1;}
- ;
- %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement