Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GPDL/CocoR/Parser.frame @ 15318

Last change on this file since 15318 was 9724, checked in by gkronber, 11 years ago

#2026 changed ATG to Coco/R syntax and use Coco/R (C#) to generate scanner and parser for GPDL

File size: 3.9 KB
RevLine 
[9724]1/*----------------------------------------------------------------------
2Compiler Generator Coco/R,
3Copyright (c) 1990, 2004 Hanspeter Moessenboeck, University of Linz
4extended by M. Loeberbauer & A. Woess, Univ. of Linz
5with improvements by Pat Terry, Rhodes University
6
7This program is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 2, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License along
18with this program; if not, write to the Free Software Foundation, Inc.,
1959 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21As an exception, it is allowed to write an extension of Coco/R that is
22used as a plugin in non-free software.
23
24If not otherwise stated, any source code generated by Coco/R (other than
25Coco/R itself) does not fall under the GNU General Public License.
26----------------------------------------------------------------------*/
27-->begin
28using System;
29
30-->namespace
31
32public class Parser {
33-->constants
34  const bool T = true;
35  const bool x = false;
36  const int minErrDist = 2;
37 
38  public Scanner scanner;
39  public Errors  errors;
40
41  public Token t;    // last recognized token
42  public Token la;   // lookahead token
43  int errDist = minErrDist;
44
45-->declarations
46
47  public Parser(Scanner scanner) {
48    this.scanner = scanner;
49    errors = new Errors();
50  }
51
52  void SynErr (int n) {
53    if (errDist >= minErrDist) errors.SynErr(la.line, la.col, n);
54    errDist = 0;
55  }
56
57  public void SemErr (string msg) {
58    if (errDist >= minErrDist) errors.SemErr(t.line, t.col, msg);
59    errDist = 0;
60  }
61 
62  void Get () {
63    for (;;) {
64      t = la;
65      la = scanner.Scan();
66      if (la.kind <= maxT) { ++errDist; break; }
67-->pragmas
68      la = t;
69    }
70  }
71 
72  void Expect (int n) {
73    if (la.kind==n) Get(); else { SynErr(n); }
74  }
75 
76  bool StartOf (int s) {
77    return set[s, la.kind];
78  }
79 
80  void ExpectWeak (int n, int follow) {
81    if (la.kind == n) Get();
82    else {
83      SynErr(n);
84      while (!StartOf(follow)) Get();
85    }
86  }
87
88
89  bool WeakSeparator(int n, int syFol, int repFol) {
90    int kind = la.kind;
91    if (kind == n) {Get(); return true;}
92    else if (StartOf(repFol)) {return false;}
93    else {
94      SynErr(n);
95      while (!(set[syFol, kind] || set[repFol, kind] || set[0, kind])) {
96        Get();
97        kind = la.kind;
98      }
99      return StartOf(syFol);
100    }
101  }
102
103 
104-->productions
105
106  public void Parse() {
107    la = new Token();
108    la.val = "";   
109    Get();
110-->parseRoot
111  }
112 
113  static readonly bool[,] set = {
114-->initialization
115  };
116} // end Parser
117
118
119public class Errors {
120  public int count = 0;                                    // number of errors detected
121  public System.IO.TextWriter errorStream = Console.Out;   // error messages go to this stream
122  public string errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
123
124  public virtual void SynErr (int line, int col, int n) {
125    string s;
126    switch (n) {
127-->errors
128      default: s = "error " + n; break;
129    }
130    errorStream.WriteLine(errMsgFormat, line, col, s);
131    count++;
132  }
133
134  public virtual void SemErr (int line, int col, string s) {
135    errorStream.WriteLine(errMsgFormat, line, col, s);
136    count++;
137  }
138 
139  public virtual void SemErr (string s) {
140    errorStream.WriteLine(s);
141    count++;
142  }
143 
144  public virtual void Warning (int line, int col, string s) {
145    errorStream.WriteLine(errMsgFormat, line, col, s);
146  }
147 
148  public virtual void Warning(string s) {
149    errorStream.WriteLine(s);
150  }
151} // Errors
152
153
154public class FatalError: Exception {
155  public FatalError(string m): base(m) {}
156}
Note: See TracBrowser for help on using the repository browser.