1 / 138

Review

Review. Lexical Analysis Syntax Analysis Semantic Analysis Code Generation Code Optimization. Syntax Analysis. Often called parsing Groups tokens of source program into grammatical phrases that are used by the compiler to check for correct syntax and to help in generating code

apria
Télécharger la présentation

Review

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. Review • Lexical Analysis • Syntax Analysis • Semantic Analysis • Code Generation • Code Optimization

  2. Syntax Analysis • Often called parsing • Groups tokens of source program into grammatical phrases that are used by the compiler to check for correct syntax and to help in generating code • Creation of a hierarchical structure called a syntax tree • Tree helps us determine if program is syntactically correct • Also aids in the translation of source program to target language

  3. Seattle San Francisco Grammar Example • Lexical • Ogay orthnay eightay undrendhay ilesmay • Syntactical • Miles go hundred north eight • Logical • Go eight hundred miles(facing south) • Run-Time • Go eight hundred miles (facing West)

  4. Grammars – Defining the Language Rules • Terminals (tokens) • Non-terminals - Syntactic variable. Contains groups of tokens that define some part of the language • Example: expression, if statement, etc • Start symbol - A special non-terminal (i.e. a program) • Productions • The manner in which terminals and non-terminals are combined to form statements • A non-terminal in LHS and a string of terminals and non-terminals in RHS

  5. Example • Variable Declaration • A type followed by one or more comma separated identifiers that end with a semi-colon. <Var> -> <Indent> <Var> -> <Var> , <Ident> • <Var> and <Ident> are non-terminals • The comma is a terminal • The two lines are called productions

  6. Grammars • We will be defining a grammar for the entire JO99 programming language. • We will then have JCUP produce for us a parser that detects if the JO99 program is syntactically correct. • In addition, the parser will create for us a tree that represents the program and allows us to be able to do other things like semantic checks and code generation.

  7. Example • Simple arithmetic expressions with + and * • 8.2 + 35.6 • 8.32 + 86 * 45.3 • (6.001 + 6.004) * (6.035 * -(6.042 + 6.046)) • Terminals (or tokens) • num for all the numbers • ‘+’, ‘-’, ‘*’, ‘(‘, ‘)’ • What is the grammar for all possible expressions?

  8. Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  *

  9. Categories of Parsers ( )

  10. Categories of Parsers • L - parse from left to right • R - parse from right to left ( )

  11. Categories of Parsers • L - leftmost derivation • R - rightmost derivation ( )

  12. Categories of Parsers • Number of look ahead characters ( )

  13. Categories of Parsers • Examples: • LL(0) – Parse Left to Right, Derive the tree using a leftmost derivation (top down), no look ahead characters • LR(1) – Parse Left to Right, Derive the tree using a rightmost derivation (bottom up), 1 look ahead character. • Each category of parsing handles a different type of language. • We will be learning about LR(k) parsers and will implement an LR(k) parser.

  14. Why LR(k)? • Virtually all programming language grammars can be parsed using a LR(k) technique • Most general parsing method for programming grammars • Can build a very efficient parser engine given just the syntax rules of the language. • Can detect a syntactic error as soon as it is possible to do so • Because its so general, programs have been written (JCUP) that produce the parser instead of writing it from scratch.

  15. LR(k) Parser implementation • Sometimes called a Shift-Reduce Parser • Parse from left to right (get the tokens from left to right) • Bottom up parsing (same as rightmost derivation)

  16. Actions of a Shift-Reduce Parser Parse Tree

  17. Actions of a Shift-Reduce Parser Parse Tree Parse Tree

  18. Actions of a Shift-Reduce Parser Parse Tree Parse Tree

  19. Actions of a Shift-Reduce Parser Parse Tree Parse Tree

  20. Actions of a Shift-Reduce Parser Parse Tree Parse Tree

  21. Actions of a Shift-Reduce Parser Parse Tree Parse Tree

  22. Actions of a Shift-Reduce Parser Parse Tree Parse Tree

  23. Actions of a Shift-Reduce Parser Parse Tree Parse Tree

  24. Actions of a Shift-Reduce Parser Parse Tree

  25. Actions of a Shift-Reduce Parser Parse Tree

  26. Actions of a Shift-Reduce Parser Parse Tree

  27. Actions of a Shift-Reduce Parser Parse Tree

  28. Actions of a Shift-Reduce Parser • How do we build this tree? • As productions are recognized, a portion of the tree is created. • This portion of the tree will be needed later to build bigger portions of the tree and therefore must be saved for future use. • This requires a stack. • The stack plus the next token read from the source program determines the action.

  29. Parser Engine Actions of a Shift-Reduce Parser stack Parser Action Stack Current Symbol

  30. Actions of a Shift-Reduce Parser • Shift • Shift the current element into top of the stack • Move the current pointer (next token) • Reduce • Apply a production (we recognize a part of the program) • Top of the stack should match the RHS of the grammar • Remove those symbols from the stack • Add the LHS non-terminal • Accept • End of stream reached and stack only has the start symbol • Reject • End of stream reached but stack has more than the start symbol

  31. Shift-Reduce Parser Example num * ( num + num )

  32. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * num * ( num + num )

  33. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * num * ( num + num )

  34. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * num SHIFT num * ( num + num )

  35. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * num REDUCE num * ( num + num )

  36. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> <expr> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  37. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> <expr> * <expr> <expr> SHIFT <expr> <op> <expr> <op> <expr> num * ( num + num )

  38. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> <expr> * <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  39. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  40. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> ( <expr> <op> <expr> <expr> SHIFT <expr> <op> <expr> <op> <expr> num * ( num + num )

  41. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> num ( <expr> <op> <expr> <expr> SHIFT <expr> <op> <expr> <op> <expr> num * ( num + num )

  42. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> num ( <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  43. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> <expr> ( <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  44. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * + <expr> <expr> ( <expr> <op> <expr> <expr> SHIFT <expr> <op> <expr> <op> <expr> num * ( num + num )

  45. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * + <expr> <expr> ( <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  46. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <op> <expr> <expr> ( <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  47. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * num <op> <expr> <expr> ( <expr> <op> <expr> <expr> SHIFT <expr> <op> <expr> <op> <expr> num * ( num + num )

  48. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * num <op> <expr> <expr> ( <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  49. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> <op> <expr> <expr> ( <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

  50. Shift-Reduce Parser Example <expr>  <expr> <op> <expr> <expr>  ( <expr> ) <expr>  - <expr> <expr>  num <op>  + <op>  - <op>  * <expr> <op> <expr> <expr> ( <expr> <op> <expr> <expr> REDUCE <expr> <op> <expr> <op> <expr> num * ( num + num )

More Related