1 / 21

Chapter 3

Chapter 3. Context-Free Grammars and Parsing. parser. syntax tree. sequence of tokens. The Parsing Process. Duties of parser: Determine correct syntax Build Syntax Tree (if necessary) Error reporting and recovery. Context-Free Grammars. Specification of programming language

bardia
Download Presentation

Chapter 3

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. Chapter 3 Context-Free Grammars and Parsing

  2. parser syntax tree sequence of tokens The Parsing Process • Duties of parser: • Determine correct syntax • Build Syntax Tree (if necessary) • Error reporting and recovery

  3. Context-Free Grammars • Specification of programming language • Somewhat like regular expressionsExcept… • Definitions can be recursive • No meta-symbol for repetition

  4. Context-Free GrammarExample exp  exp op exp | ( exp ) | num op  +|-|*

  5. Derivations and Languages • Context-free grammar rules determine the set of legal strings of token symbols for the structures defined by the rules. • Example: “(34-3)*42” corresponds to(num – num) * num • Example: “(34-3*42” is not a legal expression • Grammar rules determine the legal strings of token symbols by means of derivations

  6. Derivation • A derivation is a sequence of replacements of structure names by choices on the right-hand side of grammar rules.

  7. Derivation Example • exp  exp op exp [exp  exp op exp] •  exp op num [exp  num] •  exp * num [op  *] •  (exp) * num [exp  (exp)] •  (exp op exp) * num [exp  exp op exp] •  (exp op num) * num [exp  num] •  (exp – num) * num [op  -] •  (num – num) * num [exp  num]

  8. Other Context-Free Grammars • E  (E) | a • E  E +a • statement  if-stmt | otherif-stmt  if (exp) statement |if (exp) statement else statementexp  0 | 1

  9. Right recursive and Left recursive • Consider the grammarA  A a | a • A  Aa  Aaa  Aaaa … • Conversely considerA  A a | a • A  aA  aaA  aaaA • The first grammar is left recursiveThe second is right recursive

  10. empty • ‘ε’ matches the empty string • so the regular expression: a*Looks like A  Aa | ε • What does the following grammar doA  (A)A | ε

  11. Another Example • statement  if-stmt | otherif-stmt  if (exp) statement |if (exp) statement else-partelse-part  else statement | εexp  0 | 1

  12. Another Example • stmt-sequence  stmt; stmt-sequence | stmtstmt  s • stmt-sequence  stmt; stmt-sequence | εstmt  s Notice any differences??

  13. Parse Trees • Consider the string of tokens( num – num ) * num • How many derivations are there?? • Parse-Trees show the derivation without worrying about ordering

  14. Parse Trees • A parse tree corresponding to a derivation • labeled tree • interior nodes are • labeled by nonterminals • leaf nodes are • labeled by terminals • children of each internal node • represent the replacement of the associated nonterminal • one step of the derivation

  15. exp exp exp op number + number Parse-Tree exp  exp op exp | ( exp ) | num op  +|-|* what is the corresponding derivation?

  16. Left-most derivation • A left-most derivation is a derivation in which the leftmost nonterminal is replaced at each step in the derivation.

  17. exp exp exp op number + number Parse-Tree exp  exp op exp | ( exp ) | num op  +|-|* what is the corresponding left-most derivation?

  18. Abstract Syntax Trees • parse trees contain more information than is absolutely necessary for a compiler to produce executable code • A shorthand notation for a parse tree is called an abstract syntax tree

  19. exp exp exp op number + number Example If the string to parse was “3+4” + 3 4 The parse tree The abstract syntax tree tree

  20. Other Examples • What about parse trees and abstract syntax trees for the following grammar? • statement  if-stmt | otherif-stmt  if (exp) statement |if (exp) statement else statementexp  0 | 1

  21. Other Examples • What about parse trees and abstract syntax trees for this grammar? • stmt-sequence  stmt; stmt-sequence | stmtstmt  s

More Related