| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License |
| 7 | as published by the Free Software Foundation; either version 2 |
| 8 | of the License, or (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | //------------------------------------------------------------------------- |
| 21 | // |
| 22 | // Scanner implementation for FaCT++ program |
| 23 | // |
| 24 | //------------------------------------------------------------------------- |
| 25 | #include <cctype> // isalnum |
| 26 | |
| 27 | #include "fpp_assert.h" |
| 28 | |
| 29 | #include "scanner.h" |
| 30 | |
| 31 | // main methods |
| 32 | bool TsScanner :: isLegalIdChar ( char c ) const //id=[_a..z0-9[].] |
| 33 | { |
| 34 | switch (c) |
| 35 | { |
| 36 | case '(': |
| 37 | case ')': |
| 38 | case ';': |
| 39 | case '|': |
| 40 | return false; |
| 41 | default: |
| 42 | return !isspace(c) && !eof(c); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Word must be in a CAPITAL LETTERS |
| 47 | void TsScanner :: FillBuffer ( char c ) |
| 48 | { |
| 49 | unsigned int i = 0; |
| 50 | LexBuff [0] = c; |
| 51 | |
| 52 | while ( i < MaxIDLength && isLegalIdChar ( c = NextChar() ) ) |
| 53 | LexBuff[++i] = c; |
| 54 | |
| 55 | LexBuff[++i] = 0; |
| 56 | |
| 57 | if ( i > MaxIDLength ) |
| 58 | { |
| 59 | fpp_assert ( i == MaxIDLength + 1 ); |
| 60 | std::cerr << "Identifier was restricted to " << LexBuff << "\n"; |
| 61 | do { |
| 62 | c = NextChar(); |
| 63 | } while ( isLegalIdChar(c) ); |
| 64 | } |
| 65 | // OK or read the end of ID |
| 66 | |
| 67 | PutBack ( c ); |
| 68 | } |
| 69 | |
| 70 | void TsScanner :: FillNameBuffer ( char c ) |
| 71 | { |
| 72 | unsigned int i = 0; |
| 73 | const char stop = c; |
| 74 | |
| 75 | while ( i <= MaxIDLength && ( c = NextChar() ) != stop ) |
| 76 | LexBuff[i++] = c; |
| 77 | |
| 78 | LexBuff[i] = 0; |
| 79 | |
| 80 | if ( i > MaxIDLength ) |
| 81 | { |
| 82 | fpp_assert ( i == MaxIDLength + 1 ); |
| 83 | std::cerr << "Identifier was restricted to " << LexBuff << std::endl; |
| 84 | do { |
| 85 | c = NextChar(); |
| 86 | } while ( c != stop ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | LispToken TsScanner :: GetLex ( void ) |
| 91 | { |
| 92 | char c; |
| 93 | |
| 94 | while ( !eof( c = NextChar() ) ) |
| 95 | { |
| 96 | if ( c == ' ' || c == '\t' || c == 13 ) |
| 97 | continue; |
| 98 | |
| 99 | if ( c == '\n' ) |
| 100 | { |
| 101 | CurLine++; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if ( c == ';' ) |
| 106 | { // Skip comments |
| 107 | do { |
| 108 | c = NextChar(); |
| 109 | } while ( c != '\n' && !eof(c) ); |
| 110 | CurLine++; |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | // one-symbol tokens |
| 115 | if ( c == '(' ) |
| 116 | return LBRACK; |
| 117 | |
| 118 | if ( c == ')' ) |
| 119 | return RBRACK; |
| 120 | |
| 121 | if ( c == ':' ) // some keyword |
| 122 | { // skip colon |
| 123 | FillBuffer ( NextChar () ); |
| 124 | return ID; |
| 125 | } |
| 126 | |
| 127 | if ( c == '|' || c == '"' ) |
| 128 | { |
| 129 | FillNameBuffer ( c ); |
| 130 | return ID; |
| 131 | } |
| 132 | |
| 133 | if ( isdigit ( c ) ) // number |
| 134 | { |
| 135 | FillBuffer ( c ); |
| 136 | return NUM; |
| 137 | } |
| 138 | else // id |
| 139 | { |
| 140 | FillBuffer ( c ); |
| 141 | return ID; |
| 142 | } |
| 143 | } |
| 144 | // read EOF - end of lex |
| 145 | |
| 146 | return LEXEOF; |
| 147 | } |
| 148 | |
| 149 | // recognize TOP, BOTTOM or general ID |
| 150 | LispToken TsScanner :: getNameKeyword ( void ) const |
| 151 | { |
| 152 | if ( isKeyword ("*TOP*") || isKeyword ("TOP") ) |
| 153 | return L_TOP; |
| 154 | |
| 155 | if ( isKeyword ("*BOTTOM*") || isKeyword ("BOTTOM") ) |
| 156 | return L_BOTTOM; |
| 157 | |
| 158 | // not a keyword just an ID |
| 159 | return ID; |
| 160 | } |
| 161 | |
| 162 | // concept/role constructors; return BAD_LEX in case of error |
| 163 | LispToken TsScanner :: getExpressionKeyword ( void ) const |
| 164 | { |
| 165 | if ( isKeyword ("and") ) |
| 166 | return L_AND; |
| 167 | |
| 168 | if ( isKeyword ("or") ) |
| 169 | return L_OR; |
| 170 | |
| 171 | if ( isKeyword ("not") ) |
| 172 | return L_NOT; |
| 173 | |
| 174 | if ( isKeyword ("inv") || isKeyword ("inverse") ) |
| 175 | return L_INV; |
| 176 | |
| 177 | if ( isKeyword ("compose") ) |
| 178 | return L_RCOMPOSITION; |
| 179 | |
| 180 | if ( isKeyword ("project_into") ) |
| 181 | return L_PROJINTO; |
| 182 | |
| 183 | if ( isKeyword ("project_from") ) |
| 184 | return L_PROJFROM; |
| 185 | |
| 186 | if ( isKeyword ("some") ) |
| 187 | return L_EXISTS; |
| 188 | |
| 189 | if ( isKeyword ("all") ) |
| 190 | return L_FORALL; |
| 191 | |
| 192 | if ( isKeyword("min") || isKeyword("at-least") || isKeyword("atleast") ) |
| 193 | return L_GE; |
| 194 | |
| 195 | if ( isKeyword("max") || isKeyword("at-most") || isKeyword("atmost") ) |
| 196 | return L_LE; |
| 197 | |
| 198 | if ( isKeyword ("one-of") ) |
| 199 | return ONEOF; |
| 200 | |
| 201 | if ( isKeyword ("self-ref") ) |
| 202 | return REFLEXIVE; |
| 203 | |
| 204 | if ( isKeyword ("string") ) |
| 205 | return STRING; |
| 206 | |
| 207 | if ( isKeyword ("number") ) |
| 208 | return NUMBER; |
| 209 | |
| 210 | if ( isKeyword ("real") ) |
| 211 | return REAL; |
| 212 | |
| 213 | if ( isKeyword ("bool") ) |
| 214 | return BOOL; |
| 215 | |
| 216 | if ( isKeyword("gt") ) |
| 217 | return DTGT; |
| 218 | |
| 219 | if ( isKeyword("lt") ) |
| 220 | return DTLT; |
| 221 | |
| 222 | if ( isKeyword("ge") ) |
| 223 | return DTGE; |
| 224 | |
| 225 | if ( isKeyword("le") ) |
| 226 | return DTLE; |
| 227 | |
| 228 | if ( isKeyword("d-one-of") ) |
| 229 | return DONEOF; |
| 230 | |
| 231 | // not a keyword -- error |
| 232 | return BAD_LEX; |
| 233 | } |
| 234 | |
| 235 | // recognize FaCT++ keywords; return BAD_LEX if not found |
| 236 | LispToken TsScanner :: getCommandKeyword ( void ) const |
| 237 | { |
| 238 | // definitions |
| 239 | if ( isKeyword ("defprimconcept") ) |
| 240 | return PCONCEPT; |
| 241 | |
| 242 | if ( isKeyword ("defconcept") ) |
| 243 | return CONCEPT; |
| 244 | |
| 245 | if ( isKeyword ("defprimrole") ) |
| 246 | return PROLE; |
| 247 | |
| 248 | if ( isKeyword ("defdatarole") ) |
| 249 | return DATAROLE; |
| 250 | |
| 251 | if ( isKeyword ("defprimattribute") ) |
| 252 | return PATTR; |
| 253 | |
| 254 | if ( isKeyword ("defindividual") ) |
| 255 | return DEFINDIVIDUAL; |
| 256 | |
| 257 | // general relations |
| 258 | if ( isKeyword ("implies") || isKeyword ("implies_c") ) |
| 259 | return SUBSUMES; |
| 260 | |
| 261 | if ( isKeyword ("equal_c") ) |
| 262 | return EQUAL_C; |
| 263 | |
| 264 | if ( isKeyword ("disjoint") || isKeyword ("disjoint_c") ) |
| 265 | return DISJOINT; |
| 266 | |
| 267 | if ( isKeyword ("implies_r") ) |
| 268 | return IMPLIES_R; |
| 269 | |
| 270 | if ( isKeyword ("equal_r") ) |
| 271 | return EQUAL_R; |
| 272 | |
| 273 | if ( isKeyword ("disjoint_r") ) |
| 274 | return DISJOINT_R; |
| 275 | |
| 276 | if ( isKeyword ("inverse") ) |
| 277 | return INVERSE; |
| 278 | |
| 279 | // role stuff |
| 280 | if ( isKeyword ("functional") ) |
| 281 | return FUNCTIONAL; |
| 282 | |
| 283 | if ( isKeyword ("transitive") ) |
| 284 | return TRANSITIVE; |
| 285 | |
| 286 | if ( isKeyword ("reflexive") ) |
| 287 | return REFLEXIVE; |
| 288 | |
| 289 | if ( isKeyword ("irreflexive") ) |
| 290 | return IRREFLEXIVE; |
| 291 | |
| 292 | if ( isKeyword ("symmetric") ) |
| 293 | return SYMMETRIC; |
| 294 | |
| 295 | if ( isKeyword ("asymmetric") ) |
| 296 | return ASYMMETRIC; |
| 297 | |
| 298 | if ( isKeyword ("range") ) |
| 299 | return ROLERANGE; |
| 300 | |
| 301 | if ( isKeyword ("domain") ) |
| 302 | return ROLEDOMAIN; |
| 303 | |
| 304 | // individual stuff |
| 305 | if ( isKeyword ("instance") ) |
| 306 | return INSTANCE; |
| 307 | |
| 308 | if ( isKeyword ("related") ) |
| 309 | return RELATED; |
| 310 | |
| 311 | if (isKeyword("valueOf")) |
| 312 | return VALUEOF; |
| 313 | |
| 314 | if ( isKeyword ("same") ) |
| 315 | return SAME; |
| 316 | |
| 317 | if ( isKeyword ("different") ) |
| 318 | return DIFFERENT; |
| 319 | |
| 320 | if ( isKeyword ("fairness") ) |
| 321 | return FAIRNESS; |
| 322 | |
| 323 | // not a keyword -- error |
| 324 | return BAD_LEX; |
| 325 | } |