Subversion Repositories programming

Rev

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