old-www/LDP/LG/issue41/lopes/lcalc.htm

161 lines
7.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="GENERATOR" CONTENT="Mozilla/4.06 [en] (X11; I; Linux 2.0.34 i686) [Netscape]">
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#FF0000">
/*
<BR>Commented By: Christopher Lopes
<BR>File Name: lcalc.flex
<BR>To Create: > jflex lcalc.flex
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
and then after the parser is created
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> javac Lexer.java
<BR>*/
<P>/* --------------------------<B>Usercode Section</B>------------------------
*/
<BR>&nbsp;
<P>/* import these classes:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; java_cup.runtime.*
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sym&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
--class we create with CUP
<BR>*/
<BR><B>import java_cup.runtime.*;</B>
<BR><B>import sym;</B>
<BR>&nbsp;
<BR>&nbsp;
<P><B>%%</B>
<P>/* -----------------<B>Options and Declarations Section</B>-----------------
*/
<BR>&nbsp;
<P>/* The name of the class JFlex will create will be Lexer.
<BR>&nbsp;&nbsp; Will write the code to the file Lexer.java.
<BR>*/
<BR><B>%class Lexer</B>
<P>/*
<BR>The current line number can be accessed with the variable yyline
<BR>and the current column number with the variable yycolumn.
<BR>*/
<BR><B>%line</B>
<BR><B>%column</B>
<BR>&nbsp;
<P>/* Will switch to a CUP compatibility mode to interface with a CUP
<BR>generated parser.
<BR>*/
<BR><B>%cup</B>
<P><A NAME="decl"></A>
<BR>/*
<BR><B>Declarations</B>
<P>Code between %{ and %}, both of which must be at the beginning of a
line, will
<BR>be copied letter to letter into the lexer class source.&nbsp; Here
you declare
<BR>member variables and functions that are used inside scanner actions.
<BR>*/
<BR><B>%{</B>
<P>&nbsp; /* To create a new java_cup.runtime.Symbol with information about
the current
<BR>&nbsp;&nbsp;&nbsp;&nbsp; token, the token will have no value in this
case. */
<BR>&nbsp; private Symbol symbol(int type) {
<BR>&nbsp;&nbsp;&nbsp; return new Symbol(type, yyline, yycolumn);
<BR>&nbsp; }
<P>&nbsp; /* Also creates a new java_cup.runtime.Symbol with information
about the
<BR>&nbsp;&nbsp;&nbsp;&nbsp; current token, but this object has a value.
*/
<BR>&nbsp; private Symbol symbol(int type, Object value) {
<BR>&nbsp;&nbsp;&nbsp; return new Symbol(type, yyline, yycolumn, value);
<BR>&nbsp; }
<P><B>%}</B>
<P>/*&nbsp; <A HREF="lopes.html#decl">Return to Using JFlex</A>&nbsp; */
<BR><A NAME="macro"></A>
<BR>/*
<BR><B>Macro Declarations</B>
<P>These declarations are regular expressions that will be used latter
in the
<BR>Lexical Rules Section.
<BR>*/
<P>/* A line terminator is a \r (carriage return), \n (line feed), or \r\n.
*/
<BR><B>LineTerminator</B> = \r|\n|\r\n
<P>/* White space is a line terminator, space, tab, or line feed. */
<BR><B>WhiteSpace</B>&nbsp;&nbsp;&nbsp;&nbsp; = {LineTerminator} | [ \t\f]
<P>/* A literal integer is is a number beginning with a number between
one and nine
<BR>followed by zero or more numbers between zero and nine or just a zero.
*/
<BR><B>dec_int_lit</B> = 0 | [1-9][0-9]*
<P>/* A identifier integer is a word beginning a letter between A and Z,
a and z,
<BR>or an underscore followed by zero or more letters between A and Z,
a and z,
<BR>zero and nine, or an underscore. */
<BR><B>dec_int_id</B> = [A-Za-z_][A-Za-z_0-9]*
<P><B>/*&nbsp;</B> <A HREF="lopes.html#decl">Return to Using JFlex</A>&nbsp;
*/
<BR>&nbsp;
<P><B>%%</B>
<BR><A NAME="rule"></A>
<BR>/* ------------------------<B>Lexical Rules Section</B>----------------------
*/
<P>/*
<BR>This section contains regular expressions and actions, i.e. Java code,
that
<BR>will be executed when the scanner matches the associated regular expression.
*/
<P>/* YYINITIAL is the state at which the lexer begins scanning.&nbsp;
So these regular
<BR>expressions will only be matched if the scanner is in the start state
<BR>YYINITIAL. */
<P><B>&lt;YYINITIAL></B> {
<P>/* Return the token SEMI declared in the class sym that was found. */
<BR>";"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ return symbol(sym.SEMI); }
<P>/* Print the token found that was declared in the class sym and then
return it.*/
<BR>"+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(" + "); return symbol(sym.PLUS); }
<BR>"-"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(" - "); return symbol(sym.MINUS); }
<BR>"*"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(" * "); return symbol(sym.TIMES); }
<BR>"/"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(" / "); return symbol(sym.DIVIDE); }
<BR>"("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(" ( "); return symbol(sym.LPAREN); }
<BR>")"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(" ) "); return symbol(sym.RPAREN); }
<BR>&nbsp;
<P>/* If an integer is found print it out, return the token NUMBER that
represents
<BR>an integer and the value of the integer that is held in the string
yytext
<BR>which will get turned into an integer before returning */
<BR>{dec_int_lit}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(yytext());
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return symbol(sym.NUMBER, new Integer(yytext())); }
<P>/* If an identifier is found print it out , return the token ID that
<BR>represents an identifier and the default value one that is given to
all
<BR>identifiers. */
<BR>{dec_int_id}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ System.out.print(yytext());
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return symbol(sym.ID, new Integer(1));}
<P>/* Don't do anything if whitespace is found */
<BR>{WhiteSpace}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ /* just skip what was found, do nothing */ }
<P>}
<P>/* No token was found for the input so through an error.&nbsp; Print
out an
<BR>'Illegal character' message with the illegal character that was found.*/
<BR>.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
{ throw new Error("Illegal character &lt;"+yytext()+">");}
<P>/*&nbsp; <A HREF="lopes.html#rule">Return to Using JFlex</A>&nbsp; */
</BODY>
</HTML>