Upload
isabel-byrd
View
214
Download
0
Embed Size (px)
Citation preview
CS 614: Theory and Construction of Compilers
Lecture 7
Fall 2003
Department of Computer Science
University of Alabama
Joel Jones
Lecture 7 ©2003 Joel Jones 2
Outline
(f)lex Diversion Introduction to Java Cup Reading & Questions for Next Class
Lecture 7 ©2003 Joel Jones 3
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?
Lecture 7 ©2003 Joel Jones 4
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
Lecture 7 ©2003 Joel Jones 5
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 */
Lecture 7 ©2003 Joel Jones 6
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();}
Lecture 7 ©2003 Joel Jones 7
Fundamental of LALR Parsing Bottom-up
Unlike recursive-descent which is top-down Model is finite-automata with a stack
The parsers state represents what is expected next in the input Transition to next state based upon the top of the stack, next
(1) input symbol, and current state Stack contains symbols, both terminals and nonterminals On transition, stack may pushed (shift), popped (reduce), or
unchanged (epsilon)
Lecture 7 ©2003 Joel Jones 8
Purpose of Parser Generator
Translate a specification of a grammar into a parser
Most parser generators are for Context-free Grammars (CFG) and produce LALR(1) or LL(1) parsers
Grammar specification is annotated with action routines that are executed when a reduction is made
Lecture 7 ©2003 Joel Jones 9
Java Cup
Accepts specification of a CFG and produces an LALR(1) parser (expressed in Java) with action routines expressed in Java
Similar to yacc in its specification language, but with a few improvements (better name management)
Lecture 7 ©2003 Joel Jones 10
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
Lecture 7 ©2003 Joel Jones 11
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;
Lecture 7 ©2003 Joel Jones 12
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.)
Lecture 7 ©2003 Joel Jones 13
Running Java Cup Manually (cont.) Build driver program
java ParseDemo.java
Run driver program java ParseDemo << EOF
Lecture 7 ©2003 Joel Jones 14
Reading & Questions for Next Class Java Cup Web page
http://www.cs.princeton.edu/~appel/modern/java/CUP/