1 / 27

Syntax Analysis

Syntax Analysis. Chapter 4. Check (Important). http://www.engineersgarage.com/contribution/difference-between-compiler-and-interpreter. Introduction. covers the major parsing methods that are typically used in compilers: top-down (recursive-descent , LL ) and

rcarvajal
Download Presentation

Syntax Analysis

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. Syntax Analysis Chapter 4

  2. Check (Important) • http://www.engineersgarage.com/contribution/difference-between-compiler-and-interpreter

  3. Introduction • covers the major parsing methods that are typically used in compilers: • top-down (recursive-descent , LL) and • bottom-up (LR and its variants) .

  4. The Role o f the Parser

  5. General types of parsers for grammars • universal (algorithm), • top-down, and • bottom-up • Top-down methods build parse trees from the top (root) to the bottom (leaves). • Bottom-up methods start from the leaves and work their way up to the root.

  6. belongs to the class of LR grammars that are suitable for bottom-up parsing it cannot be used for top-down parsing because it is left recursive. non-left-recursive variant of the expression grammar (4.1) will be used for top-down parsing:

  7. Syntax Error Handling • Two of these strategies, called • panic-mode and • phrase-level recovery • Most programming language specifications do not describe how a compiler should respond to errors; error handling is left to the compiler designer.

  8. Common programming errors can occur at many different levels

  9. Detect an error • Several parsing methods, such as the LL and LR methods, detect an error, that is, when the stream of tokens from the lexical analyzer cannot be parsed further according to the grammar for the language. • More precisely, they have the viable-prefix property, meaning that they detect that an error has occurred as soon as they see a prefix of the input that cannot be completed to form a string in the language. • Another reason for emphasizing error recovery during parsing is that many errors appear syntactic (parsing cannot continue).

  10. Error handler - goals • The error handler in a parser has goals that are simple to state but challenging to realize: • Report the presence of errors clearly and accurately. • Recover from each error quickly enough to detect subsequent errors. • Add minimal overhead to the processing of correct programs

  11. Error-Recovery Strategies - 1 • Panic-Mode Recovery: on discovering an error , the parser discards input symbols one at a time until one of a designated set of synchronizing tokens is found. The synchronizing tokens are usually delimiters, such as semicolon or } , whose role in the source program is clear and unambiguous. • Phrase-Level Recovery: On discovering an error, a parser may perform local correction on the remaining input; that is , it may replace a prefix of the remaining input by some string that allows the parser to continue (replace a comma by a semicolon).

  12. Error-Recovery Strategies - 2 • Error Productions: we can augment the grammar for the language at hand with productions that generate the erroneous constructs. • Global Correction: (few changes as possible in processing an incorrect input string) There are algorithms for choosing a minimal sequence of changes to obtain a globally least-cost correction. Given an incorrect input string x and grammar G, these algorithms will find a parse tree for a related string y, such that the number of insertions, deletions, and changes of tokens required to transform x into y is as small as possible.

  13. Context-Free Grammars The Formal Definition - 1

  14. The Formal Definition - 2

  15. Notational Conventions -1

  16. Notational Conventions -2

  17. Derivations

  18. Left and Right most Derivations

  19. Parse Trees and Derivations

  20. Ambiguity • A grammar that produces more than one parse tree for some sentence is said to be ambiguous

  21. Verifying the Language Generated by a Grammar • A proof that a grammar G generates a language L has two parts: • show that every string generated by G is in L, and • conversely that every string in Lcan indeed be generated by G.

  22. Context-Free Grammars Versus Regular Expressions • Grammars are a more powerful notation than regular expressions.

More Related