There might also be more questions than you have time to answer, but do as many as you can.
a) Some of these modules are usually called phases. What is a phase?
b) Another term used in the compiler context is pass. What is the difference between a phase and a pass?
c) In the list above, the modules have been ordered alphabetically. List the phases in the correct order, for example by drawing a number of squares connected with arrows.
d) The input to the compiler, and to the first phase, is the source program as text (that is, a sequence of characters), while the output from the compiler, and from the last phase, is the target program, typically as assembly language or machine language. What are the input and output of each phase? You can write them at the arrows between the squares from the question above.
e) One of the modules are part of the compiler, but isn't a phase. Which one?
f) Some of the listed modules are not part of the compiler, but are used or exist in connection to the compiler. Add them to the figure with the phases.
a) the lexical analysis ("the scanner")
b) the syntactic analysis ("the parser")
En övning i addition! Ange två tal och deras summa: 10 29 39 Rätt!
En övning i addition! Ange två tal och deras summa: 20 30 60 Nej, 20 + 30 blir 50!
We have written the program in C, but we made some mistakes, and when we try to build (that is, compile and link) and run the program, we will get the error and warning messages that are shown. In which of the compiler phases, or at other times, are each of these errors and warnings detected?
(Some of the errors may have to be fixed before we can continue the build an get the other errors.)
The program Messages #include <stdio.h> int main(void) { int a, b, summan; printf("En övning i addition!\n) printf("Ange två tal och deras summa: "); scanf("%d", &a); scanf("%d", b); scanf("%d", &gissning); if (gissning == summan) printf("Rätt!\n"); else print("Nej, %d + %d blir %d!\n", a, b, summan); return "0"; } (1) warning: missing terminating " character (2) error: expected ';' before 'printf' (3) Segmentation fault (core dumped) (4) error: 'gissning' undeclared (5) warning: 'summan' is used uninitialized (6) undefined reference to 'print' (7) warning: returning 'char *' from a function with return type 'int'
(Some of the errors may have to be fixed before we can continue the build an get the other errors.)
The program Messages #include <stdio.h> int main(void) { int a; int 2a; printf("Hej!\n"); printg("Ange ett tal: "); scanf("%d", &a); printf("Talet = %d\n", a); return 0; } (1) error: invalid suffix "a" on integer constant (2) warning: implicit declaration of function 'printg' (3) error: undefined reference to `printg'
(Some of the errors may have to be fixed before we can continue the build an get the other errors.)
The program Messages #include <stdio.h> int main(void) { int i = 17; float f = 3.14; char c = 'x'; char* s = "foo; s = f; printf("f = %f\n", i); c s; return; } (1) missing terminating " character (2) error: incompatible types in assignment (3) warning: double format, different type arg (arg 2) (4) error: syntax error before "s" (5) warning: `return' with no value, in function returning non-void
(Some of the errors may have to be fixed before we can continue the build an get the other errors.)
The program Messages #inglude <stdio.h> int main(void) { int x, y, z, t; printf("Ange x: ); scanf("%d", &x); printf("Ange y: ") scanf("%d", y); t = x + y; u = x + y; printf("z = %d\n", z); f(8); } // main (1) error: invalid preprocessing directive #inglude (2) warning: missing terminating " character (3) error: expected ';' before '}' token (4) Segmentation fault (core dumped) (5) warning: variable 't' set but not used (6) error: 'u' undeclared (7) warning: 'z' is used uninitialized (8) warning: implicit declaration of function 'f' (9) undefined reference to 'f'
There are solutions to some of the questions, but try to solve them yourself first.