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
|
/* This is test.g which tests multiple scanners/parsers; DLG-based scanner */
<<
#include "Lexer.h"
typedef ANTLRCommonToken ANTLRToken;
int main()
{
ANTLRTokenPtr aToken = new ANTLRToken;
DLGFileInput in(stdin);
Lexer scan(&in);
scan.setToken(mytoken(aToken));
ANTLRTokenBuffer pipe(&scan);
Include parser(&pipe);
parser.init();
parser.input();
return 0;
}
>>
#token "[\ \t\n]+" <<skip();>>
#token Eof "@"
class Include {
<<
/* this is automatically defined to be a member function of Include::
* since it is within the "class {...}" boundaries.
*/
private:
char *stripquotes(ANTLRChar *s)
{
s[strlen(s)-1] = '\0';
return &s[1];
}
>>
input
: ( cmd | include )* Eof
;
cmd : "print"
( NUMBER <<printf("%s\n", $1->getText());>>
| STRING <<printf("%s\n", $1->getText());>>
)
;
include
: "#include" STRING
<<{
FILE *f;
f = fopen(stripquotes($2->getText()), "r");
if ( f==NULL ) {fprintf(stderr, "can't open %s\n", $2->getText()+1);}
else {
ANTLRTokenPtr aToken = new ANTLRToken;
DLGFileInput in(f);
Lexer scan(&in);
scan.setToken(mytoken(aToken));
ANTLRTokenBuffer pipe(&scan);
Include parser(&pipe);
parser.init();
parser.input();
}
}>>
;
}
#token STRING "\" [a-zA-Z0-9_.,\ \t]+ \""
#token NUMBER "[0-9]+"
|