1 / 19

Compiler Design

Compiler Design. Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation. Compilation Process. Start with source code Perform analysis Generate object code Execute code from start to finish on hardware or software interpreter

trumble
Télécharger la présentation

Compiler Design

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. Compiler Design • Lexical Analysis • Syntactical Analysis • Semantic Analysis • Optimization • Code Generation

  2. Compilation Process • Start with source code • Perform analysis • Generate object code • Execute code from start to finish on hardware or software interpreter • Note that execution is not a part of the compilation process

  3. Interpretation • Start with source code • Analyze, generate code, and execute code line-by-line.

  4. Compilation vs Interpretation • Compilation generates object code file. Interpretation does not. • Compile high-level code (code requiring lots of analysis). Interpret low-level code (code requiring very little analysis). • Compilation is consistent with static scoping. Interpretation is consistent with dynamic scoping.

  5. Compilation vs Interpretation (continued) • Compilation is non-interactive. Interpretation is interactive. • Compilation is used with imperative/ procedural languages (FORTRAN, Ada, Pascal, C, C++, Java, etc.) Interpretation is used with declarative languages (Prolog) and functional languages (LISP, Scheme, Hope, Miranda, ML, FP, Haskell, etc.

  6. if (a>b) x=1; else y =4; if, else – reserved word tokens (, ), ; – special character tokens a, b, x, y – ID tokens > – relop token 1, 4 – num tokens Lexical Analysis

  7. Syntactical Analysis stmt  IF ( cond ) stmt | IF (cond) stmt ELSE stmt | ID = expr cond  ID RELOP ID expr  NUM | ID | ID OP ID

  8. Parse Tree for IF Statement stmt / | \ IF ( cond ) ; stmt ELSE stmt ; / | \ / | \ / | \ ID relop ID ID = expr ID = expr | | | > 1 4

  9. Grammar Definition Grammar G = (N, S, S, P) Where: N = set of non-terminals S = set of tokens/terminals S = start non-terminal P = set of productions

  10. Grammar Example Grammar G1 = N = { <sentence>, <subject>, <predicate> } = { HE, SHE, RAN, JUMPED } S = <sentence> P = <sentence>  <subject> <predicate> <subject>  HE | SHE <predicate>  RAN | JUMPED

  11. Parse Tree <sentence> / \ <subject> <predicate> | | HE JUMPED

  12. Language Definition * Language L = { s | S  s } Where: S = Grammar Goal Symbol s = string of tokens/terminals

  13. Language Example For Grammar G1 Language L1 = HE RAN SHE RAN HE JUMPED SHE JUMPED

  14. Ambiguous Grammar G2 = N = {E } S = {ID, (, ), +, * } S = E P = E  E + E | E * E | (E) | ID

  15. Parse Trees for G2 Parse trees for: id + id + id E E / | \ / | \ E + E E + E / | \ | / | \ E + E id E + E | | | | id id id id

  16. Unambiguous Grammar G3 = E  E + T | T T  T * F | F F  ( E ) | id

  17. Parse Tree for G3 E / | \ E + T / | \ | E + T F | | | T F id | | F id | id

  18. Chomsky Grammar Heirarchy • Type 0 – Unrestricted • Type 1 – Context Sensitive • Production type: b A d  b a d • Equivalent to: Spoken Languages • Type 2 – Context Free • Production type: A  a • Equivalent to: Parser, Push-Down Automata • Type 3 – Regular • Production Type: A  a B | b or A  B a | b • Equivalent to: Scanner, Finite Automata, Regular Expressions

  19. Context Sensitivities Fire Time flies like an arrow

More Related