Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 Department of Computer Science University of Alabama Joel Jones.

Similar presentations


Presentation on theme: "CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 Department of Computer Science University of Alabama Joel Jones."— Presentation transcript:

1 CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 Department of Computer Science University of Alabama Joel Jones

2 Lecture 7©2003 Joel Jones2 Outline (f)lex Diversion Introduction to Java Cup Reading & Questions for Next Class

3 Lecture 7©2003 Joel Jones3 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?

4 Lecture 7©2003 Joel Jones4 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

5 Lecture 7©2003 Joel Jones5 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 */

6 Lecture 7©2003 Joel Jones6 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(); }

7 Lecture 7©2003 Joel Jones7 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)

8 Lecture 7©2003 Joel Jones8 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

9 Lecture 7©2003 Joel Jones9 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)

10 Lecture 7©2003 Joel Jones10 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

11 Lecture 7©2003 Joel Jones11 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;

12 Lecture 7©2003 Joel Jones12 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.)

13 Lecture 7©2003 Joel Jones13 Running Java Cup Manually (cont.) Build driver program java ParseDemo.java Run driver program java ParseDemo << EOF

14 Lecture 7©2003 Joel Jones14 Reading & Questions for Next Class Java Cup Web page http://www.cs.princeton.edu/~appel/modern/j ava/CUP/ http://www.cs.princeton.edu/~appel/modern/j ava/CUP/


Download ppt "CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 Department of Computer Science University of Alabama Joel Jones."

Similar presentations


Ads by Google