Subversion Repositories programming

Rev

Rev 261 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
230 ira 1
/*******************************************************************************
2
 * File: robot.y
243 ira 3
 *
230 ira 4
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
5
 * License: GNU General Public License v2
6
 *
7
 * This file implements the grammar that controls our fictional apple-bag
8
 * robot from Project #3, Question #2.
9
 ******************************************************************************/
10
 
11
%{
12
    import java.lang.*;
13
    import java.io.*;
14
    import java.util.StringTokenizer;
15
%}
16
 
17
/* Token Declarations */
18
%token PUT
19
%token TAKE
243 ira 20
%token NL
230 ira 21
 
22
/* Robot Grammar */
23
%%
24
 
243 ira 25
program : program line
26
    | ;
230 ira 27
 
258 ira 28
line: NL            { if (interactive) System.out.print (input_str); r.reset(); }
261 ira 29
    | S NL          { System.out.println ("There are: " + r.getVal() + " apples in the bag!\n");
258 ira 30
                      if (interactive) System.out.print (input_str); r.reset(); };
243 ira 31
 
259 ira 32
S : P S T S         { }
33
    | P S           { }
34
    |               { };
230 ira 35
 
259 ira 36
P : PUT             { System.out.println ("PUT  an apple in the bag"); r.incVal(); };
258 ira 37
 
259 ira 38
T : TAKE            { System.out.println ("TAKE an apple out of the bag"); r.decVal(); };
258 ira 39
 
230 ira 40
%%
41
 
42
private Yylex lexer;
243 ira 43
static boolean interactive = false;
281 ira 44
private static String input_str = "Input: ";
45
private Robot r = new Robot();
230 ira 46
 
47
public void yyerror (String error)
48
{
281 ira 49
    /* No need to print out anything here, since we'll
50
     * catch all errors later anyway. */
244 ira 51
    //System.out.println ("Parse Error: " + error);
230 ira 52
}
53
 
54
int yylex ()
55
{
56
    int lex_return = -1;
57
 
58
    try
59
    {
60
        lex_return = lexer.yylex();
61
    }
62
    catch (IOException e)
63
    {
64
        System.out.println ("IO Error: " + e);
65
    }
66
 
67
    return lex_return;
68
}
69
 
70
public Parser (Reader r)
71
{
72
    lexer = new Yylex (r, this);
73
}
74
 
75
public static void main (String[] args) throws Exception
76
{
243 ira 77
    Parser yyparser = null;
78
 
79
    if (args.length > 0)
80
    {
81
        // read via file argument
82
        yyparser = new Parser (new FileReader (args[0]));
83
    }
84
    else
85
    {
86
        // interactive mode
87
        yyparser = new Parser (new InputStreamReader (System.in));
88
        System.out.println("[Quit with CTRL-D]");
244 ira 89
        System.out.print(input_str);
243 ira 90
        interactive = true;
91
    }
92
 
281 ira 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. */
230 ira 95
    try
96
    {
97
        yyparser.yyparse();
98
    }
99
    catch (Exception e)
100
    {
101
        System.out.println ("Tried to take too many apples out of the bag!");
102
    }
243 ira 103
 
281 ira 104
    /* Print ending message if we are in interactive mode */
243 ira 105
    if (interactive)
106
    {
107
        System.out.println();
108
        System.out.println("Have a nice day");
109
    }
230 ira 110
}
111
 
281 ira 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
 */
230 ira 117
public class Robot
118
{
281 ira 119
    private int val = 0;
230 ira 120
 
121
    void incVal () { val++; }
122
    void decVal () { val--; }
123
    int  getVal () { return val; }
243 ira 124
    void reset  () { val = 0; }
230 ira 125
}
126