CSS 448/548 Yacc Lab

CSS 448/548 Yacc Lab

CSS 448/548 Yacc Lab Write a yacc file to convert infix expressions to postfix and to evaluate them. Use the (<expr>, <term>, <factor>) grammar. Expressions with negative factors (which must be in parentheses), the unary minus, must be added to the grammar. Your code is to write the expression in postfix notation and produce the answer to the expression. Use standard postfix notation (E.g., "2+3" infix is "2 3 +" postfix). Show a unary minus as a 'm' (E.g., 2+(‐3) infix is "2 3 m +" postfix). SAMPLE INPUT: 2+3 3 - (4*5) + 6 34+678-(((2+3)-4)*8/2-1)*97 34+(78-(((234+5)-8)*9/2-9)*7+((3+5)-(2-1)*3))*0 2 + (-5) - (-4) 2+ (-(1+3)) SAMPLE OUTPUT: 2 3 + = 5 3 4 5 * - 6 + = -11 34 678 + 2 3 + 4 - 8 * 2 / 1 - 97 * - = 421 34 78 234 5 + 8 - 9 * 2 / 9 - 7 * - 3 5 + 2 1 - 3 * - + 0 * + = 34 2 5 m + 4 m - = 1 2 1 3 + m + = -2 Notes ‐‐ Use the <expr>, <term>, <factor> grammar, allowing negative factors in parentheses (unary minus). The grammar (without the unary minus), starting with one expression is as follows: <expr> → <expr> + <term> | <expr> - <term> | <term> <term> → <term> * <factor> | <term> / <factor> | <factor> <factor> → <number> | ( <expr> ) <number> → DIGIT | <number> DIGIT ‐‐ Parts of what you do are similar to the samples. This lab is closest to y3 with the token DIGIT. You won't have other yacc declarations as in y2 (%left) because associativity and precedence are handled in the grammar itself. ‐‐ Log onto a linux machine and, if desired, create a directory to put your yacc stuff into: mkdir yacc Change to the directory: cd yacc (Note that 'cd' by itself always takes you to your home directory.) Copy the “hw” yacc file, main.cpp, the simple makefile that has main, and file “hwdata” which holds data from the web. The file "hw" is a copy of my "y3". There is a simple makefile for your use. It assumes that your yacc file is called "hw" and makes an executable file called "hw.out" . To try it out, enter: make To check out the C code produced, file y.tab.c, enter: more y.tab.c (When using the 'more' command, the 'space' displays the next screen, 'enter' for one line scroll, 'q' to quit.) To execute and enter expressions, enter: hw.out To do the lab, edit using pico: pico hw (Note that in pico, control‐c tells the line number.) Each time you make changes in "hw", Use 'make' to obtain an updated executable. ‐‐ You can run the executables interactively or using a data file. The file, data1, contains several expressions. For example, you can enter: y3.out < data1 and it will work as if you entered these expressions. You can do the same thing for the yacc program that you write. A file sample.data contains the data in the above sample input, so you can enter: hw.out < sample.data ‐‐ When you are completely done, script your yacc file and output by doing the following: script cat hw touch hw make Mail the typescript file to the css448 account: cat hw.data [email protected] hw.out < hw.data control-d .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    1 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us