2021-04-16 21:40:28 +00:00
|
|
|
%{
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "ast.h"
|
|
|
|
#define YYSTYPE struct Node*
|
|
|
|
void yyerror(FILE *fp, char *s)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "%s\n", s);
|
|
|
|
}
|
|
|
|
Node **statements;
|
|
|
|
uint32_t statementCount;
|
|
|
|
uint32_t i;
|
|
|
|
%}
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
%token NUMBER
|
2021-04-16 21:40:28 +00:00
|
|
|
%token INT
|
|
|
|
%token UINT
|
|
|
|
%token FLOAT
|
|
|
|
%token DOUBLE
|
2021-04-16 07:35:35 +00:00
|
|
|
%token STRING
|
2021-04-16 21:40:28 +00:00
|
|
|
%token BOOL
|
|
|
|
%token ID
|
|
|
|
%token STRING_LITERAL
|
2021-04-16 07:35:35 +00:00
|
|
|
%token PLUS
|
|
|
|
%token MINUS
|
|
|
|
%token MULTIPLY
|
|
|
|
%token DIVIDE
|
|
|
|
%token MOD
|
|
|
|
%token EQUAL
|
|
|
|
%token LESS_THAN
|
|
|
|
%token GREATER_THAN
|
|
|
|
%token QUOTE
|
|
|
|
%token BANG
|
|
|
|
%token BAR
|
|
|
|
%token AMPERSAND
|
|
|
|
%token POINT
|
|
|
|
%token COMMA
|
|
|
|
%token SEMICOLON
|
|
|
|
%token COLON
|
|
|
|
%token QUESTION
|
|
|
|
%token LEFT_PAREN
|
|
|
|
%token RIGHT_PAREN
|
|
|
|
%token LEFT_BRACE
|
|
|
|
%token RIGHT_BRACE
|
|
|
|
%token LEFT_BRACKET
|
|
|
|
%token RIGHT_BRACKET
|
|
|
|
%token COMMENT
|
|
|
|
%token NEWLINE
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
%parse-param { FILE* fp }
|
|
|
|
|
2021-04-16 07:35:35 +00:00
|
|
|
%left PLUS MINUS
|
|
|
|
%left BANG
|
|
|
|
%left LEFT_PAREN RIGHT_PAREN
|
|
|
|
|
|
|
|
%%
|
2021-04-16 21:40:28 +00:00
|
|
|
Program : Statements
|
2021-04-16 07:35:35 +00:00
|
|
|
{
|
2021-04-16 21:40:28 +00:00
|
|
|
for (i = 0; i < statementCount; i += 1)
|
|
|
|
{
|
|
|
|
PrintTree(statements[i], 0);
|
|
|
|
}
|
2021-04-16 07:35:35 +00:00
|
|
|
}
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
Identifier : ID
|
2021-04-16 07:35:35 +00:00
|
|
|
{
|
|
|
|
$$ = MakeIdentifierNode(yytext);
|
|
|
|
}
|
2021-04-16 21:40:28 +00:00
|
|
|
|
|
|
|
PrimaryExpression : Identifier
|
2021-04-16 07:35:35 +00:00
|
|
|
| NUMBER
|
|
|
|
{
|
|
|
|
$$ = MakeNumberNode(yytext);
|
|
|
|
}
|
|
|
|
| STRING
|
|
|
|
{
|
|
|
|
$$ = MakeStringNode(yytext);
|
|
|
|
}
|
|
|
|
| LEFT_PAREN Expression RIGHT_PAREN
|
|
|
|
;
|
|
|
|
|
|
|
|
UnaryExpression : BANG Expression
|
|
|
|
{
|
|
|
|
$$ = MakeUnaryNode(Negate, $2);
|
|
|
|
}
|
|
|
|
|
|
|
|
BinaryExpression : Expression PLUS Expression
|
|
|
|
{
|
|
|
|
$$ = MakeBinaryNode(Add, $1, $3);
|
|
|
|
}
|
|
|
|
| Expression MINUS Expression
|
|
|
|
{
|
|
|
|
$$ = MakeBinaryNode(Subtract, $1, $3);
|
|
|
|
}
|
|
|
|
|
|
|
|
Expression : PrimaryExpression
|
|
|
|
| UnaryExpression
|
|
|
|
| BinaryExpression
|
|
|
|
;
|
2021-04-16 21:40:28 +00:00
|
|
|
|
|
|
|
VariableDeclaration : INT ID
|
|
|
|
{
|
|
|
|
$$ = MakeDeclarationNode(Int, yytext);
|
|
|
|
}
|
|
|
|
| UINT ID
|
|
|
|
{
|
|
|
|
$$ = MakeDeclarationNode(UInt, yytext);
|
|
|
|
}
|
|
|
|
| FLOAT ID
|
|
|
|
{
|
|
|
|
$$ = MakeDeclarationNode(Float, yytext);
|
|
|
|
}
|
|
|
|
| DOUBLE ID
|
|
|
|
{
|
|
|
|
$$ = MakeDeclarationNode(Double, yytext);
|
|
|
|
}
|
|
|
|
| STRING ID
|
|
|
|
{
|
|
|
|
$$ = MakeDeclarationNode(String, yytext);
|
|
|
|
}
|
|
|
|
| BOOL ID
|
|
|
|
{
|
|
|
|
$$ = MakeDeclarationNode(Bool, yytext);
|
|
|
|
}
|
|
|
|
|
|
|
|
AssignmentStatement : VariableDeclaration EQUAL Expression
|
|
|
|
{
|
|
|
|
$$ = MakeAssignmentNode($1, $3);
|
|
|
|
}
|
|
|
|
| Identifier EQUAL Expression
|
|
|
|
{
|
|
|
|
$$ = MakeAssignmentNode($1, $3);
|
|
|
|
}
|
|
|
|
|
|
|
|
Statement : AssignmentStatement
|
|
|
|
;
|
|
|
|
|
|
|
|
CompleteStatement : Statement SEMICOLON;
|
|
|
|
|
|
|
|
Statements : Statements CompleteStatement
|
|
|
|
{
|
|
|
|
statements = realloc(statements, statementCount + 1);
|
|
|
|
statements[statementCount] = $2;
|
|
|
|
statementCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
{
|
|
|
|
$$ = NULL;
|
|
|
|
}
|
2021-04-16 07:35:35 +00:00
|
|
|
%%
|
|
|
|
|
|
|
|
#include "lex.yy.c"
|
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
int main(int argc, char *argv[])
|
2021-04-16 07:35:35 +00:00
|
|
|
{
|
2021-04-16 21:40:28 +00:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
printf("Please provide a file.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2021-04-16 07:35:35 +00:00
|
|
|
|
2021-04-16 21:40:28 +00:00
|
|
|
FILE *fp = fopen(argv[1], "r");
|
|
|
|
yyin = fp;
|
|
|
|
yyparse(fp);
|
|
|
|
fclose(fp);
|
|
|
|
return 0;
|
2021-04-16 07:35:35 +00:00
|
|
|
}
|