]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/tools/build/src/engine/yyacc.cpp
1 /* Copyright 2002, 2020 Rene Rivera.
2 ** Distributed under the Boost Software License, Version 1.0.
3 ** (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
14 # yyacc - yacc wrapper
16 # Allows tokens to be written as `literal` and then automatically
17 # substituted with #defined tokens.
20 # yyacc file.y filetab.h file.yy
23 # file.yy yacc grammar with ` literals
27 # filetab.h array of string <-> token mappings
30 # Documented and p moved in sed command (for some reason,
31 # s/x/y/p doesn't work).
33 # Take basename as second argument.
35 # reversed order of args to be compatible with GenFile rule
37 # Reimplemented as a C program for portability. (Rene Rivera)
39 # Reimplement yet again, in C++. (Rene Rivera)
42 static const std::string usage
[] = {
43 "yyacc <grammar output.y> <token table output.h> <grammar source.yy>"
50 std::printf("%s\n", u
.c_str());
54 std::string
tokenize_string(std::string s
)
56 std::string result
= s
;
57 if (s
== ":") result
= "_colon";
58 else if (s
== "!") result
= "_bang";
59 else if (s
== "!=") result
= "_bang_equals";
60 else if (s
== "&&") result
= "_amperamper";
61 else if (s
== "&") result
= "_amper";
62 else if (s
== "+") result
= "_plus";
63 else if (s
== "+=") result
= "_plus_equals";
64 else if (s
== "||") result
= "_barbar";
65 else if (s
== "|") result
= "_bar";
66 else if (s
== ";") result
= "_semic";
67 else if (s
== "-") result
= "_minus";
68 else if (s
== "<") result
= "_langle";
69 else if (s
== "<=") result
= "_langle_equals";
70 else if (s
== ">") result
= "_rangle";
71 else if (s
== ">=") result
= "_rangle_equals";
72 else if (s
== ".") result
= "_period";
73 else if (s
== "?") result
= "_question";
74 else if (s
== "?=") result
= "_question_equals";
75 else if (s
== "=") result
= "_equals";
76 else if (s
== ",") result
= "_comma";
77 else if (s
== "[") result
= "_lbracket";
78 else if (s
== "]") result
= "_rbracket";
79 else if (s
== "{") result
= "_lbrace";
80 else if (s
== "}") result
= "_rbrace";
81 else if (s
== "(") result
= "_lparen";
82 else if (s
== ")") result
= "_rparen";
85 result
.begin(), result
.end(), result
.begin(),
86 [](unsigned char c
){ return std::toupper(c
); });
95 bool operator<(const literal
& x
) const
97 return this->string
< x
.string
;
101 int main(int argc
, char ** argv
)
111 FILE * token_output_f
= 0;
112 FILE * grammar_output_f
= 0;
113 FILE * grammar_source_f
= 0;
115 grammar_source_f
= fopen(argv
[3],"r");
116 if (grammar_source_f
== 0) { result
= 1; }
119 std::set
<literal
> literals
;
123 if (fgets(l
,2048,grammar_source_f
) != 0)
128 char * c1
= std::strchr(c
,'`');
131 char * c2
= std::strchr(c1
+1,'`');
134 auto l
= std::string(c1
+1,c2
-c1
-1);
135 literals
.insert({ l
, tokenize_string(l
) });
150 token_output_f
= std::fopen(argv
[2],"w");
151 if (token_output_f
!= 0)
153 for (const literal
& l
: literals
)
155 std::fprintf(token_output_f
," { \"%s\", %s },\n",l
.string
.c_str(), l
.token
.c_str());
157 std::fclose(token_output_f
);
163 grammar_output_f
= std::fopen(argv
[1],"w");
164 if (grammar_output_f
!= 0)
166 for (const literal
& l
: literals
)
168 fprintf(grammar_output_f
,"%%token %s\n",l
.token
.c_str());
170 rewind(grammar_source_f
);
173 if (fgets(l
,2048,grammar_source_f
) != 0)
178 char * c1
= strchr(c
,'`');
181 char * c2
= strchr(c1
+1,'`');
184 auto replacement
= literals
.find({std::string(c1
+1,c2
-c1
-1), ""});
186 std::fprintf(grammar_output_f
,"%s%s",c
,replacement
->token
.c_str());
191 std::fprintf(grammar_output_f
,"%s",c
);
197 std::fprintf(grammar_output_f
,"%s",c
);
207 std::fclose(grammar_output_f
);