File: bug.y

package info (click to toggle)
byacc-j 1.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 464 kB
  • sloc: ansic: 6,768; yacc: 893; java: 447; makefile: 86; sh: 28
file content (153 lines) | stat: -rwxr-xr-x 3,060 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

%{
import java.io.*;
import java.util.StringTokenizer;
%}


%token          ITEM
%token <sval>   NAME
%token          REPLACE
%token          OVERRIDE
%token          TERMINATOR
%token          DONE

%type   <sval>   identifier 
%type   <ival>   mode 


%start defn
%%

defn             : ITEM identifier mode DONE '\n' 
                    {
                      System.out.println( "Item definition" );
                      System.out.println( "   Name = " + $2 );
                      System.out.println( "   mode = " + $3 );
                    }

identifier      : NAME  { $$ = $1; }

mode            : /* empty */    { $$ = OVERRIDE; }
                | REPLACE        { $$ = REPLACE; }
                | OVERRIDE       { $$ = OVERRIDE; }
                ;

%%

// Code cut from the byacc demo page and modified to take 
// first arg as input file name

 String ins;
 StringTokenizer st;

 void yyerror(String s)
 {
   System.out.println("par:"+s);
 }


 boolean newline;

 int yylex()
 {
 String s;
 int tok;
 String tokstr;
 Double d;
   //System.out.print("yylex ");
   if (!st.hasMoreTokens())
     if (!newline)
       {
       newline=true;
       System.out.println("tok: \\n");
       return '\n';  //So we look like classic YACC example
       }
     else
       return 0;

   s = st.nextToken();

   try {
     if ( s.equalsIgnoreCase("ITEM") ) {
       tok = ITEM;
       System.out.println("tok: ITEM");
     } else if ( s.equalsIgnoreCase("REPLACE") ) {
       tok = REPLACE;
       System.out.println("tok: REPLACE");
     } else if ( s.equalsIgnoreCase("DONE") ) {
       tok = DONE;
       System.out.println("tok: DONE");
     } else if ( s.equalsIgnoreCase("OVERRIDE")) {
       tok = OVERRIDE;
       System.out.println("tok: OVERRIDE");
     } else {
       yylval = new parserval( s );
       tok = NAME;
       System.out.println("tok: NAME -> " + s);
     }
   } catch ( Throwable e ) {
     return 0;
   }


   return tok;
 }



 void dotest( String filepath ) throws java.io.FileNotFoundException

 {

   
   BufferedReader in = new BufferedReader(new FileReader(filepath));

   // AS per the calculator demo
   System.out.println("Note: you will need to separate the items");

   System.out.println("with spaces, i.e.:  'ITEM hello override");

   while (true)

     {
       
       System.out.print("example item:");
       
       try       
         {
           ins = in.readLine();
         }

     catch (Exception e)
       {
       }

     st = new StringTokenizer(ins);
     
     newline=false;

     yyparse();

     }

 }



 public static void main(String args[]) throws
java.io.FileNotFoundException

 {

   parser par = new parser();
   par.yydebug = true;
   System.out.println( "parse input file: " + args[0] ); 
   par.dotest(args[0]);

 }

// Invoke this code with 
// java parser test.input
// where test.input is the file containing the input text