fml_parser.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright 2016 Google Inc. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. // Feature modeling language (fml) parser.
  13. //
  14. // BNF grammar for fml:
  15. //
  16. // <feature model> ::= { <feature extractor> }
  17. //
  18. // <feature extractor> ::= <extractor spec> |
  19. // <extractor spec> '.' <feature extractor> |
  20. // <extractor spec> '{' { <feature extractor> } '}'
  21. //
  22. // <extractor spec> ::= <extractor type>
  23. // [ '(' <parameter list> ')' ]
  24. // [ ':' <extractor name> ]
  25. //
  26. // <parameter list> = ( <parameter> | <argument> ) { ',' <parameter> }
  27. //
  28. // <parameter> ::= <parameter name> '=' <parameter value>
  29. //
  30. // <extractor type> ::= NAME
  31. // <extractor name> ::= NAME | STRING
  32. // <argument> ::= NUMBER
  33. // <parameter name> ::= NAME
  34. // <parameter value> ::= NUMBER | STRING | NAME
  35. #ifndef SYNTAXNET_FML_PARSER_H_
  36. #define SYNTAXNET_FML_PARSER_H_
  37. #include <string>
  38. #include "syntaxnet/utils.h"
  39. #include "syntaxnet/feature_extractor.pb.h"
  40. namespace syntaxnet {
  41. class FMLParser {
  42. public:
  43. // Parses fml specification into feature extractor descriptor.
  44. void Parse(const string &source, FeatureExtractorDescriptor *result);
  45. private:
  46. // Initializes the parser with the source text.
  47. void Initialize(const string &source);
  48. // Outputs error message and exits.
  49. void Error(const string &error_message);
  50. // Moves to the next input character.
  51. void Next();
  52. // Moves to the next input item.
  53. void NextItem();
  54. // Parses a feature descriptor.
  55. void ParseFeature(FeatureFunctionDescriptor *result);
  56. // Parses a parameter specification.
  57. void ParseParameter(FeatureFunctionDescriptor *result);
  58. // Returns true if end of source input has been reached.
  59. bool eos() { return current_ == source_.end(); }
  60. // Item types.
  61. enum ItemTypes {
  62. END = 0,
  63. NAME = -1,
  64. NUMBER = -2,
  65. STRING = -3,
  66. };
  67. // Source text.
  68. string source_;
  69. // Current input position.
  70. string::iterator current_;
  71. // Line number for current input position.
  72. int line_number_;
  73. // Start position for current item.
  74. string::iterator item_start_;
  75. // Start position for current line.
  76. string::iterator line_start_;
  77. // Line number for current item.
  78. int item_line_number_;
  79. // Item type for current item. If this is positive it is interpreted as a
  80. // character. If it is negative it is interpreted as an item type.
  81. int item_type_;
  82. // Text for current item.
  83. string item_text_;
  84. };
  85. } // namespace syntaxnet
  86. #endif // SYNTAXNET_FML_PARSER_H_