Script started on Mon Mar 29 09:07:07 1999 vestavia% cat PL0.jlex %% %{ private void echo () { System . out . print (yytext ()); } public int position () { return yychar; } %} %function nextToken %type Token %char %eofval{ { return new Token (Token . EOF); } %eofval} digit = [0-9] letter = [A-Za-z] %% [\ \t\n\r] { echo (); } ";" { echo (); return new Token (Token.SEMICOLON); } "." { echo (); return new Token (Token.PERIOD); } "," { echo (); return new Token (Token.COMMA); } "<" { echo (); return new Token (Token.LT); } "<=" { echo (); return new Token (Token.LE); } ">" { echo (); return new Token (Token.GT); } ">=" { echo (); return new Token (Token.GE); } "=" { echo (); return new Token (Token.EQ); } "<>" { echo (); return new Token (Token.NE); } "(" { echo (); return new Token (Token.LPAREN); } ")" { echo (); return new Token (Token.RPAREN); } "+" { echo (); return new Token (Token.PLUS); } "-" { echo (); return new Token (Token.MINUS); } "*" { echo (); return new Token (Token.TIMES); } "/" { echo (); return new Token (Token.SLASH); } ":=" { echo (); return new Token (Token.ASSIGN); } begin { echo (); return new Token (Token.BEGIN); } call { echo (); return new Token (Token.CALL); } const { echo (); return new Token (Token.CONST); } do { echo (); return new Token (Token.DO); } end { echo (); return new Token (Token.END); } if { echo (); return new Token (Token.IF); } odd { echo (); return new Token (Token.ODD); } procedure { echo (); return new Token (Token.PROC); } then { echo (); return new Token (Token.THEN); } var { echo (); return new Token (Token.VAR); } while { echo (); return new Token (Token.WHILE); } {digit}+ { echo (); return new Token (Token.INTEGER, yytext ()); } {letter}({letter}|{digit})* { echo (); return new Token (Token.ID, yytext ()); } . { echo (); ErrorMessage.print (yychar, "Illegal character"); } vestavia% java JLex.Main PL0.jlex Processing first section -- user code. Processing second section -- JLex declarations. Processing third section -- lexical rules. Creating NFA machine representation. NFA comprised of 141 states. Creating DFA transition table. Working on DFA states....................................................................... Minimizing DFA transition table. 66 states after removal of redundant states. Outputting lexical analyzer code. 46.0u 1.0s 0:51 91% 0+0k 0+0io 0pf+0w vestavia% cat Token.java // Token class definition // Token is a class to represent lexical tokens // in the PL/0 programming language, described // in Algorithms + Data Structures = Programs by // Niklaus Wirth, Prentice-Hall, 1976. public class Token { // token classes public static final int EOF = -1; public static final int BEGIN = 0; public static final int CALL = 1; public static final int CONST = 2; public static final int DO = 3; public static final int END = 4; public static final int IF = 5; public static final int ODD = 6; public static final int PROC = 7; public static final int THEN = 8; public static final int VAR = 9; public static final int WHILE = 10; public static final int ID = 11; public static final int INTEGER = 12; public static final int ASSIGN = 13; public static final int PLUS = '+'; public static final int MINUS = '-'; public static final int TIMES = '*'; public static final int SLASH = '/'; public static final int EQ = '='; public static final int LT = '<'; public static final int GT = '>'; public static final int NE = GT + 1; public static final int LE = NE + 1; public static final int GE = LE + 1; public static final int LPAREN = '('; public static final int RPAREN = ')'; public static final int COMMA = ','; public static final int PERIOD = '.'; public static final int SEMICOLON = ';'; private int symbol; // current token private String lexeme; // lexeme public Token () { } public Token (int symbol) { this (symbol, null); } public Token (int symbol, String lexeme) { this . symbol = symbol; this . lexeme = lexeme; } public int symbol () { return symbol; } public String lexeme () { return lexeme; } public String toString () { switch (symbol) { case BEGIN : return "(keyword, begin) "; case CALL : return "(keyword, call) "; case CONST : return "(keyword, const) "; case DO : return "(keyword, do) "; case END : return "(keyword, end) "; case IF : return "(keyword, if) "; case ODD : return "(keyword, odd) "; case PROC : return "(keyword, proc) "; case THEN : return "(keyword, then) "; case VAR : return "(keyword, var) "; case WHILE : return "(keyword, while) "; case ASSIGN : return "(operator, :=) "; case PLUS : return "(operator, +) "; case MINUS : return "(operator, -) "; case TIMES : return "(operator, *) "; case SLASH : return "(operator, /) "; case EQ : return "(operator, =) "; case LT : return "(operator, <) "; case GT : return "(operator, >) "; case NE : return "(operator, <>) "; case LE : return "(operator, <=) "; case GE : return "(operator, >=) "; case LPAREN : return "(operator, () "; case RPAREN : return "(operator, )) "; case COMMA : return "(punctuation, ,) "; case PERIOD : return "(punctuation, .) "; case SEMICOLON : return "(punctuation, ;) "; case ID : return "(identifier, " + lexeme + ") "; case INTEGER : return "(integer, " + lexeme + ") "; default : ErrorMessage . print (0, "Unrecognized token"); return null; } } } vestavia% cat ErrorMessage.java // ErrorMessage class // This class prints error messages. public class ErrorMessage { public static void print (String message) { System . out . println ("***** Error: " + message + " *****"); System . exit (0); } public static void print (int position, String message) { System . out . println (""); for (int i = 0; i < position; i++) System . out . print (" "); System . out . println ("^"); print (message); } } vestavia% cat PL0Lex.java // PL0Lex class // This class is a PL/0 lexical analyzer which // reads a PL/0 source program and outputs the // list of tokens comprising that program. public class PL0Lex { private static final int MAX_TOKENS = 100; public static void main (String [] args) throws java.io.IOException { int i, n; Token [] token = new Token [MAX_TOKENS]; Yylex lexer = new Yylex (System . in); System . out . println ("Source Program"); System . out . println ("--------------"); System . out . println (""); n = -1; do { if (n < MAX_TOKENS) token [++n] = lexer . nextToken (); else ErrorMessage . print (0, "Maximum number of tokens exceeded"); } while (token[n].symbol() != Token.EOF); System . out . println (""); System . out . println ("List of Tokens"); System . out . println ("--------------"); System . out . println (""); for (i = 0; i < n; i++) System . out . println (token [i]); System . out . println (""); } } vestavia% java PL0Lex < quotrem.pl0 Source Program -------------- var q, r, x, y; begin x := 32; y := 5; q := 0; r := x; while r >= y do begin q := q + 1; r := r - y end end. List of Tokens -------------- (keyword, var) (identifier, q) (punctuation, ,) (identifier, r) (punctuation, ,) (identifier, x) (punctuation, ,) (identifier, y) (punctuation, ;) (keyword, begin) (identifier, x) (operator, :=) (number, 32) (punctuation, ;) (identifier, y) (operator, :=) (number, 5) (punctuation, ;) (identifier, q) (operator, :=) (number, 0) (punctuation, ;) (identifier, r) (operator, :=) (identifier, x) (punctuation, ;) (keyword, while) (identifier, r) (operator, >=) (identifier, y) (keyword, do) (keyword, begin) (identifier, q) (operator, :=) (identifier, q) (operator, +) (integer, 1) (punctuation, ;) (identifier, r) (operator, :=) (identifier, r) (operator, -) (identifier, y) (keyword, end) (keyword, end) (punctuation, .) vestavia% exit script done on Mon Mar 29 09:39:26 1999