This commit is contained in:
2022-02-21 12:44:43 +01:00
parent 986228c449
commit c61f68540d
21 changed files with 827 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
#define STRING(x) #x
char *node_string[30] = {
STRING(PROGRAM),
STRING(GLOBAL_LIST),
STRING(GLOBAL),
STRING(STATEMENT_LIST),
STRING(PRINT_LIST),
STRING(EXPRESSION_LIST),
STRING(VARIABLE_LIST),
STRING(ARGUMENT_LIST),
STRING(PARAMETER_LIST),
STRING(DECLARATION_LIST),
STRING(FUNCTION),
STRING(STATEMENT),
STRING(BLOCK),
STRING(ASSIGNMENT_STATEMENT),
STRING(ADD_STATEMENT),
STRING(SUBTRACT_STATEMENT),
STRING(MULTIPLY_STATEMENT),
STRING(DIVIDE_STATEMENT),
STRING(RETURN_STATEMENT),
STRING(PRINT_STATEMENT),
STRING(NULL_STATEMENT),
STRING(IF_STATEMENT),
STRING(WHILE_STATEMENT),
STRING(EXPRESSION),
STRING(RELATION),
STRING(DECLARATION),
STRING(PRINT_ITEM),
STRING(IDENTIFIER_DATA),
STRING(NUMBER_DATA),
STRING(STRING_DATA)
};
#undef STRING

View File

