////////////////////////////////////////////////////////////////////// // The contents of this file are subject to the Mozilla Public License // Version 1.0 (the "License"); you may not use this file except in // compliance with the License. You may obtain a copy of the License // at http://www.mozilla.org/MPL/ // // Software distributed under the License is distributed on an "AS IS" // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. // See the License for the specific language governing rights and // limitations under the License. // // The Original Code is "Mini". // // The Initial Developer of the Original Code is David Wallace Croft // (croft@alumni.caltech.edu, http://www.alumni.caltech.edu/~croft/). // Portions created by David Wallace Croft are // Copyright (C) 1999 David Wallace Croft. All Rights Reserved. // // Contributor(s): ////////////////////////////////////////////////////////////////////// package com.orbs.open.a.mpl.compiler.mini.parse; import java.io.*; import java.text.*; import java.util.*; import java_cup.runtime.*; /********************************************************************* * Token scanner for the Mini programming language. * * Reference: * *
*
* "JLex: A Lexical Analyzer Generator for Java"
*
* http://www.cs.princeton.edu/~appel/modern/java/JLex/
*
* @author
* David W. Croft
* @version
* 1999-04-24
*********************************************************************/
//////////////////////////////////////////////////////////////////////
// Portions of this code machine-generated by JLex.
//////////////////////////////////////////////////////////////////////
%%
%public
%class MiniScanner
%implements MiniSymbols, CUPTokenScanner
%function nextToken
%type Symbol
%line
%char
%cup
%eofval{
return new Symbol ( EOF );
%eofval}
WHITE_SPACE_CHAR=[\ \r\n\t\b\012]
BEGIN="begin"
CALL="call"
DO="do"
ELSE="else"
END="end"
IF="if"
INTEGER="integer"
FI="fi"
PROCEDURE="procedure"
READ="read"
THEN="then"
TO="to"
WHILE="while"
WRITE="write"
LETTER=[a-z]
DIGIT=[0-9]
CONSTANT={DIGIT}+
NAME={LETTER}({LETTER}|{DIGIT})*
SEMICOLON=";"
LPAREN="("
RPAREN=")"
COMMA=","
EXP="^"
TIMES="*"
DIVIDE="/"
MOD="%"
PLUS="+"
MINUS="-"
GE=">="
LE="<="
GT=">"
LT="<"
EQ="="
NE="<>"
ASSIGN=":="
NONNEWLINE_WHITE_SPACE_CHAR=[\ \t\b\012]
STRING_TEXT=(\\\"|[^\n\"]|\\{WHITE_SPACE_CHAR}+\\)*
COMMENT_TEXT=([^/*\n]|[^*\n]"/"[^*\n]|[^/\n]"*"[^/\n]|"*"[^/\n]|"/"[^*\n])*
%{
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
private static final String [ ] TEST_FILES = {
"../test/Test.mini" };
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
public static void main ( String [ ] args )
throws Exception
//////////////////////////////////////////////////////////////////////
{
for ( int i = 0; i < TEST_FILES.length; i++ )
{
test ( new FileReader ( TEST_FILES [ i ] ) );
}
}
public static void test ( Reader reader )
throws Exception
//////////////////////////////////////////////////////////////////////
{
MiniScanner miniScanner = new MiniScanner ( reader );
Symbol symbol = null;
loop:
while ( ( symbol = miniScanner.nextToken ( ) ).sym != EOF )
{
System.out.println (
"Symbol: " + symbol + " Value: " + symbol.value );
}
}
public static Symbol scanConstant ( String text )
//////////////////////////////////////////////////////////////////////
{
try
{
return new Symbol ( CONSTANT, new Integer ( text ) );
}
catch ( NumberFormatException ex )
{
return null;
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
%}
%%