//===--- Main.cpp - Compile BNF grammar -----------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This is a tool to compile a BNF grammar, it is used by the build system to // generate a necessary data bits to statically construct core pieces (Grammar, // LRTable etc) of the LR parser. // //===----------------------------------------------------------------------===// #include "clang-pseudo/grammar/Grammar.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/ToolOutputFile.h" #include <algorithm> desc; init; opt; Required; value_desc; values; namespace { enum EmitType { … }; opt<std::string> Grammar("grammar", desc("Parse a BNF grammar file."), Required); opt<EmitType> Emit(desc("which information to emit:"), values(clEnumValN(EmitSymbolList, "emit-symbol-list", "Print nonterminal symbols (default)"), clEnumValN(EmitGrammarContent, "emit-grammar-content", "Print the BNF grammar content as a string"))); opt<std::string> OutputFilename("o", init("-"), desc("Output"), value_desc("file")); std::string readOrDie(llvm::StringRef Path) { … } } // namespace namespace clang { namespace pseudo { namespace { // Mangles a symbol name into a valid identifier. // // These follow names in the grammar fairly closely: // nonterminal: `ptr-declarator` becomes `ptr_declarator`; // punctuator: `,` becomes `COMMA`; // keyword: `INT` becomes `INT`; // terminal: `IDENTIFIER` becomes `IDENTIFIER`; std::string mangleSymbol(SymbolID SID, const Grammar &G) { … } // Mangles the RHS of a rule definition into a valid identifier. // // These are unique only for a fixed LHS. // e.g. for the grammar rule `ptr-declarator := ptr-operator ptr-declarator`, // it is `ptr_operator__ptr_declarator`. std::string mangleRule(RuleID RID, const Grammar &G) { … } } // namespace } // namespace pseudo } // namespace clang int main(int argc, char *argv[]) { … }