main
Øyvind Skaaden 2022-02-21 12:44:43 +01:00
parent 986228c449
commit c61f68540d
21 changed files with 827 additions and 0 deletions

Binary file not shown.

BIN
exercises/03/PS3.pdf Normal file

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,12 @@
LEX=flex
YACC=bison
YFLAGS+=--defines=src/y.tab.h -o y.tab.c
CFLAGS+=-std=c99 -g -Isrc -Iinclude -D_POSIX_C_SOURCE=200809L -DYYSTYPE="node_t *"
src/vslc: src/vslc.c src/parser.o src/scanner.o src/nodetypes.o src/tree.o
src/y.tab.h: src/parser.c
src/scanner.c: src/y.tab.h src/scanner.l
clean:
-rm -f src/parser.c src/scanner.c src/*.tab.* src/*.o
purge: clean
-rm -f src/vslc

View File

@ -0,0 +1,17 @@
#ifndef IR_H
#define IR_H
/* This is the tree node structure */
typedef struct n {
node_index_t type;
void *data;
struct s *entry;
uint64_t n_children;
struct n **children;
} node_t;
// Export the initializer function, it is needed by the parser
void node_init (
node_t *nd, node_index_t type, void *data, uint64_t n_children, ...
);
#endif

View File

@ -0,0 +1,37 @@
#ifndef NODETYPES_H
#define NODETYPES_H
typedef enum {
PROGRAM,
GLOBAL_LIST,
GLOBAL,
STATEMENT_LIST,
PRINT_LIST,
EXPRESSION_LIST,
VARIABLE_LIST,
ARGUMENT_LIST,
PARAMETER_LIST,
DECLARATION_LIST,
FUNCTION,
STATEMENT,
BLOCK,
ASSIGNMENT_STATEMENT,
ADD_STATEMENT,
SUBTRACT_STATEMENT,
MULTIPLY_STATEMENT,
DIVIDE_STATEMENT,
RETURN_STATEMENT,
PRINT_STATEMENT,
NULL_STATEMENT,
IF_STATEMENT,
WHILE_STATEMENT,
EXPRESSION,
RELATION,
DECLARATION,
PRINT_ITEM,
IDENTIFIER_DATA,
NUMBER_DATA,
STRING_DATA
} node_index_t;
extern char *node_string[26];
#endif

View File

@ -0,0 +1,35 @@
#ifndef VSLC_H
#define VSLC_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
// Numbers and names for the types of syntax tree nodes
#include "nodetypes.h"
// Definition of the tree node type
#include "ir.h"
// Token definitions and other things from bison, needs def. of node type
#include "y.tab.h"
/* This is generated from the bison grammar, calls on the flex specification */
int yyerror ( const char *error );
/* These are defined in the parser generated by bison */
extern int yylineno;
extern int yylex ( void );
extern char yytext[];
/* Global state */
extern node_t *root;
/* Global routines, called from main in vslc.c */
void simplify_syntax_tree ( void );
void print_syntax_tree ( void );
void destroy_syntax_tree ( void );
#endif

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;
}
}
}

View File

@ -0,0 +1,14 @@
VSLC := ../src/vslc
PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl))
PS3_EXAMPLES := $(patsubst ps3-simplify/%.vsl, ps3-simplify/%.ast, $(wildcard ps3-simplify/*.vsl))
all: $(PS2_EXAMPLES) $(PS2_EXAMPLES)
ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES)
%.ast: %.vsl
$(VSLC) -t < $^ > $@
clean:
-rm -r */*.ast

View File

@ -0,0 +1,14 @@
// checking that comments are ignored
// This program checks the assignment operators
func main()
begin
var a
a := 3
a += 1
a /= 2
a *= 32
a -= 2
print a
end

View File

@ -0,0 +1,8 @@
func add(a, b) begin
return a + b
end
func main()
begin
print add(40, 2)
end

View File

@ -0,0 +1,3 @@
func main() begin
print "Hello, World!"
end

View File

@ -0,0 +1,25 @@
func main()
begin
var a, b, c, d
c := 1
a := 3
b := a + c // 4
d := a * 100 + 50
print "a", a
print "b", b
print "c", c
print "d", d
if a = 14 then
print 1, "N", d / 5 + a, "RPR", a, "TERS "
else
print "COMP", c, "L", "ERS "
print b, "R", a, " "
if a < b then
if d > 42 then
print b, "W", d, "ME"
else
print "L", b, "M", c
// A dangling else, what could go wrong?
end

View File

@ -0,0 +1,19 @@
var global_var
func my_func(param)
begin
var local_var, local_var2
local_var := 1
end
var glob1, glob2
func main()
begin
var main_local_var
begin
var main_local_nested_var
main_local_nested_var := main_local_var
end
end

View File

@ -0,0 +1,10 @@
// check parsing of do-while loop
func main()
begin
var i
i := 2
while i < 9000 do
i := i * i
print i
end

View File

@ -0,0 +1,9 @@
func main() begin
var a
a := 1 + 2 + 4 + 5 + 6 + 7 + 8 + 9
b := (10 + 10 * 4) * (2 + 2 * (1 + 1)) / 10 + 2 * 5 + 6 / 3
if a = b then
print "The answer is", b
end

View File

@ -0,0 +1,16 @@
func my_fun(a, b, c, d, e, f, g, h) begin
var i, j, k, l, m
i := a + b + d
if i = f then begin
print "hmmm"
end
end
func main() begin
var n, o, p, q, r, s, t, u, v, w
n := 5
n += my_func(1, 2, 3, 5, 8, 13, 21, 34)
end