cs 614: theory and construction of compilers lecture 10 fall 2002 department of computer science...

11
CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

Upload: esmond-king

Post on 01-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

CS 614: Theory and Construction of Compilers

Lecture 10

Fall 2002

Department of Computer Science

University of Alabama

Joel Jones

Page 2: 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

Page 3: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©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?

Page 4: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©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

Page 5: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©2002 Joel Jones

Reading Binary Files With Flex%{static void printAddress(char* text);…%}ADDRESS \xFF\xFF\x00\x00\xFFNOTE \xFF\xFF\xFF\xFF\x00TODO \xFF\xFF\xFF\xFF\x00\x01EVENT \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 */

Page 6: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©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();}

Page 7: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©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

Page 8: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©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;

Page 9: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©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.)

Page 10: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©2002 Joel Jones

Running Java Cup Manually (cont.) Build driver program

Javac ParseDemo.java

Run driver program java ParseDemo << EOF

Page 11: CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones

©2002 Joel Jones

Reading & Questions for Next Class Java Cup Web page

http://www.cs.princeton.edu/~appel/modern/java/CUP/