cs 614: theory and construction of compilers lecture 9 fall 2002 department of computer science...
TRANSCRIPT
CS 614: Theory and Construction of Compilers
Lecture 9
Fall 2002
Department of Computer Science
University of Alabama
Joel Jones
©2002 Joel Jones
Outline
“Everybody Dance One More Time” Finish problem from last class
Introduction to Java Cup Reading & Questions for Next Class
©2002 Joel Jones
Definitions
P
L
M
S T
L
S
L
Program P expressed in language L
Machine M
Translator from S to T expressed in L
Interpreter for S expressed in language L
©2002 Joel Jones
More Tombstone Diagram Problems 3: (Gnu RTL intermediate language; HLL: C, C+
+, Pascal; Machine Code: Alpha, PPC, SPARC; RTL->RTL optimizer; all written in C Install on SPARC, given a C compiler for SPARC Compile P in Pascal, into SPARC machine code Compile using RTL optimizer Cross-compile C++ program Q for PPC machine
code
©2002 Joel Jones
Quick Diagnostic
Pair Up:
• What is a finite automata (finite state machine)?
• What are its components?
©2002 Joel Jones
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)
©2002 Joel Jones
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
©2002 Joel Jones
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)
©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
java ParseDemo.java
Run driver program java ParseDemo << EOF
©2002 Joel Jones
Reading & Questions for Next Class Java Cup Web page
http://www.cs.princeton.edu/~appel/modern/java/CUP/