1 / 5

Thinking about grammars

An expression language involving integers 1, 2, and 3 and the "+" operator, with rules for left-associativity. Visualize the grammar using parse trees.

mpedersen
Download Presentation

Thinking about grammars

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. Thinking about grammars • Consider an expression language involving integers 1, 2 and 3 and the + operator • These rules make the + operator left associative <e> ::= <int> | <e> + <int> <int> ::= 1 | 2 | 3 • Note that using the “|” notation obscures the fact that there are really five rules <e> ::= <int> <e> ::= <e> + <int> <int> ::= 1 <int> ::= 2 <int> ::= 3

  2. A graphical view e int int int • Each rule is a little tree with a non-terminal as its root and children which are non-terminals or terminals • Here’s how we we might visualize the grammar using ovals for non-terminals and strings as terminals e + int 1 2 3 e int <e> ::= <int> <e> ::= <e>+<int> <int> ::= 1 <int> ::= 2 <int> ::= 3

  3. Generating a string & parse tree • Create a parse tree P consisting ofthe node • Repeat until P has no non-terminals leaf nodes • Select a leaf node L that is a non-terminal • Select a grammar tree T that has the same non-terminal as its root and make a copy of it • Replace the leaf L in P with the copy of T e

  4. 1 + 2 + 3 e e e e e int int int int int int Here’s an example showing the parse tree for 1+2+3 int e + int int e + int e + int 2 3 1 1 2 3 the grammar rules the parse tree

  5. 1 + 2 + 3 e e e e int int int int int int Here’s an example showing the derivation of 1+2+3 int int int int 1 1 1 2 2 3 e e e e e e e e e e e e e e + + int int e + int e e + + int int e e e + + + int int int e e e + + + int int int

More Related