Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include "ast.h"
- void yyerror(char *s);
- int yylex();
- //node_type* root = NULL;
- //node_type* aux_node = NULL;
- %}
- %union{
- char* token;
- struct node_type* node;
- }
- %token SEMICOLON BLANKID PACKAGE RETURN AND STAR ASSIGN COMMA DIV EQ GT GE LBRACE LE LPAR
- %token LSQ LT MINUS MOD NE NOT OR PLUS RBRACE RPAR RSQ ELSE FOR IF VAR INT FLOAT32 BOOL
- %token STRING PRINT PARSEINT FUNC CMDARGS
- %token <token> RESERVED ID INTLIT REALLIT STRLIT
- %type <node> Program Declarations VarDeclaration VarSpec Type FuncDeclaration Parameters
- %type <node> FuncBody VarsAndStatements Statement ParseArgs FuncInvocation Expr
- %type <node> auxVS auxPar auxStat auxFI
- %nonassoc ELSE
- %left COMMA
- %right ASSIGN
- %left OR
- %left AND
- %left EQ NE
- %left LT LE GE GT
- %left PLUS MINUS
- %left STAR DIV MOD
- %right NOT
- %%
- Program:
- PACKAGE ID SEMICOLON Declarations {;}
- ;
- Declarations: /*empty*/ {;}
- | Declarations VarDeclaration SEMICOLON {;}
- | Declarations FuncDeclaration SEMICOLON {;}
- ;
- VarDeclaration:
- VAR VarSpec {;}
- | VAR LPAR VarSpec SEMICOLON RPAR {;}
- ;
- VarSpec:
- ID auxVS Type {;}
- ;
- auxVS: /*empty*/ {;}
- | auxVS COMMA ID {;}
- ;
- Type:
- INT {;}
- | FLOAT32 {;}
- | BOOL {;}
- | STRING {;}
- ;
- FuncDeclaration:
- FUNC ID LPAR Parameters RPAR Type FuncBody {;}
- | FUNC ID LPAR RPAR Type FuncBody {;}
- | FUNC ID LPAR Parameters RPAR FuncBody {;}
- | FUNC ID LPAR RPAR FuncBody {;}
- ;
- Parameters:
- ID Type auxPar {;}
- ;
- auxPar: /*empty*/ {;}
- | auxPar COMMA ID Type {;}
- ;
- FuncBody:
- LBRACE VarsAndStatements RBRACE {;}
- ;
- VarsAndStatements: {;}
- | VarsAndStatements VarDeclaration SEMICOLON {;}
- | VarsAndStatements Statement SEMICOLON {;}
- | VarsAndStatements SEMICOLON {;}
- ;
- Statement:
- error {;}
- | ID ASSIGN Expr {;}
- | LBRACE auxStat RBRACE {;}
- | IF Expr LBRACE auxStat RBRACE ELSE LBRACE auxStat RBRACE {;}
- | IF Expr LBRACE auxStat RBRACE {;}
- | FOR Expr LBRACE auxStat RBRACE {;}
- | FOR LBRACE auxStat RBRACE {;}
- | RETURN Expr {;}
- | RETURN {;}
- | FuncInvocation {;}
- | ParseArgs {;}
- | PRINT LPAR Expr RPAR {;}
- | PRINT LPAR STRLIT RPAR {;}
- ;
- auxStat: /*empty*/ {;}
- | auxStat Statement SEMICOLON {;}
- ;
- ParseArgs:
- ID COMMA BLANKID ASSIGN PARSEINT LPAR CMDARGS LSQ Expr RSQ RPAR {;}
- | ID COMMA BLANKID ASSIGN PARSEINT LPAR error RPAR {;}
- ;
- FuncInvocation:
- ID LPAR Expr auxFI RPAR {;}
- | ID LPAR RPAR {;}
- | ID LPAR error RPAR {;}
- ;
- auxFI: /*empty*/ {;}
- | auxFI COMMA Expr {;}
- ;
- 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 {;}
- | PLUS Expr {;}
- | INTLIT {;}
- | REALLIT {;}
- | ID {;}
- | FuncInvocation {;}
- | LPAR Expr RPAR {;}
- | LPAR error RPAR {;}
- ;
- %%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement