⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javacoco.htm

📁 cocorj09-一个Java语言分析器
💻 HTM
📖 第 1 页 / 共 5 页
字号:
MUL       Multiply            j=Pop(); i=Pop(); Push(i*j);
EQU       Compare if equal    j=Pop(); i=Pop(); if (i==j) Push(1); else Push(0);
LSS       Compare if less     j=Pop(); i=Pop(); if (i<j) Push(1); else Push(0);
GTR       Compare if greater  j=Pop(); i=Pop(); if (i>j) Push(1); else Push(0);
CALL l,a  Call procedure      slink=Frame(l); Push(slink); Push(base); Push(pc+2);
                              pc=a; base=top-3;
RET       Return from proc.   top=base+3; pc=Pop(); base=Pop(); dummy=Pop();
RES i     Reserve frame space top=top+i;
JMP a     Jump                pc=a;
FJMP a    False jump          if (Pop()==1) pc=adr;
HALT      End of the program  Halt;
NEG       Negation            Push(-Pop());
READ l,a  Read integer        stack[Frame(l)+a]=Read();
WRITE     Write integer       Writeln(Pop());
</PRE>

<P>The function <I>Frame(l)</I> returns the base address of the stack frame which
is (statically) <I>l</I> levels up in the stack. <I>l</I>=0 means the current frame,
<I>l</I>=1 means the statically surrounding frame, etc. The main program is also
considered as a procedure with a stack frame. Figure 2 shows the format
of a stack frame. </P>

<P><IMG SRC="Fig3.gif" ><BR>
<BR>
<B>Fig.2</B> Format of a stack frame </P>

<P>For example, the source code instruction </P>

<PRE>   i := i + 1
</PRE>

<P>is translated into the code </P>

<PRE>   LOAD 0,3  load value of i (at address 3 in current frame)
   LIT 1     load constant 1
   ADD
   STO 0,3   store result to i
</PRE>

<H3>Classes</H3>

<P>Figure 3 shows the primary classes of the compiler (an arrow from A to B
means that class A is used by class B). </P>

<P><IMG SRC="Fig2.gif" ><BR>
<BR>
<B>Fig.3</B> classes of the Taste compiler </P>

<P>The classes in the rectangle are generated by Coco/R, the others are
written by hand. The classes have the following responsibilities: </P>

<TABLE>
<TR>
<TD valign=top><A HREF="../Taste/Comp.java">Comp</A> </TD>

<TD>Driver class. This obtains the name of the source file and calls
the parser and the interpreter. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Taste/Parser.java">Parser</A> </TD>

<TD>Recursive descent parser generated from the attributed grammar
<A HREF="../Taste/Taste.ATG">Taste.ATG</A>.
The parser contains the semantic actions from the attributed grammar. They are
called and executed during parsing and do the actual compilation work.

</TD>
</TR>

<TR>
<TD valign=top><A HREF="../Taste/Scanner.java">Scanner</A> </TD>

<TD>Scanner generated from <A HREF="../Taste/Taste.ATG">Taste.ATG</A>. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Taste/ErrorStream.java">ErrorStream</A> </TD>

<TD>Error handler generated from <A HREF="../Taste/Taste.ATG">Taste.ATG</A> to
produce meaningful syntax error messages. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Taste/TL.java">TL</A> </TD>

<TD>Symbol table with methods to handle scopes and to store and retrieve
semantic information associated with identifiers. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Taste/TC.java">TC</A> </TD>

<TD>Code generator with methods for emitting instructions and storing these
in the simulated memory for later interpretation. This class also contains
the interpreter method and its related data structures. </TD>
</TR>
</TABLE>

<P>For instructions on where to download all these components together
see <A HREF="#Sources">Section 7.</A>
</P>


<H3>Summary - how to build your own compiler</H3>

<P>If you want to build a similar compiler, but for a source language of your
own, proceed as follows: </P>

<OL>
<LI>Describe the structure of the lexical tokens (identifiers, numbers,
etc) as exemplified in the CHARACTERS and TOKENS sections of the file
<A HREF="../Taste/Taste.ATG">Taste.ATG.</A></LI>

<LI>Describe the phrase structure and the translation process of the compiler in the form of
an attributed grammar, as exemplified in the PRODUCTIONS section of the
file <A HREF="../Taste/Taste.ATG">Taste.ATG.</A></LI>

<LI>Feed the complete ATG file to Coco/R to get the scanner, parser and syntax
error reporter of the compiler (in the form of Java classes named
Scanner.java, Parser.java and ErrorStream.java).</LI>

<LI>Write a driver program with a main method.  This has to initialize the
scanner and call the parser.  Preferably also derive an application-specific
subclass of ErrorStream that will handle semantic error messages in a
meaningful way. These classes are exemplified in the files <A
HREF="../Taste/Comp.java">Comp.java</A> and <A
HREF="../Taste/MergeErrors.java">MergeErrors.java</A>. </LI>

<LI>Write any other classes needed by the compilation process. These classes
typically contain methods that are called from the semantic actions of the
attributed grammar. For example, the Taste compiler has a symbol table class
(<A HREF="../Taste/TL.java">TL.java</A>) and a code generator (<A
HREF="../Taste/TC.java">TC.java</A>). </LI>

<LI>Compile the generated parts and the hand-written parts to get a running
compiler. </LI>
</OL>

<A NAME="Apps"></A>
<H2>6. Useful applications of Coco/R</H2>

<P>Coco/R has been used for various tasks (not only for writing compilers).
The following list should give you some ideas about useful applications: </P>

