~ubuntu-branches/ubuntu/utopic/cdrdao/utopic

« back to all changes in this revision

Viewing changes to pccts/testcpp/11/test.g

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Suffield
  • Date: 2004-06-24 22:33:16 UTC
  • Revision ID: james.westby@ubuntu.com-20040624223316-534onzugaeeyq61j
Tags: upstream-1.1.9
ImportĀ upstreamĀ versionĀ 1.1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This is test.g which tests multiple scanners/parsers; DLG-based scanner;
 
2
 * also, we test multiple lexical classes.
 
3
 */
 
4
<<
 
5
#include "Lexer.h"
 
6
typedef ANTLRCommonToken ANTLRToken;
 
7
#include "PBlackBox.h"
 
8
 
 
9
int main()
 
10
{
 
11
        ParserBlackBox<Lexer, Include, ANTLRToken> p(stdin);
 
12
        p.parser()->input();
 
13
        return 0;
 
14
}
 
15
>>
 
16
 
 
17
#token "[\ \t\n]+"      <<skip();>>
 
18
 
 
19
#lexclass START
 
20
 
 
21
class Include {
 
22
 
 
23
<<
 
24
/* this is automatically defined to be a member function of Include::
 
25
 * since it is within the "class {...}" boundaries.
 
26
 */
 
27
private:
 
28
char *stripquotes(ANTLRChar *s)
 
29
{
 
30
        s[strlen(s)-1] = '\0';
 
31
        return &s[1];
 
32
}
 
33
>>
 
34
 
 
35
input
 
36
        :       ( cmd | include )* "@"
 
37
        ;
 
38
 
 
39
cmd     :       "print"
 
40
                (       NUMBER          <<printf("%s\n", $1->getText());>>
 
41
                |       STRING          <<printf("%s\n", $1->getText());>>
 
42
                )
 
43
        ;
 
44
 
 
45
include
 
46
        :       "#data" STRING
 
47
                <<{
 
48
                FILE *f;
 
49
                f = fopen(stripquotes($2->getText()), "r");
 
50
                if ( f==NULL ) {fprintf(stderr, "can't open %s\n", $2->getText()+1);}
 
51
                else {
 
52
                        ANTLRTokenPtr aToken = new ANTLRToken;
 
53
                        DLGFileInput in(f);
 
54
                        Lexer scan(&in);
 
55
                        scan.setToken(mytoken(aToken));
 
56
                        scan.mode(Lexer::DATA);
 
57
                        ANTLRTokenBuffer pipe(&scan);
 
58
                        Include parser(&pipe);
 
59
                        parser.init();
 
60
                        parser.data();
 
61
                }
 
62
                }>>
 
63
        ;
 
64
 
 
65
#lexclass DATA
 
66
 
 
67
#token "[\ \t\n]+"      <<skip();>>
 
68
 
 
69
data:   "0x[0-9]+" ":" "0x[0-9]+"
 
70
                <<printf("data %s\n", $1->getText());>>
 
71
        ;
 
72
 
 
73
}
 
74
 
 
75
#lexclass START
 
76
 
 
77
#token STRING   "\" [a-zA-Z0-9_.,\ \t]+ \""
 
78
#token NUMBER   "[0-9]+"