DV1565 / DV1511:

Compiler and Interpreter Technology

Tuesday, January 24th, 2017

PDA and LL Languages

Table of Contents

1. Context-Free Languages (CFL)

2. Context-free Languages : Balanced Pairs

3. Context-free Languages : No possible DFA

%3 end ... done e s s x1 x1 s->x1 x x1->done y x2 x2 x1->x2 x x3 x3 x2->x3 x y1 y1 x2->y1 y x3->end x y2 y2 x3->y2 y y1->done y y2->y1 y
  • We start trying to build a DFA to recognise \(x^ny^n\).
  • We cannot encode the sequence of xs as a loop on a single state.
  • Each time we consume an x we need a new state to start the chain that consumes that many ys.
  • So we need as many states as there could be xs to remember.
  • But the number of xs is unbounded (infinite)...
  • ... we are only allowed a finite number of states in a DFA.
  • Hence it is impossible to build a working DFA in a finite number of states.

4. Context-free Languages : Matching brackets

5. Language class: CFL

5. Language class: CFL by Production Rules

6. Language class: Grammar

L : /* empty */ | 'x' L 'y' ( \(L \rightarrow \epsilon \; | \; xLy\) )

7. Language class: Bi-directionality of grammars

8. Machine class : Extending the machine

9. Machine class : Using the stack

10. Machine class : PDA Examples

Matching pairs:
%3 3 3 0 0 1 1 0->1 x, push x 1->1 x, push x 2 2 1->2 y, pop x 2->3 emp, emp 2->2 y, pop x


Nested Brackets:
%3 0 0 1 1 0->1 (, push ( 1->0 emp, emp 1->1 (, push ( 1->1 ), pop (

11. Simulation : Earley Parsers

12. Sequences in CFGs

13. More sequences in CFGs

L : /*empty*/ | M L M : 'x' 'y'
\(L(\;(xy)^*\;) = \{ \epsilon, xy, xyxy, \ldots \}\)
L : '<' DIGIT Digits '>' Digits : /*empty*/ | DIGIT Digits

Break (15mins)





Intermission

14. Equivalent Grammars : Choice clauses

15. Equivalant grammars : Derivations

list : NUM | NUM listlist : NUM list | NUM
list : NUM | list NUMlist : list NUM | NUM

16. Equivalent grammars : Comparing Derivations

Grammar\(L_1 \rightarrow x \; | \; x L_1\)\(L_2 \rightarrow x \; | \; L_2 x\)
Initial sentential form (step 0)\(< L_1 >\)\(< L_2 >\)
Rewrite using\(L_1 \Rightarrow x L_1\)\(L_2 \Rightarrow L_2 x\)
Sentential form (step 1)\(< x , L_1 >\)\(< L_2, x >\)
Rewrite using\(L_1 \Rightarrow x L_1\)\(L_2 \Rightarrow L_2 x\)
Sentential form (step 2)\(< x , x , L_1 >\)\(< L_2, x, x >\)
Rewrite using\(L_1 \Rightarrow x \)\(L_2 \Rightarrow x\)
String in the language (step 3)\(< x , x , x >\)\(< x, x, x >\)

17. The derivation algorithm

18. Leftmost and Rightmost

19. Strategies, Derivations and Trees

20. Left/right recursion

21. Top-down example I

L ::= L DIG | DIG
%3 0 L1 1 L1 0->1 r1 ... 0->r1 2 L1 1->2 r2 ... 1->r2 3 L1 2->3 r3 ... 2->r3

22. Top-down example II

R ::= DIG R | DIG
%3 0 R1 r1 DIG=1 0->r1 1 R1 0->1 r2 DIG=2 r3 DIG=3 1->r2 2 R1 1->2 2->r3 3 R2 2->3 4 DIG=4 3->4

23. Top-down vs Bottom-up

24. Top-down vs Bottom-up

25. Balanced Pairs Analysis

%3 0 OPEN x1 'x' 0->x1 1 CLOSE 0->1 2 OPEN 1->2 y1 'y' 1->y1 x2 'x' 2->x2 3 CLOSE 2->3 4 OPEN 3->4 y2 'y' 3->y2 x3 'x' 4->x3 5 CLOSE 4->5 y3 'y' 5->y3
OPEN = 'x' CLOSE CLOSE = 'y' | OPEN 'y'
  • This is a right-recursive grammar suitable for top-down parsing (LL).
  • The parse-tree shows the derivation the parser makes of "xxxyyy".
  • It looks like it works; num(xs)==num(ys).
  • Can we prove that it always works?
  • The technique is algebraic: rewrite the grammar into a form where the property can be checked mechanically.
    • I won't work through the refactoring in § 4.3, but this should give you an idea of where the techniques are useful.

26. Balanced Pairs Analysis

OPEN ::= 'x' CLOSE CLOSE ::= 'y' | OPEN 'y'
OPEN ::= 'x' 'y' | 'x' OPEN 'y'

27. Balanced Parentheses

PARS ::= PARS PAR | PAR PAR ::= '(' PARS ')' | '(' ')'
%3 0 PARS 1 PAR 0->1 l1 '(' 1->l1 2 PARS 1->2 r1 ')' 1->r1 3 PAR 2->3 4 PAR 2->4 l2 '(' 3->l2 r2 ')' 3->r2 l3 '(' 4->l3 5 PARS 4->5 r3 ')' 4->r3 6 PAR 5->6 l4 '(' 6->l4 r4 ')' 6->r4