1 / 13

Lecture # 7

Lecture # 7. Chapter 4: Syntax Analysis. What is the job of Syntax Analysis?. Syntax Analysis is also called Parsing or Hierarchical Analysis. A Parser implements grammar of the language may it be C, C++ etc

tea
Download Presentation

Lecture # 7

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

  2. What is the job of Syntax Analysis? • Syntax Analysis is also called Parsing or Hierarchical Analysis. • A Parser implements grammar of the language may it be C, C++ etc • The parser obtains a string of tokens from the lexical analyzer and verifies that the string can be generated by the grammar for the source language • The grammar that a parser implements is called a Context Free Grammar or CFG

  3. Position of a Parser in the Compiler Model Token,tokenval LexicalAnalyzer Parserand rest offront-end SourceProgram Intermediaterepresentation Get nexttoken Lexical error Syntax errorSemantic error Symbol Table

  4. The Parser The role of the parser is twofold: • To check syntax (= string recognizer) • And to report syntax errors accurately • To invoke semantic actions • For static semantics checking, e.g. type checking of expressions, functions, etc.

  5. What is the difference between Syntax and Semantic? • Syntax is the way in which we construct sentences by following principles and rules. • Semantics is the interpretations of and meanings derived from the sentence transmission and understanding of the message or in other words are the logical sentences making sense or not

  6. Error Handling • A good compiler should assist in identifying and locating errors • Lexical errors: important, compiler can easily recover and continue such as misspelling an identifier, keyword etc. • Syntax errors: most important for compiler, can almost always recover such as arithmetic expression with unbalanced parenthesis • Static semantic errors: important, can sometimes recover such as operator applied to incompatible operands • Dynamic semantic errors: hard or impossible to detect at compile time, runtime checks are required • Logical errors: hard or impossible to detect such as infinite recursive calls

  7. Viable-Prefix Property • The viable-prefix property of parsers allows early detection of syntax errors • Goal: detection of an error as soon as possible without further consuming unnecessary input • How: detect an error as soon as the prefix of the input does not match a prefix of any string in the language Error isdetected here …for (;)… Prefix

  8. Error Recovery Strategies • Panic mode • Discard input until a token in a set of designated synchronizing tokens is found • Phrase-level recovery • Perform local correction on the input to repair the error • Error productions • Augment grammar with productions for erroneous constructs • Global correction • Choose a minimal sequence of changes to obtain a global least-cost correction

  9. Context free Grammar • Context-free grammar is a 4-tupleG = (N, T, P, S) where • T is a finite set of tokens (terminal symbols) • N is a finite set of nonterminals • P is a finite set of productions of the form  where   N and   (NT)* • S  N is a designated start symbol

  10. Example Grammar Context-free grammar for simple expressions: G = <{expr,op,digit}, {+,-,*,/,0,1,2,3,4,5,6,7,8,9,),(}, P,expr> with productions P = expr expr op expr expr(expr) expr digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 op +| -| *| /

  11. Notational Conventions Used • Terminals: Lower case letters, operator symbols, punctuation symbols, digits, bolface strings are all terminals • Non Terminals: Upper case letters, lower case italic names are usually non terminals • Greek letters such as ,, represent strings of grammars symbols. Thus a generic production can be written as A 

  12. Examples of CFG • Write a CFG that generates Even Palindrome S aSa | bSb | є • Write a CFG that generates Odd Palindrome S aSa | bSb | a | b • Write a CFG that generates Equal number of a’s and b’s S aSbS | bSaS | є

  13. Examples • Write a CFG that generates Equal number of a’s and b’s S aSbScS | aScSbS | bSaScS | bScSaS | cSaSbS| cSbSaS | є

More Related