Compilers and Interpreters
Wednesday August 15, 2024
Exam for:
DT135G Kompilatorer och interpretatorer, provkod A001
| Aids: | No aids. |
| Score requirements: |
Maximum score is 41.
To pass, at least 24 points are required. |
| Results: | Announced no later than 15 working days after the exam. |
| Return of the exams: | Electronically through the student portal "Studenttjänster". |
| Examiner and teacher on call: | Thomas Padron-McCarthy, phone 070-73 47 013. |
A is a non-terminal, but x and y are any constructions consisting of terminals and non-terminals.A -> A x | y
The rule is replaced by the following two rules (or, more correctly, three productions), that describe the same language, but are not left recursive:
A -> y R R -> x R | empty
A is a non-terminal, but x, y and z are any constructions consisting of terminals and non-terminals.A -> x y | x z
Replace with these three productions:
A -> x R R -> y | z
a = b * c;
if (d < e * f / g + h) {
while (i < j) {
k = l + m * n + o;
}
p = q;
if (r < s) {
t = u;
}
else {
v = w;
}
}
x = y + z;
|
Translate the program segment to two of the following three types of representations. (Should you answer with all three, the one with the highest points will be discarded.)
a) an abstract syntax tree (by drawing it)
b) postfix code for a stack machine
c) three-address code
Note: Two of the three types, not all three.
We want to create a simple programming language that works with threads. There is already a Bison grammar for the language:
%{
#include <stdio.h>
extern int yylex(void);
extern void yyerror(const char*);
%}
%define parse.error verbose
%start program
%token VAR ID NUMBER THREAD PRINT
%%
program : variables threads ;
variables : variable variables
| %empty
;
variable : VAR ID '=' NUMBER ';' ;
threads : thread threads
| %empty
;
thread : THREAD NUMBER '{' operations '}' ;
operations : operation operations
| %empty
;
operation : ID '=' ID ';'
| ID '=' NUMBER ';'
| ID '=' ID '+' ID ';'
| ID '=' ID '+' NUMBER ';'
| PRINT '(' ID ')' ';'
;
%%
|
There is also a Flex specification for the terminals of the language:
%{
#include "threads.tab.h"
%}
%%
[\n\t ] { /* Ignore whitespace */ }
"=" { return '='; }
";" { return ';'; }
"{" { return '{'; }
"}" { return '}'; }
"(" { return '('; }
")" { return ')'; }
"+" { return '+'; }
"var" { return VAR; }
"thread" { return THREAD; }
"print" { return PRINT; }
[0-9]+ { return NUMBER; }
[A-Za-z][A-Za-z0-9]* { return ID; }
. { fprintf(stderr, "[ignoring unknown character '%c']\n", *yytext); }
%%
|
Also describe some constructs that the language will not allow, especially if there are any that might be surprising to a user.