Subversion Repositories programming

Rev

Rev 259 | Go to most recent revision | 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;
244 ira 44
static String input_str = "Input: ";
230 ira 45
 
46
public void yyerror (String error)
47
{
244 ira 48
    //System.out.println ("Parse Error: " + error);
230 ira 49
}
50
 
51
int yylex ()
52
{
53
    int lex_return = -1;
54
 
55
    try
56
    {
57
        lex_return = lexer.yylex();
58
    }
59
    catch (IOException e)
60
    {
61
        System.out.println ("IO Error: " + e);
62
    }
63
 
64
    return lex_return;
65
}
66
 
67
public Parser (Reader r)
68
{
69
    lexer = new Yylex (r, this);
70
}
71
 
72
public static void main (String[] args) throws Exception
73
{
243 ira 74
    Parser yyparser = null;
75
 
76
    if (args.length > 0)
77
    {
78
        // read via file argument
79
        yyparser = new Parser (new FileReader (args[0]));
80
    }
81
    else
82
    {
83
        // interactive mode
84
        yyparser = new Parser (new InputStreamReader (System.in));
85
        System.out.println("[Quit with CTRL-D]");
244 ira 86
        System.out.print(input_str);
243 ira 87
        interactive = true;
88
    }
89
 
230 ira 90
    try
91
    {
92
        yyparser.yyparse();
93
    }
94
    catch (Exception e)
95
    {
96
        System.out.println ("Tried to take too many apples out of the bag!");
97
    }
243 ira 98
 
99
    if (interactive)
100
    {
101
        System.out.println();
102
        System.out.println("Have a nice day");
103
    }
230 ira 104
}
105
 
106
public class Robot
107
{
108
    int val = 0;
109
 
110
    void incVal () { val++; }
111
    void decVal () { val--; }
112
    int  getVal () { return val; }
243 ira 113
    void reset  () { val = 0; }
230 ira 114
}
115
 
116
Robot r = new Robot();
117