more typename fixes
[prop.git] / tests / regexp.pcc
blob7520f9895e2c19ba6eede2eb8220db8faab85621
1 //////////////////////////////////////////////////////////////////////////////
2 //  This program tests regular expressions string matching.
3 //////////////////////////////////////////////////////////////////////////////
5 #include <assert.h>
6 #include <iostream.h>
8 lexeme digits    = /[0-9]+/
9      | sign      = /[\+\-]/
10      | integer   = /{sign}?{digits}/
11      | exponent  = /[eE]{integer}/
12      | mantassa  = /({digits}(\.{digits})?|\.{digits})/
13      | real      = /{sign}?{mantassa}{exponent}?/
14      | ident     = /[a-zA-Z_][a-zA-Z_0-9]*/
15      | string    = /"([^"\n]|\\.)*"/ 
16      | character = /'([^'\n]|\\.)*'/
17      ;
19 int get_token(const char * s) 
21    match (s)
22    {  case /if/ /* this is a comment, not a regexp */: 
23                               cout << "[if]" << flush;       return 1;
24       case /then/:            cout << "[then]" << flush;     return 2;
25       case /else/:            cout << "[else]" << flush;     return 3;
26       case /while/:           cout << "[while]" << flush;    return 4;
27       case /break/:           cout << "[break]" << flush;    return 5;
28       case /exit|bye|quit/:   cout << "[exit]" << flush;     return 6;
29       case "continue":        cout << "[continue]" << flush; return 7;
30       case /{ident}/:         cout << "[id]" << flush; return 8;
31       case /{string}/:        cout << "[string]" << flush;   return 9;
32       case /{character}/:     cout << "[char]" << flush;     return 10;
33       case /{integer}/:       cout << "[integer]" << flush;  return 11;
34       case /{real}/:          cout << "[real]" << flush;     return 12;
35       case _:                 cout << s << flush;            return 0;
36    }
39 int main() 
40 {  const char * tokens[] =
41       { "if", "then", "else", "for", "do", "while", "continue", "break",
42         "if0", "the", "exit", "exity", "quit", "0elSe", "\"foo\"",
43         "'bar\\''", "+1.98e-20", "+31415926", ".3141596e+1",
44         "(31415926"
45       };
46    int expected[] =
47       {  1,    2,     3,      8,     8,    4,       7,          5,
48          8,    8,     6,      8,        6,      0,      9,    
49          10,   12,    11,    12,    0
50       };
52    for (int i = 0; i < sizeof(tokens)/sizeof(tokens[0]); i++) {
53       cout << tokens[i] << " = "; 
54       int tok = get_token (tokens[i]);
55       cout << '\n';
56       assert (tok == expected[i]);
57    }
58    cout << "Regular expression string matching is ok.\n";
59    return 0;