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 * id;
- char * type;
- struct node * nodeSon;
- struct node * nodeBrother;
- } node;
- node * nodeAux;
- node * insertNode(char * id, char * type, node * nodeSon) {
- node * auxNode = (node *)malloc(sizeof(node));
- auxNode->type = type;
- auxNode->id = id;
- 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) {
- printPoints(points);
- printf("%s\n", node->type);
- printTree(node->nodeSon, points+2);
- node = node->nodeBrother;
- }
- }
- void linkBrother(node * node1, node * node2) {
- node1->nodeBrother = node2;
- }
- %}
- %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
- %token SEMICOLON
- %token BLANKID
- %token PACKAGE
- %token RETURN
- %token AND
- %token STAR
- %token COMMA
- %token DIV
- %token ASSIGN
- %token EQ
- %token GE
- %token GT
- %token LBRACE
- %token LE
- %token LPAR
- %token LSQ
- %token LT
- %token MINUS
- %token MOD
- %token NE
- %token NOT
- %token OR
- %token PLUS
- %token RBRACE
- %token RPAR
- %token RSQ
- %token ELSE
- %token FOR
- %token IF
- %token VAR
- %token INT
- %token FLOAT32
- %token BOOL
- %token STRING
- %token PRINT
- %token PARSEINT
- %token FUNC
- %token CMDARGS
- %token RESERVED
- %token 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(NULL, "Program", $4); if(flagError==0)printTree($$, 0);}
- ;
- Declarations: %empty {$$ = insertNode(NULL, "NULL", NULL);}
- | VarDeclaration SEMICOLON Declarations {//printf("%s\n", $1->type);
- $$ = insertNode(NULL, "VarDecl", $1);
- linkBrother($1, $3);
- nodeAux = $3;
- while (nodeAux->nodeBrother!=NULL) {
- printf("type: %s\n", nodeAux->type);
- nodeAux = nodeAux->nodeBrother;
- }
- }
- | FuncDeclaration SEMICOLON Declarations {linkBrother($1, $3); $$ = $1;}
- ;
- VarDeclaration:
- VAR VarSpec {$$ = $2;}
- | VAR LPAR VarSpec SEMICOLON RPAR {$$ = insertNode(NULL, "VarDecl", $3);}
- ;
- VarSpec:
- ID Type {$$ = insertNode($1, "Id", NULL);}
- | ID VarSpecAux Type {$$ = insertNode($1, "Id", NULL);}
- ;
- VarSpecAux:
- COMMA ID {$$ = insertNode(NULL, "Var", NULL);}
- | COMMA ID VarSpecAux {$$ = insertNode(NULL, "Var", NULL);
- nodeAux = $$;
- while (nodeAux->nodeBrother != NULL) {
- nodeAux = nodeAux->nodeBrother;
- }
- nodeAux->nodeBrother = $3;
- }
- ;
- Type:
- INT {$$ = insertNode(NULL, "Int", NULL);}
- | FLOAT32 {$$ = insertNode(NULL, "Float32", NULL);}
- | BOOL {$$ = insertNode(NULL, "Bool", NULL);}
- | STRING {$$ = insertNode(NULL, "String", NULL);}
- ;
- FuncDeclaration:
- FUNC ID LPAR RPAR FuncBody
- | FUNC ID LPAR Parameters RPAR FuncBody
- | FUNC ID LPAR RPAR Type FuncBody
- | FUNC ID LPAR Parameters RPAR Type FuncBody
- ;
- Parameters:
- ID Type
- | ID Type ParametersAux
- ;
- ParametersAux:
- COMMA ID Type
- | COMMA ID Type ParametersAux
- ;
- FuncBody:
- LBRACE VarsAndStatements RBRACE
- ;
- VarsAndStatements: %empty
- | VarsAndStatements SEMICOLON
- | VarsAndStatements VarDeclaration SEMICOLON
- | VarsAndStatements Statement SEMICOLON
- ;
- 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
- | ParseArgs
- | PRINT LPAR Expr RPAR
- | PRINT LPAR STRLIT RPAR
- | error {printf("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
- | ID LPAR Expr FuncInvocationAux RPAR
- | ID LPAR Expr RPAR
- | ID LPAR error RPAR
- ;
- FuncInvocationAux:
- COMMA Expr
- | COMMA Expr FuncInvocationAux
- ;
- Expr:
- Expr OR Expr
- | Expr AND Expr
- | Expr LT Expr
- | Expr GT Expr
- | Expr EQ Expr
- | Expr NE Expr
- | Expr LE Expr
- | Expr GE Expr
- | Expr PLUS Expr
- | Expr MINUS Expr
- | Expr STAR Expr
- | Expr DIV Expr
- | Expr MOD Expr
- | NOT Expr
- | MINUS Expr NOT
- | PLUS Expr NOT
- | INTLIT
- | REALLIT
- | ID
- | FuncInvocation
- | LPAR Expr RPAR
- | LPAR error RPAR
- ;
- %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement