Subversion Repositories programming

Rev

Rev 261 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 261 Rev 281
Line 39... Line 39...
39
 
39
 
40
%%
40
%%
41
 
41
 
42
private Yylex lexer;
42
private Yylex lexer;
43
static boolean interactive = false;
43
static boolean interactive = false;
44
static String input_str = "Input: ";
44
private static String input_str = "Input: ";
-
 
45
private Robot r = new Robot();
45
 
46
 
46
public void yyerror (String error)
47
public void yyerror (String error)
47
{
48
{
-
 
49
    /* No need to print out anything here, since we'll
-
 
50
     * catch all errors later anyway. */
48
    //System.out.println ("Parse Error: " + error);
51
    //System.out.println ("Parse Error: " + error);
49
}
52
}
50
 
53
 
51
int yylex ()
54
int yylex ()
52
{
55
{
Line 85... Line 88...
85
        System.out.println("[Quit with CTRL-D]");
88
        System.out.println("[Quit with CTRL-D]");
86
        System.out.print(input_str);
89
        System.out.print(input_str);
87
        interactive = true;
90
        interactive = true;
88
    }
91
    }
89
 
92
 
-
 
93
    /* If we have had an error parsing, we probably tried to take too many
-
 
94
     * apples out of the bag, so print out a nice error message. */
90
    try
95
    try
91
    {
96
    {
92
        yyparser.yyparse();
97
        yyparser.yyparse();
93
    }
98
    }
94
    catch (Exception e)
99
    catch (Exception e)
95
    {
100
    {
96
        System.out.println ("Tried to take too many apples out of the bag!");
101
        System.out.println ("Tried to take too many apples out of the bag!");
97
    }
102
    }
98
 
103
 
-
 
104
    /* Print ending message if we are in interactive mode */
99
    if (interactive)
105
    if (interactive)
100
    {
106
    {
101
        System.out.println();
107
        System.out.println();
102
        System.out.println("Have a nice day");
108
        System.out.println("Have a nice day");
103
    }
109
    }
104
}
110
}
105
 
111
 
-
 
112
/**
-
 
113
 * Private class, which will be used to keep track of how many apples
-
 
114
 * are in the bag. It could possibly be extended later to allow for a
-
 
115
 * graphical reprensentation of the robot.
-
 
116
 */
106
public class Robot
117
public class Robot
107
{
118
{
108
    int val = 0;
119
    private int val = 0;
109
 
120
 
110
    void incVal () { val++; }
121
    void incVal () { val++; }
111
    void decVal () { val--; }
122
    void decVal () { val--; }
112
    int  getVal () { return val; }
123
    int  getVal () { return val; }
113
    void reset  () { val = 0; }
124
    void reset  () { val = 0; }
114
}
125
}
115
 
126
 
116
Robot r = new Robot();
-
 
117
 
-