Subversion Repositories programming

Rev

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