Labs

Lab 6.2: Parsing Out

Rosetta Help

Print
References

The previous exercise showed you how simple symbolic information can be represented in binary1 form. Still it is not at all clear how a statement in a high-level language2 like JavaScript3 gets translated into PIPPIN-like form. The program below, named Rosetta, is capable of this type of translation. Rosetta can perform both parsing4 and code generation on a very specific kind of statement-those in the form of an equations, much like an assignment operation in JavaScript. In our case, though the statement can refer only to the variables W, X, Y and Z, and to the operators +, -, * (for multiplication) and /. The equal sign "=" is used to designate assignment, and parentheses may be used to group expressions. So, the statement

W = X  * (Z + Y)
is a legal one for our program.
  1. Enter this above statement into the equation window near the top of the program window. Then, click the Set Equation button to indicate that you are done typing.

  2. Click on the Parsing button

  3. Click on the Play button (the right facing arrow) to have the program produce the parse tree for our statement.

  4. Click the Reset button to reset the parser.

  5. Now, click on the Step button to watch the parsing process in more detail. After the first click, the parser recognizes the Z and Y as tokens, and sees that they are to be combined by an addition operator. Each token is considered to be an "expression," and expressions can be combined by operators to form more complex expressions. That's what the notation along the right side of the program window is describing.

  6. Click the step button three more times, watching as each part of our complicated statement get described as a series of operations on its token expressions. The notation S -> E = E indicates that our statement was ultimately recognized as an assignment statement between two expressions.

  7. Enter the following statement into the equation window, and repeat the above steps to watch it get parsed:
    X = (3*Y) + (2/W)
    
  8. Now, let's try to parse a few expressions by hand. For each of the statements below, draw its parse tree on a sheet of paper. Then, use Rosetta to see if you tree is correct.
    1. Y = 5 - Z + X
    2. Y = 5 - (Z + X)
    3. Y = 5 - ((Z + X)/2)


  9. Let's reverse the process again and see if you can match parse trees with their corresponding statements. For each of the parse trees below, write the statement that it represents. Use Rosetta to confirm your answers.
    1. parse tree on p. 226
    2. parse tree on p. 226
    3. parse tree on p. 227
1pp. 203–205, 243, 273–274, 282
2pp. 16, 126, 218-220
3pp. 126, 127, 139–196, 149
4pp. 223, 224–225, 306

Labs

MODULES:


Home Search Resources Objectives Feedback Order Form Credits

Copyright Notice
© 1998 PWS Publishing Company, All Rights Reserved.