









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A set of lecture notes from a computer science course, cpsc 326, held in spring 2013. The notes cover topics such as compilation and interpretation, the difference between compiled and interpreted languages, and the compilation process. The notes also include examples and exercises.
Typology: Study notes
1 / 15
This page cannot be seen from the preview
Don't miss anything!










Today ...
Homework ...
Compiler (think C/C++)
Target Program
input (^) output
Source Code (^) (a program)
input
output
#include
pushq %rbp movq %rsp, %rbp movq %rdi, -8(%rbp) cmpq %rax, %rdx jae L
55 89 e5 53 83 ec 04 83 e4 f0 e8 31 00 00 00 89 c3 e8 2a 00 00 00 39 c 74 10 8d b6 00 00 00 00 39 c3 7e 13 29 c3 39 c Source Code Assembly Machine code
Compilation generally results in faster programs
Interpretation is often convenient
Many interpreted languages can also be compiled
Many compiled languages often use interpretation
Many language implementations include both
Interpreter (VM)
input output
Source Language
input
output
Intermediate Language
input
Tokens are the smallest meaningful units of a program
Examples:
White space and comments are (usually) not tokens
In lexical analysis ...
int main() { int x = 0; }
INT, ID(main), LPAREN, RPAREN, LBRACKET, INT, ID(x), OP(=), CONST(0), SEMICOLON, RBRACKET
In syntax analysis ...
INT, ID(main), LPAREN, RPAREN, LBRACKET, INT, ID(x), OP(=), CONST(0), SEMICOLON, RBRACKET
fun-def
decl-spec
type-spec
INT
decl
ID(main)
…
In semantic analysis ...
Basic idea
Implemented using either
Scanner usually called one token at a time
Source Code
read more input nextToken()
Define a set of token symbols
#define LPAREN 0 #define RPAREN 1 #define SEMICOLON 2 ...
Define a Token class
class token { private: int token_type; // the type of symbol int token_line; // line number int token_column; // column number string token_lexeme; // "lexeme" (nums, vars, etc) public: token(int type, int line, int col, int lexeme); int type() const; int line() const; int column() const; string lexeme() const; };
Queries (goals) are always true or false in Prolog
?- member(a, [a, b]). true. ?- member(c, [a, b]). false.
Negation as failure
Prolog provides a not predicate
?- not(member(c, [a, b])). true.
?- + member(c, [a, b]). true.
?= + member(a, [a, b]). false.
Consider this rule
sibling(X, Y) :- parent(P, X), parent(P, Y).
Q: can you see a problem?
parent(alice, bob). parent(alice, charlie).
?- sibling(bob, bob).