1 / 48

Compilers

Compilers. Machine Code. Program. Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3. v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; }. Compiler. Compiler. Lexical analyzer. parser. input. output. machine code. program.

Télécharger la présentation

Compilers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compilers

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

  3. Compiler Lexical analyzer parser input output machine code program

  4. A parser knows the grammar of the programming language

  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. The parser finds the derivation of a particular input derivation Parser input E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 E -> E + E | E * E | INT 10 + 2 * 5

  7. derivation tree derivation E E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 + E E 10 E E * 2 5

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

  9. Parsing

  10. Parser input string derivation grammar

  11. Example: Parser derivation input ?

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

  13. Phase 2 Phase 1

  14. Phase 2 Phase 3

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

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

  17. For grammar with rules Time for phase 1: possible derivations

  18. Time for phase 2: possible derivations

  19. Time for phase : possible derivations

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

  21. There exist faster algorithms for specialized grammars S-grammar: symbol string of variables appears once Pair

  22. S-grammar example: Each string has a unique derivation

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

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

  25. Simplifications of Context-Free Grammars

  26. A Substitution Rule Equivalent grammar SubstituteB

  27. In general: Substitute B equivalent grammar

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

  29. Useless Production Another grammar: Not reachable from S

  30. In general: If Then variable is useful Otherwise, variable is useless

  31. A production is useful if all its variables are useful

  32. Removing Useless Productions Example Grammar:

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

  34. Keep only the variables that produce terminal symbols

  35. Second: Find all variables reachable from Dependency Graph not reachable

  36. Keep only the variables reachable from S Final Grammar

  37. Nullable Variables Nullable Variable:

  38. Removing Nullable Variables Example Grammar: Nullable variable

  39. Final Grammar Substitute

  40. Unit-Productions Unit Production:

  41. Removing Unit Productions Observation: Is removed immediately

  42. Example Grammar:

  43. Substitute

  44. Remove

  45. Substitute

  46. Remove repeated productions Final grammar

  47. Removing All • Step 1: Remove Nullable Variables • Step 2: Remove Unit-Productions • Step 3: Remove Useless Variables

More Related