Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }...... Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.

Similar presentations


Presentation on theme: "1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }...... Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v."— Presentation transcript:

1 1 Compilers

2 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }...... Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code

3 3 Lexical analyzer parser Compiler program machine code input output

4 4 A parser knows the grammar of the programming language

5 5 Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT

6 6 The parser finds the derivation of a particular input 10 + 2 * 5 Parser E -> E + E | E * E | INT E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 input derivation

7 7 10 E 25 E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 derivation derivation tree EE EE + *

8 8 10 E 25 derivation tree EE EE + * mult a, 2, 5 add b, 10, a machine code

9 9 Parsing

10 10 grammar Parser input string derivation

11 11 Example: Parser derivation input ?

12 12 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of

13 13

14 14 Phase 2 Phase 1

15 15 Phase 2 Phase 3

16 16 Final result of exhaustive search Parser derivation input (top-down parsing)

17 17 Time complexity of exhaustive search Suppose there are no productions of the form Number of phases for string :

18 18 Time for phase 1: possible derivations For grammar with rules

19 19 Time for phase 2: possible derivations

20 20 Time for phase : possible derivations

21 21 Total time needed for string : Extremely bad!!! phase 1 phase 2 phase 2|w|

22 22 There exist faster algorithms for specialized grammars S-grammar: symbolstring of variables appears once Pair

23 23 S-grammar example: Each string has a unique derivation

24 24 In the exhaustive search parsing there is only one choice in each phase For S-grammars: Total time for parsing string : Time for a phase:

25 25 For general context-free grammars: There exists a parsing algorithm that parses a string in time

26 26 Simplifications of Context-Free Grammars

27 27 A Substitution Rule Substitute B Equivalent grammar

28 28 In general: Substitute B equivalent grammar

29 29 Useless Productions Some derivations never terminate... Useless Production

30 30 Another grammar: Not reachable from S Useless Production

31 31 In general: If Then variable is useful Otherwise, variable is useless

32 32 A production is useful if all its variables are useful

33 33 Removing Useless Productions Example Grammar:

34 34 First: find all variables that produce strings with only terminals Round 1: Round 2:

35 35 Keep only the variables that produce terminal symbols

36 36 Second: Find all variables reachable from Dependency Graph not reachable

37 37 Keep only the variables reachable from S Final Grammar

38 38 Nullable Variables Nullable Variable:

39 39 Removing Nullable Variables Example Grammar: Nullable variable

40 40 Substitute Final Grammar

41 41 Unit-Productions Unit Production:

42 42 Removing Unit Productions Observation: Is removed immediately

43 43 Example Grammar:

44 44 Substitute

45 45 Remove

46 46 Substitute

47 47 Remove repeated productions Final grammar

48 48 Removing All Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Step 3: Remove Useless Variables


Download ppt "1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }...... Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v."

Similar presentations


Ads by Google