1 / 8

CS 280 Data Structures

CS 280 Data Structures. Professor John Peterson. Lexer Project. Questions? Must be in by Friday – solutions will be posted after class The next project is nearly ready – look in the wiki. Parse Trees.

geona
Télécharger la présentation

CS 280 Data Structures

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. CS 280Data Structures Professor John Peterson

  2. Lexer Project Questions? Must be in by Friday – solutions will be posted after class The next project is nearly ready – look in the wiki.

  3. Parse Trees One of the big deals in computer science is context free languages – we use these to create recursive structures (trees) from linear ones (strings or sequences). There is a whole lot of theory underneath – we’ll skip most of it and concentrate on the practical stuff.

  4. Example: English

  5. The Problem Given: • A sequence of tokens • A grammar that gives structure to these tokens Produce: • A parse tree that covers the sequence

  6. Grammars • Names: the left side of a production is a name – this name can be used in other productions • Constants: specific pieces of the underlying token-level language • Sequence: x y means that y follows x • Choice: (x | y) means either x or y may appear here • Optionals: [x] means x may appear here • Repetition: (x)* means that an arbitrary number of x’s are repeated

  7. Example: Java Tokens: a = a + b * c; Grammar: statement = assignment assignment = var‘=‘addexp‘;’ addexp = mulexp (‘+’mulexp)* mulexp = aexp (‘*’aexp)* aexp = var | num | ‘(‘addexp‘)’

  8. How Does this Work? You need to know where to start (“statement”) This grammar is constructed so that you can always decide what to do based on the next token (peek). When you have a choice, always go as far as possible. If you get to a place where the current token doesn’t fit into the grammar, you have a “parse error”.

More Related