added my own from ps3

main
Øyvind Skaaden 2022-03-12 19:21:38 +01:00
parent 6856407f38
commit a5f6aaf1fa
5 changed files with 298 additions and 161 deletions

View File

@ -10,8 +10,16 @@ typedef struct n {
struct n **children; struct n **children;
} node_t; } node_t;
// Export the initializer function, it is needed by the parser /**Export the initializer function, it is needed by the parser
void node_init ( * @param *nd node to initialize
* @param type type of node (see nodetype.h)
* @param *data associated data. Declared void to allow any type
* @param n_children number of children
* @param ... variable argument list of child nodes (node_t *)
*
* @return Pointer to the initialized node
* */
node_t* node_init (
node_t *nd, node_index_t type, void *data, uint64_t n_children, ... node_t *nd, node_index_t type, void *data, uint64_t n_children, ...
); );

View File

@ -25,6 +25,7 @@ int yyerror ( const char *error );
/* These are defined in the parser generated by bison */ /* These are defined in the parser generated by bison */
extern int yylineno; extern int yylineno;
extern int yylex ( void ); extern int yylex ( void );
extern int yylex_destroy( void );
extern char yytext[]; extern char yytext[];
/* Global state */ /* Global state */

View File

@ -1,176 +1,303 @@
%{ %{
#include <vslc.h> #include <vslc.h>
#define N0C(n,t,d) do { \ #define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children)
node_init ( n = malloc(sizeof(node_t)), t, d, 0 ); \
} while ( false )
#define N1C(n,t,d,a) do { \
node_init ( n = malloc(sizeof(node_t)), t, d, 1, a ); \
} while ( false )
#define N2C(n,t,d,a,b) do { \
node_init ( n = malloc(sizeof(node_t)), t, d, 2, a, b ); \
} while ( false )
#define N3C(n,t,d,a,b,c) do { \
node_init ( n = malloc(sizeof(node_t)), t, d, 3, a, b, c ); \
} while ( false )
%} %}
%left '|' %define api.value.type {node_t}
%left '^' %token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
%left '&' %token VAR NUMBER IDENTIFIER STRING
%left '|' '&' '^'
%left '+' '-' %left '+' '-'
%left '*' '/' %left '*' '/'
%nonassoc UMINUS %nonassoc UMINUS
%right '~' %right '~'
%expect 1 //%expect 1
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK %nonassoc IF THEN
%token VAR NUMBER IDENTIFIER STRING %nonassoc ELSE
/* Tried fixing vscode complaining about the type for the non-terminals, didn't work
%union {
node_t* node;
}
%type <node> global_list global
%type <node> statement_list print_list expression_list variable_list argument_list parameter_list declaration_list
%type <node> function statement block
%type <node> assignment_statement return_statement print_statement null_statement if_statement while_statement
%type <node> relation expression declaration print_item identifier number string
*/
%% %%
program :
global_list { N1C ( root, PROGRAM, NULL, $1 ); } program:
; global_list {
global_list : root = NODE(PROGRAM, NULL, 1, $1);
global { N1C ( $$, GLOBAL_LIST, NULL, $1 ); } }
| global_list global { N2C ( $$, GLOBAL_LIST, NULL, $1, $2 ); } ;
;
global_list:
global {
$$ = NODE(GLOBAL_LIST, NULL, 1, $1);
}
| global_list global {
$$ = NODE(GLOBAL_LIST, NULL, 2, $1, $2);
}
;
global: global:
function { N1C ( $$, GLOBAL, NULL, $1 ); } function {
| declaration { N1C ( $$, GLOBAL, NULL, $1 ); } $$ = NODE(GLOBAL, NULL, 1, $1);
; }
statement_list : | declaration {
statement { N1C ( $$, STATEMENT_LIST, NULL, $1 ); } $$ = NODE(GLOBAL, NULL, 1, $1);
| statement_list statement { N2C ( $$, STATEMENT_LIST, NULL, $1, $2 ); } }
; ;
print_list :
print_item { N1C ( $$, PRINT_LIST, NULL, $1 ); } statement_list:
| print_list ',' print_item { N2C ( $$, PRINT_LIST, NULL, $1, $3 ); } statement {
; $$ = NODE(STATEMENT_LIST, NULL, 1, $1);
expression_list : }
expression { N1C ( $$, EXPRESSION_LIST, NULL, $1 ); } | statement_list statement {
| expression_list ',' expression { N2C($$, EXPRESSION_LIST, NULL, $1, $3); } $$ = NODE(STATEMENT_LIST, NULL, 2, $1, $2);
; }
variable_list : ;
identifier { N1C ( $$, VARIABLE_LIST, NULL, $1 ); }
| variable_list ',' identifier { N2C ( $$, VARIABLE_LIST, NULL, $1, $3 ); } print_list:
; print_item {
argument_list : $$ = NODE(PRINT_LIST, NULL, 1, $1);
expression_list { N1C ( $$, ARGUMENT_LIST, NULL, $1 ); } }
| /* epsilon */ { $$ = NULL; } | print_list ',' print_item {
; $$ = NODE(PRINT_LIST, NULL, 2, $1, $3);
parameter_list : }
variable_list { N1C ( $$, PARAMETER_LIST, NULL, $1 ); } ;
| /* epsilon */ { $$ = NULL; }
; expression_list:
declaration_list : expression {
declaration { N1C ( $$, DECLARATION_LIST, NULL, $1 ); } $$ = NODE(EXPRESSION_LIST, NULL, 1, $1);
| declaration_list declaration { N2C ($$, DECLARATION_LIST, NULL, $1, $2); } }
; | expression_list ',' expression {
function : $$ = NODE(EXPRESSION_LIST, NULL, 2, $1, $3);
FUNC identifier '(' parameter_list ')' statement }
{ N3C ( $$, FUNCTION, NULL, $2, $4, $6 ); } ;
;
statement : variable_list:
assignment_statement { N1C ( $$, STATEMENT, NULL, $1 ); } identifier {
| return_statement { N1C ( $$, STATEMENT, NULL, $1 ); } $$ = NODE(VARIABLE_LIST, NULL, 1, $1);
| print_statement { N1C ( $$, STATEMENT, NULL, $1 ); } }
| if_statement { N1C ( $$, STATEMENT, NULL, $1 ); } | variable_list ',' identifier {
| while_statement { N1C ( $$, STATEMENT, NULL, $1 ); } $$ = NODE(VARIABLE_LIST, NULL, 2, $1, $3);
| null_statement { N1C ( $$, STATEMENT, NULL, $1 ); } }
| block { N1C ( $$, STATEMENT, NULL, $1 ); } ;
;
block : argument_list:
OPENBLOCK declaration_list statement_list CLOSEBLOCK expression_list {
{ N2C ($$, BLOCK, NULL, $2, $3); } $$ = NODE(ARGUMENT_LIST, NULL, 1, $1);
| OPENBLOCK statement_list CLOSEBLOCK { N1C ($$, BLOCK, NULL, $2 ); } }
; | /* epsilon */ {
assignment_statement : $$ = NODE(ARGUMENT_LIST, NULL, 0);
identifier ':' '=' expression }
{ N2C ( $$, ASSIGNMENT_STATEMENT, NULL, $1, $4 ); } ;
| identifier '+' '=' expression
{ N2C ( $$, ADD_STATEMENT, NULL, $1, $4 ); } parameter_list:
| identifier '-' '=' expression variable_list {
{ N2C ( $$, SUBTRACT_STATEMENT, NULL, $1, $4 ); } $$ = NODE(PARAMETER_LIST, NULL, 1, $1);
| identifier '*' '=' expression }
{ N2C ( $$, MULTIPLY_STATEMENT, NULL, $1, $4 ); } | /* epsilon */ {
| identifier '/' '=' expression $$ = NODE(PARAMETER_LIST, NULL, 0);
{ N2C ( $$, DIVIDE_STATEMENT, NULL, $1, $4 ); } }
; ;
return_statement :
RETURN expression declaration_list:
{ N1C ( $$, RETURN_STATEMENT, NULL, $2 ); } declaration {
; $$ = NODE(DECLARATION_LIST, NULL, 1, $1);
print_statement : }
PRINT print_list | declaration_list declaration {
{ N1C ( $$, PRINT_STATEMENT, NULL, $2 ); } $$ = NODE(DECLARATION_LIST, NULL, 2, $1, $2);
; }
null_statement : ;
CONTINUE
{ N0C ( $$, NULL_STATEMENT, NULL ); } function:
; FUNC identifier '(' parameter_list ')' statement {
if_statement : $$ = NODE(FUNCTION, NULL, 3, $2, $4, $6);
IF relation THEN statement }
{ N2C ( $$, IF_STATEMENT, NULL, $2, $4 ); } ;
| IF relation THEN statement ELSE statement
{ N3C ( $$, IF_STATEMENT, NULL, $2, $4, $6 ); } statement:
; assignment_statement {
while_statement : $$ = NODE(STATEMENT, NULL, 1, $1);
WHILE relation DO statement }
{ N2C ( $$, WHILE_STATEMENT, NULL, $2, $4 ); } | return_statement {
; $$ = NODE(STATEMENT, NULL, 1, $1);
}
| print_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| if_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| while_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| null_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| block {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
;
block:
OPENBLOCK declaration_list statement_list CLOSEBLOCK {
$$ = NODE(BLOCK, NULL, 2, $2, $3);
}
| OPENBLOCK statement_list CLOSEBLOCK {
$$ = NODE(BLOCK, NULL, 1, $2);
}
;
assignment_statement:
identifier ':' '=' expression {
$$ = NODE(ASSIGNMENT_STATEMENT, NULL, 2, $1, $4);
}
| identifier '+' '=' expression {
$$ = NODE(ADD_STATEMENT, NULL, 2, $1, $4);
}
| identifier '-' '=' expression {
$$ = NODE(SUBTRACT_STATEMENT, NULL, 2, $1, $4);
}
| identifier '*' '=' expression {
$$ = NODE(MULTIPLY_STATEMENT, NULL, 2, $1, $4);
}
| identifier '/' '=' expression {
$$ = NODE(DIVIDE_STATEMENT, NULL, 2, $1, $4);
}
;
return_statement:
RETURN expression {
$$ = NODE(RETURN_STATEMENT, NULL, 1, $2);
}
;
print_statement:
PRINT print_list {
$$ = NODE(PRINT_STATEMENT, NULL, 1, $2);
}
;
null_statement:
CONTINUE {
$$ = NODE(NULL_STATEMENT, NULL, 0);
}
;
if_statement:
IF relation THEN statement {
$$ = NODE(IF_STATEMENT, NULL, 2, $2, $4);
}
| IF relation THEN statement ELSE statement {
$$ = NODE(IF_STATEMENT, NULL, 3, $2, $4, $6);
}
;
while_statement:
WHILE relation DO statement {
$$ = NODE(WHILE_STATEMENT, NULL, 2, $2, $4);
}
;
relation: relation:
expression '=' expression expression '=' expression {
{ N2C ( $$, RELATION, strdup("="), $1, $3 ); } $$ = NODE(RELATION, strdup("="), 2, $1, $3);
| expression '<' expression }
{ N2C ( $$, RELATION, strdup("<"), $1, $3 ); } | expression '<' expression {
| expression '>' expression $$ = NODE(RELATION, strdup("<"), 2, $1, $3);
{ N2C ( $$, RELATION, strdup(">"), $1, $3 ); } }
; | expression '>' expression {
expression : $$ = NODE(RELATION, strdup(">"), 2, $1, $3);
expression '|' expression }
{ N2C ( $$, EXPRESSION, strdup("|"), $1, $3 ); } ;
| expression '^' expression
{ N2C ( $$, EXPRESSION, strdup("^"), $1, $3 ); } expression:
| expression '&' expression expression '|' expression {
{ N2C ( $$, EXPRESSION, strdup("&"), $1, $3 ); } $$ = NODE(EXPRESSION, strdup("|"), 2, $1, $3);
| expression '+' expression }
{ N2C ( $$, EXPRESSION, strdup("+"), $1, $3 ); } | expression '^' expression {
| expression '-' expression $$ = NODE(EXPRESSION, strdup("^"), 2, $1, $3);
{ N2C ( $$, EXPRESSION, strdup("-"), $1, $3 ); } }
| expression '*' expression | expression '&' expression {
{ N2C ( $$, EXPRESSION, strdup("*"), $1, $3 ); } $$ = NODE(EXPRESSION, strdup("&"), 2, $1, $3);
| expression '/' expression }
{ N2C ( $$, EXPRESSION, strdup("/"), $1, $3 ); } | expression '+' expression {
| '-' expression %prec UMINUS $$ = NODE(EXPRESSION, strdup("+"), 2, $1, $3);
{ N1C ( $$, EXPRESSION, strdup("-"), $2 ); } }
| '~' expression %prec UMINUS | expression '-' expression {
{ N1C ( $$, EXPRESSION, strdup("~"), $2 ); } $$ = NODE(EXPRESSION, strdup("-"), 2, $1, $3);
| '(' expression ')' { $$ = $2; } }
| number { N1C ( $$, EXPRESSION, NULL, $1 ); } | expression '*' expression {
| identifier $$ = NODE(EXPRESSION, strdup("*"), 2, $1, $3);
{ N1C ( $$, EXPRESSION, NULL, $1 ); } }
| identifier '(' argument_list ')' | expression '/' expression {
{ N2C ( $$, EXPRESSION, NULL, $1, $3 ); } $$ = NODE(EXPRESSION, strdup("/"), 2, $1, $3);
; }
declaration : | '-' expression %prec UMINUS {
VAR variable_list { N1C ( $$, DECLARATION, NULL, $2 ); } $$ = NODE(EXPRESSION, strdup("-"), 1, $2);
; }
print_item : | '~' expression {
expression $$ = NODE(EXPRESSION, strdup("~"), 1, $2);
{ N1C ( $$, PRINT_ITEM, NULL, $1 ); } }
| string | '(' expression ')' {
{ N1C ( $$, PRINT_ITEM, NULL, $1 ); } $$ = NODE(EXPRESSION, /*NULL*/ strdup("group"), 1, $2);
; }
identifier: IDENTIFIER { N0C($$, IDENTIFIER_DATA, strdup(yytext) ); } | number {
number: NUMBER $$ = NODE(EXPRESSION, /*NULL*/ strdup("number"), 1, $1);
{ }
int64_t *value = malloc ( sizeof(int64_t) ); | identifier {
*value = strtol ( yytext, NULL, 10 ); $$ = NODE(EXPRESSION, /*NULL*/ strdup("identifier"), 1, $1);
N0C($$, NUMBER_DATA, value ); }
} | identifier '(' argument_list ')' {
string: STRING { N0C($$, STRING_DATA, strdup(yytext) ); } $$ = NODE(EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3);
}
;
declaration:
VAR variable_list {
$$ = NODE(DECLARATION, NULL, 1, $2);
}
;
print_item:
expression {
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
}
| string {
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
}
;
identifier:
IDENTIFIER {
$$ = NODE(IDENTIFIER_DATA, strdup(yytext), 0); // Zero children
}
;
number:
NUMBER {
uint64_t* p_number = malloc(sizeof(uint64_t));
*p_number = strtol(yytext, NULL, 10);
$$ = NODE(NUMBER_DATA, p_number, 0); // Zero children
}
;
string:
STRING {
$$ = NODE(STRING_DATA, strdup(yytext), 0); // Zero children
}
;
%% %%
int int

View File

@ -28,6 +28,7 @@ main ( int argc, char **argv )
options ( argc, argv ); options ( argc, argv );
yyparse(); // Generated from grammar/bison, constructs syntax tree yyparse(); // Generated from grammar/bison, constructs syntax tree
yylex_destroy(); // Free heap used by flex
if ( print_full_tree ) if ( print_full_tree )
print_syntax_tree (); print_syntax_tree ();