public class Symbol {
public static final int NL = 0, EOF = 1, IDENT = 2, ICONST = 3,
LPAR = 4, RPAR = 5, LT = 6, GT = 7, LBRA = 8, RBRA = 9,
ASSIGN = 10, SEMI = 11, READ = 12, PRINT = 13, IF = 14,
Program = 15, StatList = 16, Stat = 17, Cond = 18, Expr = 19;
public static final int N = 15;
}
%%
%class Lexer
%int
L = [A-Za-z_]
D = [0-9]
Ident = {L}({L}|{D})*
Iconst = [-+]?{D}+
Blank = [ \t\n]+
%%
{Blank} { /* ignore */ }
\( { return Symbol.LPAR; }
\) { return Symbol.RPAR; }
; { return Symbol.SEMI; }
\{ { return Symbol.LBRA; }
\} { return Symbol.RBRA; }
= { return Symbol.ASSIGN; }
\> { return Symbol.GT; }
\< { return Symbol.LT; }
read { return Symbol.READ; }
print { return Symbol.PRINT; }
if { return Symbol.IF; }
{Ident} { return Symbol.IDENT; }
{Iconst} { return Symbol.ICONST; }
import java.util.*;
import java.io.*;
public class Sam61 {
static int[][] rules = {
{ },
{ Symbol.Program, Symbol.StatList }, //1
{ Symbol.StatList, Symbol.Stat, Symbol.StatList }, //2
{ Symbol.StatList }, //3
{ Symbol.Stat, Symbol.IDENT, Symbol.ASSIGN, Symbol.Expr, Symbol.SEMI },//4
{ Symbol.Stat, Symbol.READ, Symbol.IDENT, Symbol.SEMI }, //5
{ Symbol.Stat, Symbol.PRINT, Symbol.Expr, Symbol.SEMI }, //6
{ Symbol.Stat, Symbol.IF, Symbol.LPAR, Symbol.Cond,
Symbol.RPAR, Symbol.Stat }, //7
{ Symbol.Stat, Symbol.LBRA, Symbol.StatList, Symbol.RBRA }, //8
{ Symbol.Cond, Symbol.Expr, Symbol.LT, Symbol.Expr }, //9
{ Symbol.Cond, Symbol.Expr, Symbol.GT, Symbol.Expr }, //10
{ Symbol.Expr, Symbol.IDENT }, //11
{ Symbol.Expr, Symbol.ICONST }, //12
};
static int [][] ptab = {
{},{},{},{}, {},{},{},{}, {},{},{},{}, {},{},{},
{ 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1 },
{ 0, 3, 2, 0, 0, 0, 0, 0, 2, 3, 0, 0, 2, 2, 2 },
{ 0, 0, 4, 0, 0, 0, 0, 0, 8, 0, 0, 0, 5, 6, 7 },
{ 0, 0, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0,11,12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
public static void main(String[] args) throws Exception {
Tokenizer tok = new Tokenizer(args[0]);
Stack<Integer> stk = new Stack<Integer>();
stk.push(Symbol.Program);
while(stk.size() > 0) {
// System.out.printf("%s : %s\n", stk.toString(), tok.curStr());
if(stk.peek() < Symbol.N) {
if(!tok.chkfwd(stk.pop())) {
System.err.printf("token mismatch: %s at %d\n",
tok.curStr(), tok.curLine()); return;
}
} else {
int t = tok.curTok(); if(tok.isEof()) { t = Symbol.EOF; }
int r = ptab[stk.peek()][t];
if(r == 0) {
System.err.printf("cannot determine rule for %d: %s at %d\n",
stk.peek(), tok.curStr(), tok.curLine()); return;
}
System.out.printf("rule: %d\n", r);
int[] a = rules[r];
stk.pop();
for(int i = a.length-1; i > 0; --i) { stk.push(a[i]); }
}
}
}
}
class Tokenizer {
Lexer lex;
int tok, line = 1;
boolean eof = false;
public Tokenizer(String s) throws Exception {
lex = new Lexer(new FileReader(s));
tok = lex.yylex();
}
public boolean isEof() { return tok == Lexer.YYEOF; }
public int curTok() { return tok; }
public String curStr() { return lex.yytext(); }
public int curLine() { return line; }
public boolean chk(int t) { return tok == t; }
public void fwd() {
if(isEof()) { return; }
try {
tok = lex.yylex();
while(tok == Symbol.NL) { ++line; tok = lex.yylex(); }
} catch(IOException ex) { tok = Lexer.YYEOF; }
}
public boolean chkfwd(int t) {
if(chk(t)) { fwd(); return true; } else { return false; }
}
}
read x; read y; if(x > y) { z = x; x = y; y = z; } print x; print y;