<UL>
<LI>An analyzer for the static complexity of programs. The analyzer evaluates
the kind of operators and statements, the program nesting and the use of
local and global variables to obtain a measure of the program complexity
and an indication of whether the program is well structured. </LI>

<LI>A cross reference generator which lists all occurrences of the objects
in a program according to their scope, together with information where the
objects have been assigned a value and where they have been referenced.
</LI>

<LI>An &quot;intelligent&quot; pretty printer which uses the structure
and the length of statements for proper indentation. </LI>

<LI>A program which generates an index for books and reports. The index
is generated from relations between page numbers and keywords entered in
an appropriate way. </LI>

<LI>The front end of a syntax oriented editor. A program is translated
into a tree representation which is the internal data structure of the
editor. </LI>

<LI>A program that builds a repository of symbols and their relations in
a program. The repository is accessed by a case tool. </LI>
</UL>

<P><A NAME="Sources"></A></P>

<H2>7. Sources of Coco/R</H2>

<P>The sources of Coco/R for Java are as follows: </P>

<TABLE>
<TR>
<TD valign=top><A HREF="../Coco/Coco.ATG">Coco.ATG</A> </TD>

<TD>The attributed grammar. Describes the processing of a compiler description.
</TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/Comp.java">Comp.java</A> </TD>

<TD>Driver class. Initializes the scanner and calls the parser. This file
also contains the custom error message class CocoErrors. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/Scanner.java">Scanner.java</A> </TD>

<TD>Scanner generated from Coco.ATG. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/Parser.java">Parser.java</A> </TD>

<TD>Parser generated from Coco.ATG. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/ErrorStream.java">ErrorStream.java</A> </TD>

<TD>A class for printing parser error messages. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/MergeErrors.java">MergeErrors.java</A> </TD>

<TD>A class for merging parser error messages with the source text.  By
editing Comp.java this class can be used in preference to ErrorStream. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/Tab.java">Tab.java</A> </TD>

<TD>Symbol table of Coco. Stores information about terminals and nonterminals.
</TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/DFA.java">DFA.java</A> </TD>

<TD>This class builds the scanner automaton and generates the scanner source
file. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/ParserGen.java">ParserGen.java</A> </TD>

<TD>This class builds a syntax graph of the grammar rules and generates
the parser source file from it. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/Sets.java">Sets.java</A> </TD>

<TD>An auxiliary class with operations for sets. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Coco/Trace.java">Trace.java</A> </TD>

<TD>An auxiliary class to produce trace output. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Frames/Scanner.frame">Scanner.frame</A> </TD>

<TD>This is the frame file from which the scanner is generated. Coco/R inserts
compiler-specific parts into this frame file. </TD>
</TR>

<TR>
<TD valign=top><A HREF="../Frames/Parser.frame">Parser.frame</A> </TD>

<TD>This is the frame file from which the parser is generated. Coco/R inserts
compiler-specific parts into this frame file. </TD>
</TR>
</TABLE>

<P>If you would prefer to download a ZIP file with the latest .java source and
.class files and the .frame and .ATG files for Coco/R and Taste, you
can get it from here
<A HREF="ftp://cs.ru.ac.za/pub/coco/jcoco109win.zip"> in Win95 ZIP format</A>,
or from here
<A HREF="ftp://cs.ru.ac.za/pub/coco/jcoco109unix.zip"> in Unix ZIP format</A>,
or from here
<A HREF="ftp://cs.ru.ac.za/pub/coco/jcoco109.tgz"> in Unix .tgz format</A>,
or from here
<A HREF="ftp://cs.ru.ac.za/pub/coco/jcoco108mac.zip"> in Macintosh ZIP format</A>
(these formats differ only in the way in which line marks appear in the source
files).

The ZIP files must be decompressed with a tool that will retain long file names
and directory structures.</P>

<A NAME="port"></A> <H2>8. Portability issues</H2>

<P>Some of the source files of Coco/R itself, and for the systems it
generates, contain multiple classes. This can be a problem for some Java
compilers (e.g. JDK 1.1.5), which expect each class to be in a separate file.
If your compiler produces error messages, you can either put every class into
a separate file or simply compile the application twice. The second compilation will
usually succeed, because the compiler builds up dependency information during
the first compilation.</P>

<P>The sources of Taste and of Coco/R and the frame files are supplied in a
form that should be Java 1.1 compatible.  They are, however, clearly marked
with the changes (relating to input and output streams) that will need to be
effected to produce source code acceptable to a Java 1.0 compiler.</P>

<P>Coco/R has been developed so that it can accept input and frame files in any of
the three common text file formats (Unix, MS-DOS, Macintosh); it generates
output in the format of the host system.</P>

<A NAME="Lit"></A> <H2>9. Literature</H2>

<P><A HREF="ftp://ftp.ssw.uni-linz.ac.at/pub/Reports/Coco.Report.ps.Z">Coco.Report.ps.Z<BR>
</A>The original report on Coco/R, including an implementation description.
</P>

<P><A HREF="ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Coco.Paper.ps.Z">Coco.Paper.ps.Z<BR>
</A>H.M&ouml;ssenb&ouml;ck: A Generator for Production Quality Compilers.
Lecture Notes in Computer Science 477, Springer-Verlag, 1990. </P>

<P><A HREF="http://cs.ru.ac.za/homes/cspt/compbook.htm">Compiler book using
Coco/R<BR>
</A>P.D.Terry: <I>Compilers and Compiler Generators - An Introduction With
C++</I>. International Thomson Computer Press, 1997<BR>
(This book uses the C++ version of Coco/R, and also supplies case studies for
the Modula-2 and Pascal versions.) </P>

</BODY>
</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -