1 / 56

Parsing

Parsing. Front-End: Parser. Checks the stream of words and their parts of speech for grammatical correctness. tokens. source code. IR. scanner. parser. errors. Front-End: Parser. Determines if the input is syntactically well formed. tokens. source code. IR. scanner. parser. errors.

gayora
Download Presentation

Parsing

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. Parsing

  2. Front-End: Parser • Checks the stream of words and their parts of speech for grammatical correctness tokens sourcecode IR scanner parser errors

  3. Front-End: Parser • Determines if the input is syntactically well formed tokens sourcecode IR scanner parser errors

  4. Front-End: Parser • Guides context-sensitive (“semantic”) analysis (type checking) tokens sourcecode IR scanner parser errors

  5. Front-End: Parser • Builds IR for source program tokens sourcecode IR scanner parser errors

  6. Syntactic Analysis • Natural language analogy: consider the sentence He wrote the program

  7. Syntactic Analysis He wrote the program noun verb article noun

  8. Syntactic Analysis He wrote the program noun verb article noun subject predicate object

  9. Syntactic Analysis • Natural language analogy He wrote the program noun verb article noun subject predicate object sentence

  10. Syntactic Analysis • Programming language if ( b <= 0 ) a = b assignment bool expr if-statement

  11. Syntactic Analysis syntax errors int* foo(int i, int j)) { for(k=0; i j; ) fi( i > j ) return j; }

  12. Compiler Construction Sohail Aslam Lecture 11

  13. Syntactic Analysis int* foo(int i, int j)) { for(k=0; i j; ) fi( i > j ) return j; } extra parenthesis Missing expression not a keyword

  14. Semantic Analysis • Grammatically correct He wrote the computer noun verb article noun subject predicate object sentence

  15. Semantic Analysis • semantically (meaning) wrong! He wrote the computer noun verb article noun subject predicate object sentence

  16. Semantic Analysis int* foo(int i, int j) { for(k=0; i < j; j++ ) if( i < j-2 ) sum = sum+i return sum; } undeclared var return type mismatch

  17. Role of the Parser • Not all sequences of tokens are program. • Parser must distinguish between valid and invalid sequences of tokens.

  18. Role of the Parser • Not all sequences of tokens are program. • Parser must distinguish between valid and invalid sequences of tokens.

  19. Role of the Parser What we need • An expressive way to describe the syntax • An acceptor mechanism that determines if input token stream satisfies the syntax

  20. Role of the Parser What we need • An expressive way to describe the syntax • An acceptor mechanism that determines if input token stream satisfies the syntax

  21. Role of the Parser What we need • An expressive way to describe the syntax • An acceptor mechanism that determines if input token stream satisfies the syntax

  22. Study of Parsing • Parsing is the process of discovering a derivation for some sentence

  23. Study of Parsing • Mathematical model of syntax – a grammar G. • Algortihm for testing membership in L(G).

  24. Study of Parsing • Mathematical model of syntax – a grammar G. • Algortihm for testing membership in L(G).

  25. Context Free Grammars A CFG is a four tuple G=(S,N,T,P) • S is the start symbol • N is a set of non-terminals • T is a set of terminals • P is a set of productions

  26. Why Not Regular Expressions? Reason:regular languages do not have enough power to express syntax of programming languages.

  27. Limitations of Regular Languages • Finite automaton can’t remember number of times it has visited a particular state

  28. Example of CFG • Context-free syntax is specified with a CFG

  29. Example of CFG • ExampleSheepNoise → SheepNoise baa| baa • This CFG defines the set ofnoises sheep make

  30. Example of CFG • We can use the SheepNoise grammar to create sentences • We use the productions as rewriting rules

  31. Example of CFG SheepNoise → SheepNoise baa| baa

  32. Example of CFG SheepNoise → SheepNoise baa| baa

  33. Example of CFG And so on ...

  34. Example of CFG • While it is cute, this example quickly runs out intellectual steam • To explore uses of CFGs, we need a more complex grammar

  35. Example of CFG • While it is cute, this example quickly runs out intellectual steam • To explore uses of CFGs, we need a more complex grammar

  36. More Useful Grammar

  37. Backus-Naur Form (BNF) • Grammar rules in a similar form were first used in the description of the Algol60 Language.

  38. Backus-Naur Form (BNF) • The notation was developed by John Backus and adapted by Peter Naur for the Algol60 report. • Thus the term Backus-Naur Form (BNF)

  39. Backus-Naur Form (BNF) • The notation was developed by John Backus and adapted by Peter Naur for the Algol60 report. • Thus the term Backus-Naur Form (BNF)

  40. Derivation: • Let us use the expression grammar to derive the sentence x – 2 * y

  41. Derivation: x – 2 * y

  42. Derivation: x – 2 * y

  43. Derivation • Such a process of rewrites is called a derivation. • Process or discovering a derivations is called parsing

  44. Derivation • Such a process of rewrites is called a derivation. • Process or discovering a derivations is called parsing

  45. Derivation We denote this derivation as:expr→*id – num* id

  46. Derivations • At each step, we choose a non-terminal to replace • Different choices can lead to different derivations.

  47. Derivations • At each step, we choose a non-terminal to replace • Different choices can lead to different derivations.

  48. Derivations • Two derivations are of interest • Leftmost derivation • Rightmost derivation

  49. Derivations • Leftmost derivation: replace leftmost non-terminal (NT) at each step • Rightmost derivation: replace rightmost NT at each step

  50. Derivations • Leftmost derivation: replace leftmost non-terminal (NT) at each step • Rightmost derivation: replace rightmost NT at each step

More Related