167 lines
4.2 KiB
Plaintext
167 lines
4.2 KiB
Plaintext
%{
|
|
#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;
|
|
%}
|
|
|
|
%token NUMBER
|
|
%token INT
|
|
%token UINT
|
|
%token FLOAT
|
|
%token DOUBLE
|
|
%token STRING
|
|
%token BOOL
|
|
%token ID
|
|
%token STRING_LITERAL
|
|
%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
|
|
|
|
%parse-param { FILE* fp }
|
|
|
|
%left PLUS MINUS
|
|
%left BANG
|
|
%left LEFT_PAREN RIGHT_PAREN
|
|
|
|
%%
|
|
Program : Statements
|
|
{
|
|
for (i = 0; i < statementCount; i += 1)
|
|
{
|
|
PrintTree(statements[i], 0);
|
|
}
|
|
}
|
|
|
|
Identifier : ID
|
|
{
|
|
$$ = MakeIdentifierNode(yytext);
|
|
}
|
|
|
|
PrimaryExpression : Identifier
|
|
| 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
|
|
;
|
|
|
|
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;
|
|
}
|
|
%%
|
|
|
|
#include "lex.yy.c"
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if (argc < 2)
|
|
{
|
|
printf("Please provide a file.\n");
|
|
return 1;
|
|
}
|
|
|
|
FILE *fp = fopen(argv[1], "r");
|
|
yyin = fp;
|
|
yyparse(fp);
|
|
fclose(fp);
|
|
return 0;
|
|
}
|