Introduction

There are 3 labs and you are expected to complete one of them (each pair in a group takes one of the assignments):

  1. Build systems
  2. Embedded languages
  3. Vector graphics

The lab skeleton is on Gitlab and the instructions for the individual parts are below.

1. Build systems

Build systems often build on a basis of running system commands to perform tasks. In this assignment, you will create two build systems for the same project – one using autoconf and one using cmake.

The C++ and C files in the build system are incomplete. You will need to add some missing implementation for non-Linux operating systems (you can stub these away and make the implementation do nothing with a comment on what should be done). The copyfile.c file should be compiled into a shared object (e.g. libcopyfile.so) and main.cpp into an executable. It should be possible to disable usage of the trigonometric functions by setting an option when calling cmake or the generated configure script. You will need to create at least the following files:

  • CMakeLists.txt
  • configure.ac
  • Makefile.in

Sample usage of autoconf:

autoconf
./configure --without-trig-functions CC=clang CXX=clang++
make

Sample usage of cmake:

mkdir -p build
cmake -DTRIG_FUNCTIONS:BOOL=OFF ..
make

Note

You should detect if FICLONE is available inside the configure/cmake script.

It should not be handled by preprocessor macros (except ones defined by cmake or configure).

Note

There are some common conventions when it comes to makefiles. For example, there are some common variables used by implicit rules. The build project should correctly detect which flags are needed depending on the flags it was given, perhaps something like this:

  • clang -DHAVE_FICLONE -fPIC -o copyfile.o copyfile.c
  • clang++ -DTRIG_FUNCTIONS -o main.o main.cpp
  • clang++ -o main main.o (MacOS, with trigonometric functions)
  • clang++ -o main main.o -lm -ldl (Linux, with trigonometric functions)
  • clang++ -o main main.o -ldl (Linux, without trigonometric functions)

Note

When using cmake, the makefile will be generated automatically. If you use make VERBOSE=1 when running this file, you can see some of the commands it executes.

Note

autoconf performs a simple text substitution on the files you give it. Start by creating a makefile that does what you want for some flags, then create the autoconf file that fills in the same flags. Then finish up by handling all the other cases.

2. Embedded languages

One usage of domain-specific languages is: languages that you embed into another programming language. Examples of this include regular expressions (see courses in compiler construction or formal languages), SQL (for querying databases), or scripting languages such as Lua.

Regular expressions

Regular expressions are used in a variety of applications. There are ways to use it in most good text editors, unix shells, and programming languages. In this exercise, you will use the unix commands grep and sed, which are used to search or manipulate text files using regular expressions.

  1. Using grep on file1.txt, find all records containing the entry “memory” in books 19-23.
  2. Using sed, create a copy of file2.txt where entries that have Carl or Diana in column 1 have column 2 replaced with 4.
  3. Write a C++ program that performs essentially the same operations as in 1 and 2 above using C++11 regular expressions. You can compile the skeleton by running make re.

SQL

Regular expressions are good for many things, but it is cumbersome to access structured data using regular expressions (which hopefully the previous exercise showed you, and CSV-style files are still among the easiest to manipulate using regular expressions). The next exercise uses Python and sqlite, which is a small and simple database engine with support for SQL. There is a database of the bible which contains a table called t_bbe; it has columns called b, c, v and t (book, chapter, verse, text). Make SQL queries to get the following:

  • All book numbers and the number of verses in these books
  • The book numbers that have a verse containing the string “memory”
  • The average number of verses per chapter

Note

Only use SQL queries – do not return data and process it in Python.

Lua

Finish the C program which is supposed to load test.lua and call the function defined in the file, which in turn should call the fromC function defined in the C file (C main function calls function in lua script which calls a function in C). You can compile the skeleton by running make lua-dsl and then ./lua-dsl runs the program.

digraph luacallchain { compound=true; labelloc="t"; subgraph clusterLua { label="test.lua" Lua[shape="none"][style="invis"][label=""]; addFromC } subgraph clusterMain { label="main.c" Main[shape="none"][style="invis"][label=""]; fromC main } main -> addFromC [label="(x=41,y=22)"][ltail ="clusterLua"]; addFromC -> fromC [label="v=22"][ltail ="clusterMain"]; fromC -> addFromC [style=dashed, label="result=-22"][ltail ="clusterLua"]; addFromC -> main [style=dashed, label="result=19"][ltail ="clusterMain"]; }

Call chain through the program. Dashed lines are the returned results.

3. Vector graphics

There exists many ways to draw text or shapes in computer programs. An efficient way to make sure a certain figure always looks reasonably good on a computer regardless of screen resolution is to use vector graphics. There are of course ways to draw in frameworks such as Qt, or using OpenGL – these are great if you want to draw something inside of a program. In this exercise you will try to use SVG (an XML-based image format for 2D vector graphics), Postscript (a programming language in which you describe a vector image that is rasterized by a Postscript interpreter), and Graphviz (a language for describing graphs and shapes in a convenient way).

Postscript

Create a postscript program that loops a certain number of times, writing text and line numbers. Then write an overlapping circle and a triangle somewhere using a filled colour.

_images/ps.png

Example of what the postscript program could produce.

SVG

Create an SVG file with similar elements as the postscript program, but skip creating a loop to create text objects. Do propose possible solutions to how you would use for loops to construct the SVG image for the seminar discussion.

_images/svg.png

Example of what the SVG file could produce.

Graphviz

Using graphs to visualize something is very common, and might be used in for example PowerPoint (or similar) presentations. The problem is that you don’t want to draw these graphs in a graphics editor such as InkScape or Dia if you ever need to update this graph (because you need to move everything around to fit the screen). If you want to create graphs that can easily be updated, use a language to describe graphs and use a tool to create a nice layout for you instead. Use GraphViz and its associated command-line tools to do the exercise.

There is a small, empty program and a script to create SVG and Postscript files for some of the layout engines available in GraphViz. You are supposed to create two graphs in GraphViz – one where you draw edges from a part of a node and one that represents a state machine.

_images/dot1.png

Example of GraphViz drawing edges from part of a node. (0,1,2,3) is a single node in the graph with 4 different parts that it is possible to draw edges from.

_images/dot2.png

Example of a state machine produced by GraphViz. The regular expression

(b*a+b(ab)*b)*(b*a+b(ab)*)