Compilerbau: Kombination von JLex und CUP |
In diesem Beispiel wird aus einer JLex-Spezifikation für einen Scanner und einer CUP-Spezifikation für einen Parser ein lauffähiges Java-Programm erzeugt. |
import java_cup.runtime.Symbol;
%%
%cup
%class scanner
%implements java_cup.runtime.Scanner
%function next_token
%type Symbol
%%
";" { return
new Symbol(sym.SEMI); }
"+" { return
new Symbol(sym.PLUS); }
"*" { return
new Symbol(sym.TIMES); }
"(" { return
new Symbol(sym.LPAREN); }
")" { return
new Symbol(sym.RPAREN); }
[0-9]+ { return
new Symbol(sym.NUMBER,
new Integer(yytext())); }
[ \t\r\n\f] { /* ignore white space. */ }
. { System.err.println("Illegal character: "
+ yytext()); }
|
import java_cup.runtime.*;
parser code {:
public
static
void
main(String args[])
throws Exception {
new parser(new scanner(System.in)).parse();
}
:}
terminal SEMI, PLUS, TIMES, LPAREN, RPAREN;
terminal Integer NUMBER;
non terminal expr_list, expr_part;
non terminal Integer expr;
precedence left PLUS;
precedence left TIMES;
expr_list ::= expr_list expr_part
| expr_part
;
expr_part ::= expr:e
{: System.out.println(" = "+e+";"); :}
SEMI
;
expr ::= NUMBER:n
{: RESULT=n; :}
| expr:l PLUS expr:r
{: RESULT=new Integer( l.intValue()
+ r.intValue()); :}
| expr:l TIMES expr:r
{: RESULT=new Integer( l.intValue()
* r.intValue()); :}
| LPAREN expr:e RPAREN
{: RESULT=e; :}
;
|
//----------------------------------------------------
// The following code was generated by CUP v0.10k
// Wed Nov 19 14:47:36 CET 2003
//----------------------------------------------------
/** CUP generated class containing symbol constants. */
public class sym
{
/* terminals */
public static final int RPAREN = 6;
public static final int error = 1;
public static final int PLUS = 3;
public static final int NUMBER = 7;
public static final int SEMI = 2;
public static final int LPAREN = 5;
public static final int TIMES = 4;
public static final int EOF = 0;
}
|
import java_cup.runtime.Symbol;
class scanner implements java_cup.runtime.Scanner
{
private final int YY_BUFFER_SIZE = 512;
private final int YY_F = -1;
private final int YY_NO_STATE = -1;
private final int YY_NOT_ACCEPT = 0;
private final int YY_START = 1;
private final int YY_END = 2;
private final int YY_NO_ANCHOR = 4;
private final char YY_EOF = '\uFFFF';
private java.io.BufferedReader yy_reader;
private int yy_buffer_index;
private int yy_buffer_read;
private int yy_buffer_start;
private int yy_buffer_end;
private char yy_buffer[];
private int yy_lexical_state;
scanner (java.io.Reader reader)
{
this ();
if (null == reader)
{
throw (new Error ("Error: Bad input stream initializer."));
}
yy_reader = new java.io.BufferedReader (reader);
}
scanner (java.io.InputStream instream)
{
this ();
if (null == instream)
{
throw (new Error ("Error: Bad input stream initializer."));
}
yy_reader =
new java.io.BufferedReader (new java.io.InputStreamReader (instream));
}
private scanner ()
{
yy_buffer = new char[YY_BUFFER_SIZE];
yy_buffer_read = 0;
yy_buffer_index = 0;
yy_buffer_start = 0;
yy_buffer_end = 0;
yy_lexical_state = YYINITIAL;
}
private boolean yy_eof_done = false;
private final int YYINITIAL = 0;
private final int yy_state_dtrans[] = {
0
};
private void yybegin (int state)
{
yy_lexical_state = state;
}
private char yy_advance () throws java.io.IOException
{
int next_read;
int i;
int j;
if (yy_buffer_index < yy_buffer_read)
{
return yy_buffer[yy_buffer_index++];
}
if (0 != yy_buffer_start)
{
i = yy_buffer_start;
j = 0;
while (i < yy_buffer_read)
{
yy_buffer[j] = yy_buffer[i];
++i;
++j;
}
yy_buffer_end = yy_buffer_end - yy_buffer_start;
yy_buffer_start = 0;
yy_buffer_read = j;
yy_buffer_index = j;
next_read = yy_reader.read (yy_buffer,
yy_buffer_read,
yy_buffer.length - yy_buffer_read);
if (-1 == next_read)
{
return YY_EOF;
}
yy_buffer_read = yy_buffer_read + next_read;
}
while (yy_buffer_index >= yy_buffer_read)
{
if (yy_buffer_index >= yy_buffer.length)
{
yy_buffer = yy_double (yy_buffer);
}
next_read = yy_reader.read (yy_buffer,
yy_buffer_read,
yy_buffer.length - yy_buffer_read);
if (-1 == next_read)
{
return YY_EOF;
}
yy_buffer_read = yy_buffer_read + next_read;
}
return yy_buffer[yy_buffer_index++];
}
private void yy_move_start ()
{
++yy_buffer_start;
}
private void yy_pushback ()
{
--yy_buffer_end;
}
private void yy_mark_start ()
{
yy_buffer_start = yy_buffer_index;
}
private void yy_mark_end ()
{
yy_buffer_end = yy_buffer_index;
}
private void yy_to_mark ()
{
yy_buffer_index = yy_buffer_end;
}
private java.lang.String yytext ()
{
return (new java.lang.String (yy_buffer,
yy_buffer_start,
yy_buffer_end - yy_buffer_start));
}
private int yylength ()
{
return yy_buffer_end - yy_buffer_start;
}
private char[] yy_double (char buf[])
{
int i;
char newbuf[];
newbuf = new char[2 * buf.length];
for (i = 0; i < buf.length; ++i)
{
newbuf[i] = buf[i];
}
return newbuf;
}
private final int YY_E_INTERNAL = 0;
private final int YY_E_MATCH = 1;
private java.lang.String yy_error_string[] = {
"Error: Internal error.\n",
"Error: Unmatched input.\n"
};
private void yy_error (int code, boolean fatal)
{
java.lang.System.out.print (yy_error_string[code]);
java.lang.System.out.flush ();
if (fatal)
{
throw new Error ("Fatal Error.\n");
}
}
private int[][] unpackFromString (int size1, int size2, String st)
{
int colonIndex = -1;
String lengthString;
int sequenceLength = 0;
int sequenceInteger = 0;
int commaIndex;
String workString;
int res[][] = new int[size1][size2];
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
{
if (sequenceLength == 0)
{
commaIndex = st.indexOf (',');
if (commaIndex == -1)
workString = st;
else
workString = st.substring (0, commaIndex);
st = st.substring (commaIndex + 1);
colonIndex = workString.indexOf (':');
if (colonIndex == -1)
{
res[i][j] = Integer.parseInt (workString);
}
else
{
lengthString = workString.substring (colonIndex + 1);
sequenceLength = Integer.parseInt (lengthString);
workString = workString.substring (0, colonIndex);
sequenceInteger = Integer.parseInt (workString);
res[i][j] = sequenceInteger;
sequenceLength--;
}
}
else
{
res[i][j] = sequenceInteger;
sequenceLength--;
}
}
return res;
}
private int yy_acpt[] = {
YY_NOT_ACCEPT,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR,
YY_NO_ANCHOR
};
private int yy_cmap[] = {
0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0,
2, 3, 4, 5, 0, 0, 0, 0,
6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 0, 7, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
private int yy_rmap[] = {
0, 1, 1, 1, 1, 1, 1, 2,
1
};
private int yy_nxt[][] = unpackFromString (3, 8,
"1,2,3,4,5,6,7,8,-1:14,7,-1");
public Symbol next_token () throws java.io.IOException
{
char yy_lookahead;
int yy_anchor = YY_NO_ANCHOR;
int yy_state = yy_state_dtrans[yy_lexical_state];
int yy_next_state = YY_NO_STATE;
int yy_last_accept_state = YY_NO_STATE;
boolean yy_initial = true;
int yy_this_accept;
yy_mark_start ();
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept)
{
yy_last_accept_state = yy_state;
yy_mark_end ();
}
while (true)
{
yy_lookahead = yy_advance ();
yy_next_state = YY_F;
if (YY_EOF != yy_lookahead)
{
yy_next_state = yy_nxt[yy_rmap[yy_state]][yy_cmap[yy_lookahead]];
}
if (YY_F != yy_next_state)
{
yy_state = yy_next_state;
yy_initial = false;
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept)
{
yy_last_accept_state = yy_state;
yy_mark_end ();
}
}
else
{
if (YY_EOF == yy_lookahead && true == yy_initial)
{
return null;
}
else if (YY_NO_STATE == yy_last_accept_state)
{
throw (new Error ("Lexical Error: Unmatched Input."));
}
else
{
yy_to_mark ();
yy_anchor = yy_acpt[yy_last_accept_state];
if (0 != (YY_END & yy_anchor))
{
yy_pushback ();
}
if (0 != (YY_START & yy_anchor))
{
yy_move_start ();
}
switch (yy_last_accept_state)
{
case 1:
{
System.err.println ("Illegal character: " + yytext ());
}
case -2:
break;
case 2:
{ /* ignore white space. */
}
case -3:
break;
case 3:
{
return new Symbol (sym.LPAREN);
}
case -4:
break;
case 4:
{
return new Symbol (sym.RPAREN);
}
case -5:
break;
case 5:
{
return new Symbol (sym.TIMES);
}
case -6:
break;
case 6:
{
return new Symbol (sym.PLUS);
}
case -7:
break;
case 7:
{
return new Symbol (sym.NUMBER, new Integer (yytext ()));
}
case -8:
break;
case 8:
{
return new Symbol (sym.SEMI);
}
case -9:
break;
default:
yy_error (YY_E_INTERNAL, false);
case -1:
}
yy_initial = true;
yy_state = yy_state_dtrans[yy_lexical_state];
yy_next_state = YY_NO_STATE;
yy_last_accept_state = YY_NO_STATE;
yy_mark_start ();
yy_this_accept = yy_acpt[yy_state];
if (YY_NOT_ACCEPT != yy_this_accept)
{
yy_last_accept_state = yy_state;
}
}
}
}
}
}
|
//----------------------------------------------------
// The following code was generated by CUP v0.10k
// Wed Nov 19 14:47:36 CET 2003
//----------------------------------------------------
import java_cup.runtime.*;
/** CUP v0.10k generated parser.
* @version Wed Nov 19 14:47:36 CET 2003
*/
public class parser extends java_cup.runtime.lr_parser
{
/** Default constructor. */
public parser ()
{
super ();
}
/** Constructor which sets the default scanner. */
public parser (java_cup.runtime.Scanner s)
{
super (s);
}
/** Production table. */
protected static final short _production_table[][] =
unpackFromStrings (new String[]{
"\000\011\000\002\003\004\000\002\002\004\000\002\003"
+
"\003\000\002\006\002\000\002\004\005\000\002\005\003"
+ "\000\002\005\005\000\002\005\005\000\002\005\005"
});
/** Access to production table. */
public short[][] production_table ()
{
return _production_table;
}
/** Parse-action table. */
protected static final short[][] _action_table =
unpackFromStrings (new String[]{
"\000\020\000\006\007\010\011\005\001\002\000\010\002"
+
"\uffff\007\uffff\011\uffff\001\002\000\012\004\ufffc\005\ufffc"
+
"\006\ufffc\010\ufffc\001\002\000\010\002\022\007\010\011"
+
"\005\001\002\000\010\004\ufffe\005\013\006\014\001\002"
+
"\000\006\007\010\011\005\001\002\000\010\005\013\006"
+
"\014\010\012\001\002\000\012\004\ufff9\005\ufff9\006\ufff9"
+
"\010\ufff9\001\002\000\006\007\010\011\005\001\002\000"
+
"\006\007\010\011\005\001\002\000\012\004\ufffa\005\ufffa"
+
"\006\ufffa\010\ufffa\001\002\000\012\004\ufffb\005\ufffb\006"
+
"\014\010\ufffb\001\002\000\004\004\020\001\002\000\010"
+
"\002\ufffd\007\ufffd\011\ufffd\001\002\000\010\002\001\007"
+ "\001\011\001\001\002\000\004\002\000\001\002"
});
/** Access to parse-action table. */
public short[][] action_table ()
{
return _action_table;
}
/** <code>reduce_goto</code> table. */
protected static final short[][] _reduce_table =
unpackFromStrings (new String[]{
"\000\020\000\010\003\005\004\003\005\006\001\001\000"
+
"\002\001\001\000\002\001\001\000\006\004\020\005\006"
+
"\001\001\000\004\006\016\001\001\000\004\005\010\001"
+
"\001\000\002\001\001\000\002\001\001\000\004\005\015"
+
"\001\001\000\004\005\014\001\001\000\002\001\001\000"
+
"\002\001\001\000\002\001\001\000\002\001\001\000\002"
+ "\001\001\000\002\001\001"
});
/** Access to <code>reduce_goto</code> table. */
public short[][] reduce_table ()
{
return _reduce_table;
}
/** Instance of action encapsulation class. */
protected CUP$parser$actions action_obj;
/** Action encapsulation object initializer. */
protected void init_actions ()
{
action_obj = new CUP$parser$actions (this);
}
/** Invoke a user supplied parse action. */
public java_cup.runtime.Symbol do_action (int act_num,
java_cup.runtime.lr_parser parser,
java.util.Stack stack,
int top)
throws java.lang.Exception
{
/* call code in generated class */
return action_obj.CUP$parser$do_action (act_num, parser, stack, top);
}
/** Indicates start state. */
public int start_state ()
{
return 0;
}
/** Indicates start production. */
public int start_production ()
{
return 1;
}
/** <code>EOF</code> Symbol index. */
public int EOF_sym ()
{
return 0;
}
/** <code>error</code> Symbol index. */
public int error_sym ()
{
return 1;
}
public static void main (String args[]) throws Exception
{
new parser (new scanner (System.in)).parse ();
}
}
/** Cup generated class to encapsulate user supplied action code.*/
class CUP$parser$actions
{
private final parser parser;
/** Constructor */
CUP$parser$actions (parser parser)
{
this.parser = parser;
}
/** Method with the actual generated action code. */
public final java_cup.runtime.
Symbol CUP$parser$do_action (int CUP$parser$act_num,
java_cup.runtime.lr_parser CUP$parser$parser,
java.util.Stack CUP$parser$stack,
int CUP$parser$top) throws java.lang.
Exception
{
/* Symbol object for return from actions */
java_cup.runtime.Symbol CUP$parser$result;
/* select the action based on the action number */
switch (CUP$parser$act_num)
{
/*. . . . . . . . . . . . . . . . . . . . */
case 8: // expr ::= LPAREN expr RPAREN
{
Integer RESULT = null;
int eleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).left;
int eright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).right;
Integer e =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).value;
RESULT = e;
CUP$parser$result =
new java_cup.runtime.Symbol (3 /*expr */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
2)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 7: // expr ::= expr TIMES expr
{
Integer RESULT = null;
int lleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).left;
int lright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).right;
Integer l =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).value;
int rleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).left;
int rright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).right;
Integer r =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).value;
RESULT = new Integer (l.intValue () * r.intValue ());
CUP$parser$result =
new java_cup.runtime.Symbol (3 /*expr */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
2)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 6: // expr ::= expr PLUS expr
{
Integer RESULT = null;
int lleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).left;
int lright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).right;
Integer l =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).value;
int rleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).left;
int rright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).right;
Integer r =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).value;
RESULT = new Integer (l.intValue () + r.intValue ());
CUP$parser$result =
new java_cup.runtime.Symbol (3 /*expr */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
2)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 5: // expr ::= NUMBER
{
Integer RESULT = null;
int nleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).left;
int nright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).right;
Integer n =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).value;
RESULT = n;
CUP$parser$result =
new java_cup.runtime.Symbol (3 /*expr */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 4: // expr_part ::= expr NT$0 SEMI
{
Object RESULT = null;
// propagate RESULT from NT$0
if (((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).value != null)
RESULT =
(Object) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).value;
int eleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).left;
int eright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).right;
Integer e =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 2)).value;
CUP$parser$result =
new java_cup.runtime.Symbol (2 /*expr_part */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
2)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 3: // NT$0 ::=
{
Object RESULT = null;
int eleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).left;
int eright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).right;
Integer e =
(Integer) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 0)).value;
System.out.println (" = " + e + ";");
CUP$parser$result =
new java_cup.runtime.Symbol (4 /*NT$0 */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 2: // expr_list ::= expr_part
{
Object RESULT = null;
CUP$parser$result =
new java_cup.runtime.Symbol (1 /*expr_list */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 1: // $START ::= expr_list EOF
{
Object RESULT = null;
int start_valleft =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).left;
int start_valright =
((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).right;
Object start_val =
(Object) ((java_cup.runtime.Symbol) CUP$parser$stack.
elementAt (CUP$parser$top - 1)).value;
RESULT = start_val;
CUP$parser$result =
new java_cup.runtime.Symbol (0 /*$START */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
1)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
/* ACCEPT */
CUP$parser$parser.done_parsing ();
return CUP$parser$result;
/*. . . . . . . . . . . . . . . . . . . . */
case 0: // expr_list ::= expr_list expr_part
{
Object RESULT = null;
CUP$parser$result =
new java_cup.runtime.Symbol (1 /*expr_list */ ,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
1)).left,
((java_cup.runtime.
Symbol) CUP$parser$stack.
elementAt (CUP$parser$top -
0)).right, RESULT);
}
return CUP$parser$result;
/* . . . . . . */
default:
throw new
Exception ("Invalid action number found in internal parse table");
}
}
}
|
JCP := $(PWD)/../../../software:$(PWD)/../../../software/CUP:$(CLASSPATH):.
JAVA := java -classpath $(JCP)
JAVAC := javac -classpath $(JCP)
JLEX := java -classpath $(JCP) JLex.Main
CUP := java -classpath $(JCP) java_cup.Main
lexsrc := $(shell echo *.lex)
cupsrc := $(shell echo *.cup)
javagen := scanner.java parser.java sym.java
javasrc := $(shell echo *.java)
classes := $(javagen:.java=.class)
tests = test1
all : examples
examples : $(javagen) $(classes) $(tests)
scanner.java : minimal.lex sym.java
$(JLEX) $<
mv $<.java $@
indent $@
parser.java sym.java : minimal.cup
$(CUP) minimal.cup
indent parser.java sym.java
test1 : $(classes)
echo "(3 + 4) * (1 + 2 + 3);" | $(JAVA) parser
%.java : %.lex
$(JLEX) $<
%.class : %.java
$(JAVAC) $<
.SUFFIXES : .class .java .lex .cup
clean :
rm -f $(javagen) *.class *~ .*~
|
# parser generation
java -classpath ... java_cup.Main minimal.cup
Opening files...
Parsing specification from standard input...
Checking specification...
Building parse tables...
Computing non-terminal nullability...
Computing first sets...
Building state machine...
Filling in tables...
Checking for non-reduced productions...
Writing parser...
Closing files...
------- CUP v0.10k Parser Generation Summary -------
0 errors and 0 warnings
8 terminals, 5 non-terminals, and 9 productions declared,
producing 16 unique parse states.
0 terminals declared but not used.
0 non-terminals declared but not used.
0 productions never reduced.
0 conflicts detected (0 expected).
Code written to "parser.java", and "sym.java".
---------------------------------------------------- (v0.10k)
# scanner generation
java -classpath ... JLex.Main minimal.lex
Processing first section -- user code.
Processing second section -- JLex declarations.
Processing third section -- lexical rules.
Creating NFA machine representation.
NFA comprised of 27 states.
Creating DFA transition table.
Working on DFA states............
Minimizing DFA transition table.
9 states after removal of redundant states.
Outputting lexical analyzer code.
mv minimal.lex.java scanner.java
javac -classpath ... scanner.java
javac -classpath ... parser.java
echo "(3 + 4) * (1 + 2 + 3);" \
| java -classpath ... parser
= 42;
|
Letzte Änderung: 14.02.2012 | © Prof. Dr. Uwe Schmidt |