Programming Language Technology DAT151/DIT231

Andreas Abel

2020-12-17 Training exam

We go through the exam of 2020-01-13.

Q1: Grammars

see BNFC grammar

Q2: Lexing

  1. Regular expression for acceptable password

Syntax elements : .(Σ) a b c | * + ( ) and juxtaposition for sequence

Possible solutions:

  1. Automaton for acceptable password

States:

Transition table:

0 a b ab
a a a ab ab
b b ab b ab
c 0 a b ab

Q3: LR parsing

Notation: stack . remaining input // action

        . x + y - x // reduce with None
P       . x + y - x // shift x
P x     . + y - x   // reduce with X
P A     . + y - x   // shift +
P A +   . y - x     // reduce with Plus
P       . y - x     // shift y
P y     . - x       // reduce with Y
P A     . - x       // shift -
P A -   . x         // shift x
P A - x .           // reduce with X
P A - A .           // reduce with Done
P A - M .           // reduce with Minus
P M     .           // reduce with Seq
S       .           // accept

Q4: Type checking and evaluation

  1. Type checking statements

  2. Interpretation of expressions

Q5: Compilation

Compilation state:

Service functions:

Part 1. Compilation schemes:

compile(Blk ss):
  newBlock
  for (s : ss)
    compile(s)
  popBlock

compile(SBlock b):
  compile(b)

compile(SInit t x e):
  a ← addVar t x
  compile(e)
  emit (istore a)

compile(SExp e):
  compile(e)
  emit(pop)

compile(SIfElse e s₁ s₂):
  Lelse ← newLabel
  Ldone ← newLabel
  compile(e)
  emit(ifeq Lelse)
  newBlock
  compile(s₁)
  popBlock
  emit(goto Ldone)
  emit(Lelse:)
  newBlock
  compile(s₂)
  popBlock
  emit(Ldone:)

compile(ETrue):
  emit(ldc 1)

compile(EFalse):
  emit(ldc 0)

compile(EInt i):
  emit(ldc i)

compile(ECall f es):
  g ← lookupFun f
  for (e : es)
    compile(e)
  emit(invokestatic g)

compile(EPostIncr x):
  a ← lookupVar x
  emit(iload a)
  emit(dup)
  emit(ldc 1)
  emit(iadd)
  emit(istore a)

compile(EPlus e₁ e₂):
  compile(e₁)
  compile(e₂)
  emit(iadd)

Part 2. Small-step semantics

iadd           : (P,V,S.i.j)      → (P+1,V,     S.(i+j))
istore a       : (P,V,S.i)        → (P+1,V[a=i],S)
ldc i          : (P,V,S)          → (P+1,V,     S.i)
dup            : (P,V,S.i)        → (P+1,V,     S.i.i)
pop            : (P,V,S.i)        → (P+1,V,     S)
iload  a       : (P,V,S)          → (P+1,V,     S.V[a])
invokestatic g : (P,V,S.i₁....iₙ) → (P+1,V,     S.i)
  g is a method expecting n int arguments and returning and int
  i should be the value computed by g from arguments i₁...iₙ
ifeq L         : (P,V,S.i)        → (L,  V,     S)  if i=0
ifeq L         : (P,V,S.i)        → (P+1,V,     S)  if i≠0
goto L         : (P,V,S)          → (L,  V,     S)

Q6: Functional languages

  1. Call-by-name interpreter

See lecture notes

Environment γ maps variables x to closures ⟨e;γ⟩ Values are either integer literals n or function closures ⟨λx→e;γ⟩

Judgement: γ ⊢ e ⇓ v

Rules:

δ ⊢ e ⇓ v
----------- γ(x) = ⟨e;δ⟩
γ ⊢ x ⇓ v

-------------------
γ ⊢ λx→e ⇓ ⟨λx→e;γ⟩

γ ⊢ e₁ ⇓ ⟨λx→e;δ⟩
δ,x=⟨e₂;γ⟩ ⊢ e ⇓ v
-------------------
γ ⊢ e₁ e₂ ⇓ v

----------
γ ⊢ n ⇓ n

γ ⊢ e₁ ⇓ n₁
γ ⊢ e₂ ⇓ n₂
---------------------
γ ⊢ e₁ + e₂ ⇓ n₁ + n₂

End.