CS 614: Theory and Construction of Compilers Lecture 7 Fall 2003 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.
A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Abstract Syntax Mooly Sagiv html:// 1.
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.
Mooly Sagiv and Roman Manevich School of Computer Science
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Context-Free Grammars Lecture 7
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
Compiler Construction Parsing I Ran Shaham and Ohad Shacham School of Computer Science Tel-Aviv University.
Compilers: Yacc/7 1 Compiler Structures Objective – –describe yacc (actually bison) – –give simple examples of its use , Semester 1,
1 Languages and Compilers (SProg og Oversættere) Parsing.
贺天行.  First please download to appetitzer in our webpage  This tutorial will be mainly about codes provided in the appetizer.
LR Parsing Compiler Baojian Hua
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
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.
Top-Down Parsing - recursive descent - predictive parsing
Automated Parser Generation (via CUP)CUP 1. High-level structure JFlexjavac Lexer spec Lexical analyzer text tokens.java CUPjavac Parser spec.javaParser.
CS 614: Theory and Construction of Compilers Lecture 10 Fall 2002 Department of Computer Science University of Alabama Joel Jones.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Miscellaneous 컴파일러 입문.
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.
Introduction to Yacc Ying-Hung Jiang
Prof. Necula CS 164 Lecture 8-91 Bottom-Up Parsing LR Parsing. Parser Generators. Lecture 6.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
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 Principles Fall Compiler Principles Lecture 6: Parsing part 5 Roman Manevich Ben-Gurion University.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
PL&C Lab, DongGuk University Compiler Lecture Note, MiscellaneousPage 1 Yet Another Compiler-Compiler Stephen C. Johnson July 31, 1978 YACC.
Compiler Principles Fall Compiler Principles Lecture 5: Parsing part 4 Roman Manevich Ben-Gurion University.
CS 614: Theory and Construction of Compilers Lecture 5 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 6: LR grammars and automatic parser generators.
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.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
9-December-2002cse Tools © 2002 University of Washington1 Lexical and Parser Tools CSE 413, Autumn 2002 Programming Languages
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
CUP: An LALR Parser Generator for Java
Tutorial On Lex & Yacc.
Programming Languages Translator
Java CUP.
Chapter 4 Syntax Analysis.
Context-free Languages
Regular Languages.
Compiler Lecture 1 CS510.
Fall Compiler Principles Lecture 4: Parsing part 3
CS416 Compiler Design lec00-outline September 19, 2018
CPSC 388 – Compiler Design and Construction
CPSC 388 – Compiler Design and Construction
Bison Marcin Zubrowski.
CS 3304 Comparative Languages
Subject: Language Processor
CS416 Compiler Design lec00-outline February 23, 2019
Fall Compiler Principles Lecture 4: Parsing part 3
Compiler Lecture Note, Miscellaneous
Compiler Design Yacc Example "Yet Another Compiler Compiler"
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
CMPE 152: Compiler Design December 4 Class Meeting
Presentation transcript:

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

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

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?

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

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 */

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

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)

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

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)

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

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;

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

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

Lecture 7©2003 Joel Jones14 Reading & Questions for Next Class Java Cup Web page ava/CUP/ ava/CUP/