@@ -0,0 +1,308 @@
%{
#include <vslc.h>
#define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children)
%}
%define api.value.type {node_t}
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
%token VAR NUMBER IDENTIFIER STRING
%left '|' '&' '^'
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%right '~'
%expect 1
%nonassoc IF THEN
%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 {
root = NODE(PROGRAM, NULL, 1, $1);
}
;
global_list:
global {
$$ = NODE(GLOBAL_LIST, NULL, 1, $1);
}
| global_list global {
$$ = NODE(GLOBAL_LIST, NULL, 2, $1, $2);
}
;
global:
function {
$$ = NODE(GLOBAL, NULL, 1, $1);
}
| declaration {
$$ = NODE(GLOBAL, NULL, 1, $1);
}
;
statement_list:
statement {
$$ = NODE(STATEMENT_LIST, NULL, 1, $1);
}
| statement_list statement {
$$ = NODE(STATEMENT_LIST, NULL, 2, $1, $2);
}
;
print_list:
print_item {
$$ = NODE(PRINT_LIST, NULL, 1, $1);
}
| print_list ',' print_item {
$$ = NODE(PRINT_LIST, NULL, 2, $1, $3);
}
;
expression_list:
expression {
$$ = NODE(EXPRESSION_LIST, NULL, 1, $1);
}
| expression_list ',' expression {
$$ = NODE(EXPRESSION_LIST, NULL, 2, $1, $3);
}
;
variable_list:
identifier {
$$ = NODE(VARIABLE_LIST, NULL, 1, $1);
}
| variable_list ',' identifier {
$$ = NODE(VARIABLE_LIST, NULL, 2, $1, $3);
}
;
argument_list:
expression_list {
$$ = NODE(ARGUMENT_LIST, NULL, 1, $1);
}
| /* epsilon */ {
$$ = NODE(ARGUMENT_LIST, NULL, 0);
}
;
parameter_list:
variable_list {
$$ = NODE(PARAMETER_LIST, NULL, 1, $1);
}
| /* epsilon */ {
$$ = NODE(PARAMETER_LIST, NULL, 0);
}
;
declaration_list:
declaration {
$$ = NODE(DECLARATION_LIST, NULL, 1, $1);
}
| declaration_list declaration {
$$ = NODE(DECLARATION_LIST, NULL, 2, $1, $2);
}
;
function:
FUNC identifier '(' parameter_list ')' statement {
$$ = NODE(FUNCTION, NULL, 3, $2, $4, $6);
}
;
statement:
assignment_statement {
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| 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:
expression '=' expression {
$$ = NODE(RELATION, strdup("="), 2, $1, $3);
}
| expression '<' expression {
$$ = NODE(RELATION, strdup("<"), 2, $1, $3);
}
| expression '>' expression {
$$ = NODE(RELATION, strdup(">"), 2, $1, $3);
}
;
expression:
expression '|' expression {
$$ = NODE(EXPRESSION, strdup("|"), 2, $1, $3);
}
| expression '^' expression {
$$ = NODE(EXPRESSION, strdup("^"), 2, $1, $3);
}
| expression '&' expression {
$$ = NODE(EXPRESSION, strdup("&"), 2, $1, $3);
}
| expression '+' expression {
$$ = NODE(EXPRESSION, strdup("+"), 2, $1, $3);
}
| expression '-' expression {
$$ = NODE(EXPRESSION, strdup("-"), 2, $1, $3);
}
| expression '*' expression {
$$ = NODE(EXPRESSION, strdup("*"), 2, $1, $3);
}
| expression '/' expression {
$$ = NODE(EXPRESSION, strdup("/"), 2, $1, $3);
}
| '-' expression %prec UMINUS {
$$ = NODE(EXPRESSION, strdup("-"), 1, $2);
}
| '~' expression {
$$ = NODE(EXPRESSION, strdup("~"), 1, $2);
}
| '(' expression ')' {
$$ = NODE(EXPRESSION, /*NULL*/ strdup("group"), 1, $2);
}
| number {
$$ = NODE(EXPRESSION, /*NULL*/ strdup("number"), 1, $1);
}
| identifier {
$$ = NODE(EXPRESSION, /*NULL*/ strdup("identifier"), 1, $1);
}
| identifier '(' argument_list ')' {
$$ = 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
yyerror ( const char *error )
{
fprintf ( stderr, "%s on line %d\n", error, yylineno );
exit ( EXIT_FAILURE );
}

View File

@@ -0,0 +1,30 @@
%{
#include <vslc.h>
%}
%option noyywrap
%option array
%option yylineno
WHITESPACE [\ \t\v\r\n]
COMMENT \/\/[^\n]+
QUOTED \"([^\"\n]|\\\")*\"
%%
{WHITESPACE}+ { /* Eliminate whitespace */ }
{COMMENT} { /* Eliminate comments */ }
func { return FUNC; }
print { return PRINT; }
return { return RETURN; }
continue { return CONTINUE; }
if { return IF; }
then { return THEN; }
else { return ELSE; }
while { return WHILE; }
do { return DO; }
begin { return OPENBLOCK; }
end { return CLOSEBLOCK; }
var { return VAR; }
[0-9]+ { return NUMBER; }
[A-Za-z_][0-9A-Za-z_]* { return IDENTIFIER; }
{QUOTED} { return STRING; }
. { return yytext[0]; }
%%

View File

@@ -0,0 +1,175 @@
#include <vslc.h>
static void node_print ( node_t *root, int nesting );
static void simplify_tree ( node_t **simplified, node_t *root );
static void node_finalize ( node_t *discard );
static void destroy_subtree ( node_t *discard );
typedef struct stem_t *stem;
struct stem_t { const char *str; stem next; };
static void
tree_print(node_t* root, stem head)
{
static const char *sdown = "", *slast = "", *snone = " ";
struct stem_t col = {0, 0}, *tail;
for (tail = head; tail; tail = tail->next) {
if (!tail->next) {
if (!strcmp(sdown, tail->str))
printf("");
else
printf("%s", tail->str);
break;
}
printf("%s", tail->str);
}
printf("──%s", node_string[root->type] );
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION )
printf("(%s)", (char *) root->data);
else if (root->type == NUMBER_DATA)
printf ( "(%ld)", *((int64_t *)root->data) );
putchar ( '\n' );
if (!root->n_children) return;
if (tail && tail->str == slast)
tail->str = snone;
if (!tail) tail = head = &col;
else tail->next = &col;
for ( int64_t i=0; i < root->n_children; i++ ) {
col.str = root->n_children - i - 1 ? sdown : slast;
tree_print(root->children[i], head);
}
tail->next = 0;
}
/* External interface */
void
destroy_syntax_tree ( void )
{
destroy_subtree ( root );
}
void
simplify_syntax_tree ( void )
{
simplify_tree ( &root, root );
}
void
print_syntax_tree ( void )
{
node_print ( root, 0 );
//tree_print(root, 0);
}
void
node_init (node_t *nd, node_index_t type, void *data, uint64_t n_children, ...)
{
va_list child_list;
*nd = (node_t) {
.type = type,
.data = data,
.entry = NULL,
.n_children = n_children,
.children = (node_t **) malloc ( n_children * sizeof(node_t *) )
};
va_start ( child_list, n_children );
for ( uint64_t i=0; i<n_children; i++ )
nd->children[i] = va_arg ( child_list, node_t * );
va_end ( child_list );
}
/* Internal choices */
static void
node_print ( node_t *root, int nesting )
{
if ( root != NULL )
{
printf ( "%*c%s", nesting, ' ', node_string[root->type] );
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION )
printf ( "(%s)", (char *) root->data );
else if ( root->type == NUMBER_DATA )
printf ( "(%ld)", *((int64_t *)root->data) );
putchar ( '\n' );
for ( int64_t i=0; i<root->n_children; i++ )
node_print ( root->children[i], nesting+1 );
}
else
printf ( "%*c%p\n", nesting, ' ', root );
}
static void
node_finalize ( node_t *discard )
{
if ( discard != NULL )
{
free ( discard->data );
free ( discard->children );
free ( discard );
}
}
static void
destroy_subtree ( node_t *discard )
{
if ( discard != NULL )
{
for ( uint64_t i=0; i<discard->n_children; i++ )
destroy_subtree ( discard->children[i] );
node_finalize ( discard );
}
}
static void
simplify_tree ( node_t **simplified, node_t *root )
{
/* TODO: Simplify the syntax tree structure
1. prune children: Delete nodes which can only ever have 1 child and no
meaningful data, and associate their child directly with their parent.
2. resolve constant expressions: Compute the value of subtrees representing
arithmetic with constants, and replace them with their value.
3. flatten: Delete internal nodes of list structures, leaving only a parent
node with a list type, and all list items as its children. Print list items
can be associated directly with the print statement.
VARIABLE_LIST VARIABLE_LIST
VARIABLE_LIST IDENTIFIER_DATA(i)
VARIABLE_LIST IDENTIFIER_DATA(j)
VARIABLE_LIST IDENTIFIER_DATA(k)
VARIABLE_LIST IDENTIFIER_DATA(l)
IDENTIFIER_DATA(i) becomes IDENTIFIER_DATA(m)
IDENTIFIER_DATA(j)
IDENTIFIER_DATA(k)
IDENTIFIER_DATA(l)
IDENTIFIER_DATA(m)
Tip: implement these three steps as separate functions to complete one task
at the time. e.g.:
prune_children(root);
redolve_constant_expressions(root);
flatten(root);
simplified = &root->children[0];
node_finalize(root);
*/
}

View File

@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <vslc.h>
/* Global state */
node_t *root; // Syntax tree
/* Command line option parsing for the main function */
static void options ( int argc, char **argv );
bool
print_full_tree = false,
print_simplified_tree = false;
/* Entry point */
int
main ( int argc, char **argv )
{
options ( argc, argv );
yyparse(); // Generated from grammar/bison, constructs syntax tree
if ( print_full_tree )
print_syntax_tree ();
simplify_syntax_tree (); // In tree.c
if ( print_simplified_tree )
print_syntax_tree ();
destroy_syntax_tree (); // In tree.c
}
static const char *usage =
"Command line options\n"
"\t-h\tOutput this text and halt\n"
"\t-t\tOutput the full syntax tree\n"
"\t-T\tOutput the simplified syntax tree\n";
static void
options ( int argc, char **argv )
{
int o;
while ( (o=getopt(argc,argv,"htT")) != -1 )
{
switch ( o )
{
case 'h':
printf ( "%s:\n%s", argv[0], usage );
exit ( EXIT_FAILURE );
break;
case 't': print_full_tree = true; break;
case 'T': print_simplified_tree = true; break;
}
}
}