CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones
©2002 Joel Jones Outline A Design Problem Java Cup Reading & Questions for Next Class
©2002 Joel Jones A Design Problem You have a binary file that somehow has been corrupted You need to recover as much as possible from the file Pair Up: (Let’s make this interactive—ask me questions) How do you approach this problem? What information do you gather? How do you use this knowledge to build a tool?
©2002 Joel Jones A Brief (f)lex Diversion Despite their name, lexical analyzers can be used on binary files This is somewhat difficult with JLex, due to conversions of byte input to character input
©2002 Joel Jones Reading Binary Files With Flex %{ static void printAddress(char* text); … %} ADDRESS \xFF\xFF\x00\x00\xFF NOTE \xFF\xFF\xFF\xFF\x00 TODO \xFF\xFF\xFF\xFF\x00\x01 EVENT \x55\x54\x55\x54\x55\x54\x00\x00 % [ \t\n]+ /* eat whitespace */ {ADDRESS}[^\0]+ { printAddress(yytext + 8); } {NOTE}[\0\x3c\x22][^\0]+ { printNote(yytext + 6); } {TODO} [^\0]+ { printToDo(yytext + 14); } {EVENT}[^\0]+ { printEvent(yytext + 8); }. /* eat everything else */
©2002 Joel Jones Reading Binary Files With Flex % main(int argc, char** argv) { argv++; argc++; /* skip over program name */ if (argc > 0) yyin = fopen(argv[0], "r"); else yyin = stdin; yylex(); }
©2002 Joel Jones java_cup_spec ::= package_spec import_list code_part init_code scan_code symbol_list precedence_list start_spec production_list Java Cup Specification Structure Great, but what does it mean? Package and import controls Java naming Code and init_code allows insertion of code in generated output Scan code specifies how scanner (lexer) is invoked Symbol list and precedence list specify terminal and non- terminal names and their precedence Start and production specify grammar and its start point
©2002 Joel Jones Example Java Cup Specification (partial) import java_cup.runtime.*; /* Terminals (tokens returned by the scanner). */ terminal SEMI, PLUS, MINUS, TIMES, DIVIDE, MOD; terminal Integer NUMBER; /* Non terminals */ non terminal expr_list, expr_part; /* Precedences */ precedence left PLUS, MINUS; /* The grammar */ expr_list ::= expr_list expr_part | expr_part;
©2002 Joel Jones Running Java Cup Manually Download and build Java Cup export CLASSPATH=.:~/src/java_cup_v10k./INSTALL Run Java Cup on the specification java java_cup.Main Arith.y -or- java_cup Arith.y Run Jlex on scanner specification jlex Number.l Build parser and scanner javac parser.java javac Number.l.java (cont.)
©2002 Joel Jones Running Java Cup Manually (cont.) Build driver program Javac ParseDemo.java Run driver program java ParseDemo << EOF
©2002 Joel Jones Reading & Questions for Next Class Java Cup Web page ava/CUP/ ava/CUP/