161 lines
7.2 KiB
HTML
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>
|
|
and then after the parser is created
|
|
<BR>
|
|
> javac Lexer.java
|
|
<BR>*/
|
|
<P>/* --------------------------<B>Usercode Section</B>------------------------
|
|
*/
|
|
<BR>
|
|
<P>/* import these classes:
|
|
<BR> java_cup.runtime.*
|
|
<BR> sym
|
|
--class we create with CUP
|
|
<BR>*/
|
|
<BR><B>import java_cup.runtime.*;</B>
|
|
<BR><B>import sym;</B>
|
|
<BR>
|
|
<BR>
|
|
<P><B>%%</B>
|
|
<P>/* -----------------<B>Options and Declarations Section</B>-----------------
|
|
*/
|
|
<BR>
|
|
<P>/* The name of the class JFlex will create will be Lexer.
|
|
<BR> 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>
|
|
<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. Here
|
|
you declare
|
|
<BR>member variables and functions that are used inside scanner actions.
|
|
<BR>*/
|
|
<BR><B>%{</B>
|
|
<P> /* To create a new java_cup.runtime.Symbol with information about
|
|
the current
|
|
<BR> token, the token will have no value in this
|
|
case. */
|
|
<BR> private Symbol symbol(int type) {
|
|
<BR> return new Symbol(type, yyline, yycolumn);
|
|
<BR> }
|
|
<P> /* Also creates a new java_cup.runtime.Symbol with information
|
|
about the
|
|
<BR> current token, but this object has a value.
|
|
*/
|
|
<BR> private Symbol symbol(int type, Object value) {
|
|
<BR> return new Symbol(type, yyline, yycolumn, value);
|
|
<BR> }
|
|
<P><B>%}</B>
|
|
<P>/* <A HREF="lopes.html#decl">Return to Using JFlex</A> */
|
|
<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> = {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>/* </B> <A HREF="lopes.html#decl">Return to Using JFlex</A>
|
|
*/
|
|
<BR>
|
|
<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.
|
|
So these regular
|
|
<BR>expressions will only be matched if the scanner is in the start state
|
|
<BR>YYINITIAL. */
|
|
<P><B><YYINITIAL></B> {
|
|
<P>/* Return the token SEMI declared in the class sym that was found. */
|
|
<BR>";"
|
|
{ return symbol(sym.SEMI); }
|
|
<P>/* Print the token found that was declared in the class sym and then
|
|
return it.*/
|
|
<BR>"+"
|
|
{ System.out.print(" + "); return symbol(sym.PLUS); }
|
|
<BR>"-"
|
|
{ System.out.print(" - "); return symbol(sym.MINUS); }
|
|
<BR>"*"
|
|
{ System.out.print(" * "); return symbol(sym.TIMES); }
|
|
<BR>"/"
|
|
{ System.out.print(" / "); return symbol(sym.DIVIDE); }
|
|
<BR>"("
|
|
{ System.out.print(" ( "); return symbol(sym.LPAREN); }
|
|
<BR>")"
|
|
{ System.out.print(" ) "); return symbol(sym.RPAREN); }
|
|
<BR>
|
|
<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}
|
|
{ System.out.print(yytext());
|
|
<BR>
|
|
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}
|
|
{ System.out.print(yytext());
|
|
<BR>
|
|
return symbol(sym.ID, new Integer(1));}
|
|
<P>/* Don't do anything if whitespace is found */
|
|
<BR>{WhiteSpace}
|
|
{ /* just skip what was found, do nothing */ }
|
|
<P>}
|
|
<P>/* No token was found for the input so through an error. Print
|
|
out an
|
|
<BR>'Illegal character' message with the illegal character that was found.*/
|
|
<BR>.
|
|
{ throw new Error("Illegal character <"+yytext()+">");}
|
|
<P>/* <A HREF="lopes.html#rule">Return to Using JFlex</A> */
|
|
</BODY>
|
|
</HTML>
|