Subversion Repositories programming

Rev

Rev 230 | Rev 244 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 230 Rev 243
Line 1... Line 1...
1
/*******************************************************************************
1
/*******************************************************************************
2
 * File: robot.y
2
 * File: robot.y
3
 * 
3
 *
4
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
4
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
5
 * License: GNU General Public License v2
5
 * License: GNU General Public License v2
6
 *
6
 *
7
 * This file implements the grammar that controls our fictional apple-bag
7
 * This file implements the grammar that controls our fictional apple-bag
8
 * robot from Project #3, Question #2.
8
 * robot from Project #3, Question #2.
Line 15... Line 15...
15
%}
15
%}
16
 
16
 
17
/* Token Declarations */
17
/* Token Declarations */
18
%token PUT
18
%token PUT
19
%token TAKE
19
%token TAKE
-
 
20
%token NL
-
 
21
%token RESET
20
 
22
 
21
/* Robot Grammar */
23
/* Robot Grammar */
22
%%
24
%%
23
 
25
 
-
 
26
program : program line
-
 
27
    | ;
-
 
28
 
-
 
29
line: NL        { if (interactive) System.out.print ("Line: "); }
24
program : S         { System.out.println ("There are: " + r.getVal() + " apples in the bag!"); };
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"); };
25
 
33
 
26
S : PUT S TAKE S    { System.out.println ("S -> PUT S TAKE S"); }
34
S : PUT S TAKE S    { System.out.println ("S -> PUT S TAKE S"); }
27
    | PUT S         { System.out.println ("S -> PUT S"); r.incVal(); }
35
    | PUT S         { System.out.println ("S -> PUT S"); r.incVal(); }
28
    |               { System.out.println ("S -> epsilon"); };
36
    |               { System.out.println ("S -> epsilon"); };
29
 
37
 
30
%%
38
%%
31
 
39
 
32
private Yylex lexer;
40
private Yylex lexer;
-
 
41
static boolean interactive = false;
33
 
42
 
34
public void yyerror (String error)
43
public void yyerror (String error)
35
{
44
{
36
    System.out.println ("Parse Error: " + error);
45
    System.out.println ("Parse Error: " + error);
37
}
46
}
Line 57... Line 66...
57
    lexer = new Yylex (r, this);
66
    lexer = new Yylex (r, this);
58
}
67
}
59
 
68
 
60
public static void main (String[] args) throws Exception
69
public static void main (String[] args) throws Exception
61
{
70
{
-
 
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
 
62
    try
92
    try
63
    {
93
    {
64
        Parser yyparser = new Parser (new FileReader (args[0]));
-
 
65
        yyparser.yyparse();
94
        yyparser.yyparse();
66
    }
95
    }
67
    catch (Exception e)
96
    catch (Exception e)
68
    {
97
    {
69
        System.out.println ("Tried to take too many apples out of the bag!");
98
        System.out.println ("Tried to take too many apples out of the bag!");
70
    }
99
    }
-
 
100
 
-
 
101
    if (interactive)
-
 
102
    {
-
 
103
        System.out.println();
-
 
104
        System.out.println("Have a nice day");
-
 
105
    }
71
}
106
}
72
 
107
 
73
public class Robot
108
public class Robot
74
{
109
{
75
    int val = 0;
110
    int val = 0;
76
 
111
 
77
    void incVal () { val++; }
112
    void incVal () { val++; }
78
    void decVal () { val--; }
113
    void decVal () { val--; }
79
    int  getVal () { return val; }
114
    int  getVal () { return val; }
-
 
115
    void reset  () { val = 0; }
80
}
116
}
81
 
117
 
82
Robot r = new Robot();
118
Robot r = new Robot();
83
 
119