Revision 45

Date:
2010/11/15 17:47:07
Author:
gdshaw@RISCID.ORG
Revision Log:
Added support for unary rules.
Files:

Legend:

 
Added
 
Removed
 
Modified
  • trunk/parser/fragtable.cc

     
    44 44
    45 45 void fragtable::apply(const grammar& rules)
    46 46 {
    47 for (size_t i=0;i!=_capacity;++i)
    48 {
    49 tags(i,1)|=rules.find(tags(i,1));
    50 }
    51
    47 52 // Loop 1: begin constructing fragments of length 2,
    48 53 // end with one that fills the whole buffer.
    49 54 for (size_t i=2;i!=_capacity+1;++i)
     
    57 62 for (size_t k=1;k!=i;++k)
    58 63 {
    59 64 tags(j,i)|=rules.find(tags(j,k),tags(j+k,i-k));
    65 tags(j,i)|=rules.find(tags(j,i));
    60 66 }
    61 67 }
    62 68 }
  • trunk/parser/grammar.cc

     
    41 41 if (i==j) throw std::runtime_error("tag expected");
    42 42 std::string lhs=line.substr(j,i-j);
    43 43 while ((i!=line.size())&&(line[i]==' ')) ++i;
    44 j=i;
    45 while ((i!=line.size())&&(isalpha(line[i]))) ++i;
    46 if (i==j) throw std::runtime_error("tag expected");
    47 std::string rhs=line.substr(j,i-j);
    48 while ((i!=line.size())&&(line[i]==' ')) ++i;
    49 if (i!=line.size()) throw std::runtime_error(
    50 "end of line expected");
    51 _binary_rules.push_back(binary_rule(lhs,rhs,result));
    44 if (i==line.size())
    45 {
    46 _unary_rules.push_back(unary_rule(lhs,result));
    47 }
    48 else
    49 {
    50 j=i;
    51 while ((i!=line.size())&&(isalpha(line[i]))) ++i;
    52 if (i==j) throw std::runtime_error("tag expected");
    53 std::string rhs=line.substr(j,i-j);
    54 while ((i!=line.size())&&(line[i]==' ')) ++i;
    55 if (i!=line.size()) throw std::runtime_error(
    56 "end of line expected");
    57 _binary_rules.push_back(binary_rule(lhs,rhs,result));
    58 }
    52 59 }
    53 60 }
    54 61 }
    55 62
    63 tagset grammar::find(const tagset& tags) const
    64 {
    65 tagset result;
    66 bool found=true;
    67 while (found)
    68 {
    69 found=false;
    70 for (std::vector<unary_rule>::const_iterator i=_unary_rules.begin();
    71 i!=_unary_rules.end();++i)
    72 {
    73 if (tags.find(i->arg())&&!result.find(i->result()))
    74 {
    75 found=true;
    76 result|=i->result();
    77 }
    78 }
    79 }
    80 return result;
    81 }
    82
    56 83 tagset grammar::find(const tagset& lhs,const tagset& rhs) const
    57 84 {
    58 85 tagset result;
  • trunk/parser/grammar.h

     
    8 8 #define LEXICON_PARSER_GRAMMAR
    9 9
    10 10 #include <vector>
    11 #include <map>
    11 12 #include <string>
    12 13
    14 #include "unary_rule.h"
    13 15 #include "binary_rule.h"
    14 16
    15 17 class tagset;
     
    21 23 * - a comment, or
    22 24 * - a blank line.
    23 25 *
    26 * Unary rules are of the form "result <- arg" where:
    27 * - arg is the tag that the fragment must already possess
    28 * - result is the that to be applied.
    29 *
    24 30 * Binary rules are of the form "result <- lhs rhs" where:
    25 31 * - lhs is the tag that the left-hand fragment must possess,
    26 32 * - rhs is the tag that the right-hand fragment must possess,
     
    29 35 class grammar
    30 36 {
    31 37 private:
    38 /** The unary rules. */
    39 std::vector<unary_rule> _unary_rules;
    40
    32 41 /** The binary rules. */
    33 42 std::vector<binary_rule> _binary_rules;
    34 43 public:
     
    37 46 */
    38 47 void read(const std::string& pathname);
    39 48
    49 /** Find unary rules matching tagset.
    50 * @param tags the existing tags
    51 * @return the tags to be added
    52 */
    53 tagset find(const tagset& tags) const;
    54
    40 55 /** Find binary rules matching tagsets.
    41 56 * @param lhs the tags possible for the left-hand fragment
    42 57 * @param rhs the tags possible for the right-hand fragment
  • trunk/parser/Makefile.am

     
    10 10 tag.cc \
    11 11 tagset.cc \
    12 12 lexicon.cc \
    13 unary_rule.cc \
    13 14 binary_rule.cc \
    14 15 grammar.cc \
    15 16 fragtable.cc \
  • trunk/parser/unary_rule.cc

     
    1 // © 2010 Graham Shaw.
    2 // Copying and distribution of this file, with or without modification,
    3 // are permitted in any medium without royalty provided the copyright
    4 // notice and this notice are preserved. This file is offered as-is,
    5 // without any warranty.
    6
    7 #include "unary_rule.h"
    8
    9 unary_rule::unary_rule(tag arg,tag result):
    10 _arg(arg),
    11 _result(result)
    12 {}
  • trunk/parser/unary_rule.h

     
    1 // © 2010 Graham Shaw.
    2 // Copying and distribution of this file, with or without modification,
    3 // are permitted in any medium without royalty provided the copyright
    4 // notice and this notice are preserved. This file is offered as-is,
    5 // without any warranty.
    6
    7 #ifndef LEXICON_PARSER_UNARY_RULE
    8 #define LEXICON_PARSER_UNARY_RULE
    9
    10 #include "tag.h"
    11
    12 /** A class to represent a unary grammatical rule.
    13 * A unary rule states that when a given tag is applied to a text
    14 * fragment then some other tag should be added too.
    15 */
    16 class unary_rule
    17 {
    18 private:
    19 /** The tag that a fragment must possess in order to match. */
    20 tag _arg;
    21
    22 /** The tag to be applied to the result. */
    23 tag _result;
    24 public:
    25 /** Construct unary rule.
    26 * @param arg the tag that a fragment must possess in order
    27 * to match.
    28 * @param result the tag to be applied to the result
    29 */
    30 unary_rule(tag arg,tag result);
    31
    32 /** Get tag that fragment must possess in order to match.
    33 * @return the tag
    34 */
    35 tag arg() const
    36 { return _arg; }
    37
    38 /** Get tag to be applied to result
    39 * @return the tag
    40 */
    41 tag result() const
    42 { return _result; }
    43 };
    44
    45 #endif