CS453 Colorado State University ========================================== Regression testing and Ambiguity ========================================== --------------- The plan Overview of PA3 Regression testing Expressions in MeggyJava Expression ambiguity Fixing ambiguity in the grammar Simpler fix provided by JavaCUP --------------- Overview of PA3 Building a lexer with JLex Building a parser with JavaCUP Using syntax-directed evaluation of constant integer and byte expressions with parser actions. Generating main prologue and epilogue and AVR assembly with syntax-directed translation, again with parser actions. Creating a regression testing framework and demonstrating it in recitation NEXT Friday, February the 25th. ------------------ Regression Testing Main ideas -an automated testing framework -every time a modification is made to the program you can test that all of the previous functionality still works -you can easily add new tests to the framework Examples of regression testing frameworks you have written in the past? Programs you will be writing Meggy Java compiler for PA3 grammar java -jar MJPA3_groupname.jar InputFile.java // output will be InputFile.java.s Other tools meggy.Meggy static methods javac InputFile.java java InputFile // output is a trace to standard out Meggy AVR simulator java -jar MJSIM.jar -b -f InputFile.java.s Brainstorm some ideas for a regression testing framework for your Meggy Java compiler. Some unix scripting commands // Put the below at the top of the file to indicate what // shell the script should run under. #!/bin/sh echo "A string that will be printed to standard out" # A loop that ... for filename in `ls *.java` do # executes all of the commands for each value of $filename. java -jar MJPA3_groupname.jar $filename done # An if statement that checks the output from the previous command. javac $filename if [$? -eq 0] then # If javac returned a 0 (which means success in unix), # then the commands in this body will execute. fi ------------- Expression evaluation in MeggyJava PA3 The expression grammar for PA3 Expression ::= Expression ("+" | "-" | "*" ) Expression | "(" "byte" ")" Expression | | | "(" Expression ")" (1) expr -> expr + expr (2) expr -> expr - expr (3) expr -> expr * expr (4) expr -> ( byte ) expr (5) expr -> NUM (6) expr -> COLOR (7) expr -> ( expr ) -------------------------- Ambiguity reprise -now do a different derivation for 42 + 7 * 6 expr1 -> expr2 * expr3 used production (3) -> expr4 + expr5 * expr3 used production(1) -> NUM(42) + NUM(7) * NUM(6) used production (4), 3 times -> see tree and step through computation - example expression: 42 + 7 * 6 (slides 5 through 7) - ambiguity of expression grammar is a problem (slide 8 has alternative parse tree for 42 + 7 * 6) ------------------------------- Example of how to fix ambiguity by modifying the grammar -grammar in Figure 2.28 in book is NOT ambiguous expr -> expr + term | term term -> term * factor | factor factor -> ( expr ) | num -> do derivations, draw parse trees, and do evaluation of some expressions in class. Do an example with parentheses. Questions -> what if we switch + and * in the unambiguous grammar? -> how can we incorporate - into the above grammar? -> what if expr goes to term - expr? -> what if factor goes to something other than (expr)? -> how could we handle (byte) casts? (byte) 3 * 2 Main concepts precedence associativity ----------------------------- Quick Fix Provided by JavaCUP ... terminal PLUS, MINUS, TIMES, LPAREN, RPAREN, NUMBER; non terminal expr; precedence left PLUS,MINUS; precedence left TIMES; expr ::= NUMBER:n {: RESULT = n; :} | expr:e1 PLUS expr:e2 {: RESULT = e1 + e2; :} | expr:e1 MINUS expr:e2 {: RESULT = e1 - e2; :} | expr:e1 TIMES expr:e2 {: RESULT = e1 * e2; :} | LPAREN expr:e1 RPAREN {: RESULT = e1; :} Can use the precedence keyword and left and right associativity key words. - precedence should be listed from lowest to highest (yes opposite of precedence in JLex files) Note the following: - terminals in JavaCUP are typically written with all capitals - ::= is the right arrow in the production and | indicates another production rule for the nonterminal expr - The {: :} syntax are brackets around the action being associated with the production rule it follows. - The :varname syntax give a name to the value being "passed up" the implicit parse tree. The varname can then be used in the action associated with the production. - RESULT is a special variable name that represents the value being associated with the lhs nonterminal of the production and being passed up the implicit parse tree. ------------------------ mstrout@cs.colostate.edu, 2/17/11