001 /* 002 * Created on Aug 30, 2003 by Ravi Mohan 003 * 004 */ 005 package aima.logic.propositional.parsing; 006 007 import java.util.HashSet; 008 import java.util.Set; 009 010 import aima.logic.common.Lexer; 011 import aima.logic.common.LogicTokenTypes; 012 import aima.logic.common.Token; 013 014 /** 015 * @author Ravi Mohan 016 * 017 */ 018 019 public class PELexer extends Lexer { 020 021 Set<String> connectors; 022 023 public PELexer() { 024 connectors = new HashSet<String>(); 025 connectors.add("NOT"); 026 connectors.add("AND"); 027 connectors.add("OR"); 028 connectors.add("=>"); 029 connectors.add("<=>"); 030 } 031 032 public PELexer(String inputString) { 033 this(); 034 setInput(inputString); 035 } 036 037 @Override 038 public Token nextToken() { 039 Token result = null; 040 int tokenType; 041 String tokenContent; 042 043 if (lookAhead(1) == '(') { 044 consume(); 045 return new Token(LogicTokenTypes.LPAREN, "("); 046 047 } else if (lookAhead(1) == ')') { 048 consume(); 049 return new Token(LogicTokenTypes.RPAREN, ")"); 050 } else if (identifierDetected()) { 051 return symbol(); 052 053 } else if (Character.isWhitespace(lookAhead(1))) { 054 consume(); 055 return nextToken(); 056 // return whiteSpace(); 057 } else if (lookAhead(1) == (char) -1) { 058 return new Token(LogicTokenTypes.EOI, "EOI"); 059 } else { 060 throw new RuntimeException("Lexing error on character " 061 + lookAhead(1)); 062 } 063 064 } 065 066 private boolean identifierDetected() { 067 return (Character.isJavaIdentifierStart((char) lookAheadBuffer[0])) 068 || partOfConnector(); 069 } 070 071 private boolean partOfConnector() { 072 return (lookAhead(1) == '=') || (lookAhead(1) == '<') 073 || (lookAhead(1) == '>'); 074 } 075 076 private Token symbol() { 077 StringBuffer sbuf = new StringBuffer(); 078 while ((Character.isLetterOrDigit(lookAhead(1))) 079 || (lookAhead(1) == '=') || (lookAhead(1) == '<') 080 || (lookAhead(1) == '>')) { 081 sbuf.append(lookAhead(1)); 082 consume(); 083 } 084 String symbol = sbuf.toString(); 085 if (isConnector(symbol)) { 086 return new Token(LogicTokenTypes.CONNECTOR, sbuf.toString()); 087 } else if (symbol.equalsIgnoreCase("true")) { 088 return new Token(LogicTokenTypes.TRUE, "TRUE"); 089 } else if (symbol.equalsIgnoreCase("false")) { 090 return new Token(LogicTokenTypes.FALSE, "FALSE"); 091 } else { 092 return new Token(LogicTokenTypes.SYMBOL, sbuf.toString()); 093 } 094 095 } 096 097 private Token connector() { 098 StringBuffer sbuf = new StringBuffer(); 099 while (Character.isLetterOrDigit(lookAhead(1))) { 100 sbuf.append(lookAhead(1)); 101 consume(); 102 } 103 return new Token(LogicTokenTypes.CONNECTOR, sbuf.toString()); 104 } 105 106 private Token whiteSpace() { 107 StringBuffer sbuf = new StringBuffer(); 108 while (Character.isWhitespace(lookAhead(1))) { 109 sbuf.append(lookAhead(1)); 110 consume(); 111 } 112 return new Token(LogicTokenTypes.WHITESPACE, sbuf.toString()); 113 114 } 115 116 private boolean isConnector(String aSymbol) { 117 return (connectors.contains(aSymbol)); 118 } 119 120 }