Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that.

Similar presentations


Presentation on theme: "CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that."— Presentation transcript:

1 CS 280 Data Structures Professor John Peterson

2 How Does Parsing 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”.

3 Parsing Theory Not all grammars are “easy” to parse Grammars can handle things like operator precedence Grammars can be ambiguous – we’ll avoid these The grammar “inverts” the recursive “print” for a datatype. There are other ways to represent the same thing – railroad diagrams.

4 Railroads

5 The Tree Parsing creates a tree but the tree isn’t exactly the same as the grammar. You need to decide for each production what needs to be remembered. The signature of each production tells you what kind of thing the parser produces at each point.

6 Parsing We’re going to use the simplest method of parsing: recursive descent Each production becomes a function which processes some tokens from the input stream and returns some value. In general, we have to turn the abstract tree defined by the grammar into a concrete data object. So we need to figure out how to represent each different kind of object associated with the productions.

7 A Simple Math Language All we need are the following: variables Function calls, like f(2, 3) or x+y Built-in functions, like + or sqrt Constants Parenthesis for grouping

8 An Evaluation Tree To create a tree that corresponds to an executable program, we need to figure out what sort of tree nodes are needed. Constants: data (like 1.2) and functions (like +) Variable references: a Function calls: the function is a variable / constant. Definitions, like f(x) = y We’ll wrap these up into an abstract class

9 Using an Abstract Class Big idea: create an abstract class which is extended into specific concrete classes. The abstract class is a name that can stand for any of the concrete subclasses Placing virtual methods in the abstract class indicates that the subclasses must implement these methods. This is the SAME as an interface! Advantage: we can place default method definitions / operations in the abstract class

10 Grammar to Parser Each production becomes a function These function usually don’t take inputs – the token stream (global variable) is the input Other productions are called recursively | needs if / then * needs a while loop Constants need a check that may fail (parse error) Some productions may signal failure in other ways (often by returning null)


Download ppt "CS 280 Data Structures Professor John Peterson. How Does Parsing Work? You need to know where to start (“statement”) This grammar is constructed so that."

Similar presentations


Ads by Google