1 / 21

Compiler course

Compiler course. Syntax Analysis. Outline. Role of parser Error handling and recovery Context free grammars Grammar transformation Top down parsing Bottom up parsing. The role of parser. token. Lexical Analyzer. Parser. Rest of Front End. Source program. Parse tree. Intermediate

hepp
Download Presentation

Compiler course

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 course Syntax Analysis

  2. Outline • Role of parser • Error handling and recovery • Context free grammars • Grammar transformation • Top down parsing • Bottom up parsing

  3. The role of parser token Lexical Analyzer Parser Rest of Front End Source program Parse tree Intermediate representation getNext Token Symbol table

  4. Role of parser in compiler • Types of parsers: - universal -Top-down -bottom-up • Both top down & bottom up works only for sub classes of grammars. • Sevaral of these are LL and LR grammars • Both grammars are expressive enough to describe most of the syntactic construct of current p.languages • gg

  5. Parsers implemented by hand use LL grammars predictive parser • Parsers constructed using automated tools use LR grammars

  6. Error handling • Common programming errors • Lexical errors: Misspellings of keywords, identifiers, and operators. • Syntactic errors: Misplaced semicolons, extra or missing braces. • Semantic errors: Type mismatches between operators and operands. Like, a return statements with return type void. • Logical Errors : Incorrect reasoning or plain carelessness might result in errors like interchangeably using = and == operators

  7. Error handler goals • 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 progrms

  8. Error-recover strategies • Panic mode recovery • Discard input symbol one at a time until one of designated set of synchronization tokens is found • Phrase level recovery • Replacing a prefix of remaining input by some string that allows the parser to continue

  9. Context free grammars • Terminals • Nonterminals • Start symbol • productions expression -> expression + term expression -> expression – term expression -> term term -> term * factor term -> term / factor term -> factor factor -> (expression) factor -> id

  10. Notational conventions

  11. Derivations • Productions are treated as rewriting rules to generate a string • Rightmost and leftmost derivations • E -> E + E | E * E | -E | (E) | id • Derivations for –(id+id) • E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)

  12. Parse trees • -(id+id) • E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)

  13. Ambiguity • For some strings there exist more than one parse tree • Or more than one leftmost derivation • Or more than one rightmost derivation • Example: id+id*id

  14. Left recursion Example grammars: E -> E + T | T T -> T * F | F F -> (E) | id Above grammar is not suitable for top down because it is left recursive E -> TE’ E’ -> +TE’ | Ɛ T -> FT’ T’ -> *FT’ | Ɛ F -> (E) | id Suitable for top down

  15. Elimination of left recursion • A grammar is left recursive if it has a non-terminal A such that there is a derivation A=> Aα • Top down parsing methods cant handle left-recursive grammars • A simple rule for direct left recursion elimination: • For a rule like: • A -> A α|β • We may replace it with • A -> β A’ • A’ -> α A’| ɛ +

  16. A->Aα1 | A α2 |... |A αm | β1 | β2 |…| βn • A -> β1A||β2A||…|βnA| • A’ -> α1A||α2A| |... |αmA| |є • Ex 1: S -> Sab | Sd | ad | ed • Ex 2 : A -> Abd | be | Bed | Aed • Ex 2: S  Aa | b • A  Ac | Sd |є

  17. S  Aa | b • A  bdA’ | A’ • A’  cA’ | adA’ |Є

  18. Left factoring • Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive or top-down parsing. • Consider following grammar: • Stmt -> if expr then stmt else stmt • | if expr then stmt • On seeing input if it is not clear for the parser which production to use • We can easily perform left factoring: • If we have A->αβ1| αβ2 then we replace it with • A -> αA’ • A’ -> β1| β2

  19. Left factoring (cont.) • Algorithm • For each non-terminal A, find the longest prefix α common to two or more of its alternatives. If α<> ɛ, then replace all of A-productions A->αβ1|αβ2 | … | αβn | γby • A -> αA’ | γ • A’ -> β1|β2 | … | βn • Example: • S -> I E t S | i E t S e S | a • E -> b

  20. Elimination of ambiguity

  21. Elimination of ambiguity (cont.) • Idea: match each else with its closest unmatched then • A statement appearing between a then and an else must be matched

More Related