Merge remote branch 'master'
[prop.git] / tests / qa2.cc
blobeab5449eef633947ec4c6f3a43342480ca7476dc
1 // Test some lexer stuff
3 #include <stdio.h>
4 #include <assert.h>
5 #include <AD/automata/acgen.h> // Aho-Corasick automaton
6 #include <AD/automata/lexergen.h> // Lexical scanner
8 int main(int argc, const char * argv[])
10 static const char * grammar[] =
11 { "break", "class", "continue", "do", "for", "goto",
12 "if", "struct", "typedef", "union", "while", "return",
13 "for", "switch", "case", "else", "default", "public",
14 "private", "protected", "virtual",
15 "[0-9]+", "[a-zA-Z_][a-zA-Z0-9_]*",
16 "==", "!=", "\\>", "\\<", "\\>=", "\\<=", "!", "\\(", "\\)",
17 ",", ";", "\\+", "-", "\\*", "/", "\\>\\>", "\\<\\<", "~",
18 "\\|", "&", "\\|\\|", "&&", "%", "\\.\\.\\.",
19 "\\+=", "-=", "\\*=", "/=", "%=", "\\<\\<=", "\\>\\>=",
20 "\\|=", "&="
23 LexerGen lex;
24 argc--; argv++;
25 if (argc > 0) {
26 lex.compile(argc,argv);
27 } else {
28 argv = grammar;
29 lex.compile(sizeof(grammar) / sizeof(grammar[0]), grammar);
31 if (! lex.ok())
32 printf("syntax error near rule %d: %s\n",lex.bad(),argv[lex.bad()]);
33 // lex.gen_code(cout, "lex");
35 ACGen ac;
36 const struct {
37 int len;
38 ACGen::Symbol string[4];
39 } keywords[] =
40 { { 2, { 'h', 'e' } },
41 { 3, { 's', 'h', 'e' } },
42 { 3, { 'h', 'i', 's' } },
43 { 4, { 'h', 'e', 'r', 's' }}
46 ac.start('a','z');
47 for (int i = 0; i < sizeof(keywords)/sizeof(keywords[0]); i++)
48 ac.add_string(i,keywords[i].len, keywords[i].string);
49 ac.finish();
51 ac.gen_code(cout, "ac");
52 assert(ac.size() == 11);
54 printf("OK\n");
55 return 0;