Add initial files

main
Øyvind Skaaden 2022-02-13 17:40:20 +01:00
parent f18c9eeb19
commit c498cf79e6
13 changed files with 174 additions and 0 deletions

BIN
exercises/02/ps2.pdf Normal file

Binary file not shown.

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,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,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,12 @@
VSLC := ../src/vslc
PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl))
all: $(PS2_EXAMPLES)
echo $(PS2_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