Debian Codegraph @

src/tools/qlalr/examples/dummy-xml/ll/dummy-xml-ll.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include <cstdlib>
5 #include <cstdio>
6
7 enum Token {
8 EOF_SYMBOL,
9 LEFT_ANGLE,
10 RIGHT_ANGLE,
11 ANY,
12 };
13
14 static int current_char;
15 static int yytoken;
16 static bool in_tag = false;
17
18 bool parseXmlStream();
19 bool parseTagOrWord();
20 bool parseTagName();
21
22 inline int nextToken()
23 {
24 current_char = fgetc(stdin);
25 if (current_char == EOF) {
26 return (yytoken = EOF_SYMBOL);
27 } else if (current_char == '<') {
28 in_tag = true;
29 return (yytoken = LEFT_ANGLE);
30 } else if (in_tag && current_char == '>') {
31 in_tag = false;
32 return (yytoken = RIGHT_ANGLE);
33 }
34 return (yytoken = ANY);
35 }
36
37 bool parse()
38 {
39 nextToken();
40 return parseXmlStream();
41 }
42
43 bool parseXmlStream()
44 {
45 while (parseTagOrWord())
46 ;
47
48 return true;
49 }
50
51 bool parseTagOrWord()
52 {
53 if (yytoken == LEFT_ANGLE) {
54 nextToken();
55 if (! parseTagName())
56 return false;
57 if (yytoken != RIGHT_ANGLE)
58 return false;
59 nextToken();
60
61 fprintf (stderr, "*** found a tag\n");
62
63 } else if (yytoken == ANY) {
64 nextToken();
65 } else {
66 return false;
67 }
68 return true;
69 }
70
71 bool parseTagName()
72 {
73 while (yytoken == ANY)
74 nextToken();
75
76 return true;
77 }
78
79 int main()
80 {
81 if (parse())
82 printf("OK\n");
83 else
84 printf("KO\n");
85 }