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

Slides:



Advertisements
Similar presentations
JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
Advertisements

1 Assignment 3 Jianguo Lu. 2 Task: check whether the a program is syntactically correct /** this is a comment line in the sample program **/ INT f2(INT.
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
Context-Free Grammars Lecture 7
Lexical Analysis Textbook:Modern Compiler Design Chapter 2.1
Tools for building compilers Clara Benac Earle. Tools to help building a compiler C –Lexical Analyzer generators: Lex, flex, –Syntax Analyzer generator:
1 Problem 2 A Scanner / Parser for Simple C. 2 Outline l Language syntax for SC l Requirements for the scanner l Requirement for the parser l companion.
CUP: An LALR Parser Generator for Java
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Compiler Construction Parsing I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
1 Scanning Aaron Bloomfield CS 415 Fall Parsing & Scanning In real compilers the recognizer is split into two phases –Scanner: translate input.
A brief [f]lex tutorial Saumya Debray The University of Arizona Tucson, AZ
CS 536 Spring Learning the Tools: JLex Lecture 6.
贺天行.  First please download to appetitzer in our webpage  This tutorial will be mainly about codes provided in the appetizer.
Parsing arithmetic expressions Reading material: These notes and an implementation (see course web page). The best way to prepare [to be a programmer]
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Compilers: lex/3 1 Compiler Structures Objectives – –describe lex – –give many examples of lex's use , Semester 1, Lex.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
CPSC 388 – Compiler Design and Construction Scanners – JLex Scanner Generator.
COMP 3438 – Part II - Lecture 2: Lexical Analysis (I) Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
CS453 LectureIntroduction1 CS453 Compiler Construction Instructor:Wim Bohm Computer Science Building 470 TA: tba
1 Languages and Compilers (SProg og Oversættere) Lexical analysis.
JLex Lecture 4 Mon, Jan 24, JLex JLex is a lexical analyzer generator in Java. It is based on the well-known lex, which is a lexical analyzer generator.
By Neng-Fa Zhou Lexical Analysis 4 Why separate lexical and syntax analyses? –simpler design –efficiency –portability.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
Compiler Construction Lexical Analysis. 2 Administration Project Teams Project Teams Send me your group Send me your group
1 Using Lex. 2 Introduction When you write a lex specification, you create a set of patterns which lex matches against the input. Each time one of the.
Introduction to Lexical Analysis and the Flex Tool. © Allan C. Milne Abertay University v
1 Lex & Yacc. 2 Compilation Process Lexical Analyzer Source Code Syntax Analyzer Symbol Table Intermed. Code Gen. Code Generator Machine Code.
The ScC grammar example1 Contoh pemakaian Cup - Parser Generator available at telaga.cs.ui.ac.id/WebKuliah/IKI40800/newstuffs/CupScCGrammar.ppt see also.
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
CS 614: Theory and Construction of Compilers Lecture 9 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes.
Compiler Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
1 JavaCUP JavaCup (Construct Useful Parser) is a parser generator; Produce a parser written in java, itself is also written in Java; There are many parser.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
CS453 LectureIntroduction1 CS453 Compiler Construction Original Design: Michelle Strout Instructor:Wim Bohm
1 Steps to use Flex Ravi Chotrani New York University Reviewed By Prof. Mohamed Zahran.
CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
©SoftMoore ConsultingSlide 1 Lexical Analysis (a.k.a. Scanning)
Elements of Computing Systems, Nisan & Schocken, MIT Press, Chapter 10: Compiler I: Syntax Analysis slide 1www.nand2tetris.org Building.
MiniJava Compiler A multi-back-end JIT compiler of Java.
1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University.
CS 614: Theory and Construction of Compilers Lecture 8 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
CS 614: Theory and Construction of Compilers Lecture 4 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
CUP: An LALR Parser Generator for Java
Tutorial On Lex & Yacc.
Languages and Compilers (SProg og Oversættere)
Java CUP.
CS 614: Theory and Construction of Compilers
RegExps & DFAs CS 536.
JLex Lecture 4 Mon, Jan 26, 2004.
Fall Compiler Principles Lecture 4: Parsing part 3
CPSC 388 – Compiler Design and Construction
Lexical Analysis Why separate lexical and syntax analyses?
CPSC 388 – Compiler Design and Construction
Lecture 5: Lexical Analysis III: The final bits
Compilers B V Sai Aravind (11CS10008).
Fall Compiler Principles Lecture 4: Parsing part 3
Compiler Structures 3. Lex Objectives , Semester 2,
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Regular Expressions and Lexical Analysis
Compiler Design 3. Lexical Analyzer, Flex
Presentation transcript:

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/