⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lexer.java

📁 本设计Tiger语言为源语言
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
   */  protected static int find_single_char(int ch)    {      Integer result;      result = (Integer)char_symbols.get(new Integer((char)ch));      if (result == null) 	return -1;      else	return result.intValue();    }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** Handle swallowing up a comment.  Both old style C and new style C++   *  comments are handled.   */  protected static void swallow_comment() throws java.io.IOException    {      /* next_char == '/' at this point */      /* is it a traditional comment */      if (next_char2 == '*')	{	  /* swallow the opener */	  advance(); advance();	  /* swallow the comment until end of comment or EOF */	  for (;;)	    {	      /* if its EOF we have an error */	      if (next_char == EOF_CHAR)		{		  emit_error("Specification file ends inside a comment");		  return;		}	      /* if we can see the closer we are done */	      if (next_char == '*' && next_char2 == '/')		{		  advance();		  advance();		  return;		}	      /* otherwise swallow char and move on */	      advance();	    }	}      /* is its a new style comment */      if (next_char2 == '/')	{	  /* swallow the opener */	  advance(); advance();	  /* swallow to '\n', '\r', '\f', or EOF */ 	  while (next_char != '\n' && next_char != '\r' && 		 next_char != '\f' && next_char!=EOF_CHAR)	    advance();	  return;	}      /* shouldn't get here, but... if we get here we have an error */      emit_error("Malformed comment in specification -- ignored");      advance();    }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** Swallow up a code string.  Code strings begin with "{:" and include      all characters up to the first occurrence of ":}" (there is no way to       include ":}" inside a code string).  The routine returns a String      object suitable for return by the scanner.   */  protected static Symbol do_code_string() throws java.io.IOException    {      StringBuffer result = new StringBuffer();      /* at this point we have lookahead of "{:" -- swallow that */      advance(); advance();      /* save chars until we see ":}" */      while (!(next_char == ':' && next_char2 == '}'))	{	  /* if we have run off the end issue a message and break out of loop */	  if (next_char == EOF_CHAR)	    {	      emit_error("Specification file ends inside a code string");	      break;	    }	  /* otherwise record the char and move on */	  result.append(new Character((char)next_char));	  advance();	}      /* advance past the closer and build a return Symbol */      advance(); advance();      return new Symbol(sym.CODE_STRING, result.toString());    }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** Process an identifier.  Identifiers begin with a letter, underscore,   *  or dollar sign, which is followed by zero or more letters, numbers,   *  underscores or dollar signs.  This routine returns a String suitable   *  for return by the scanner.   */  protected static Symbol do_id() throws java.io.IOException    {      StringBuffer result = new StringBuffer();      String       result_str;      Integer      keyword_num;      char         buffer[] = new char[1];      /* next_char holds first character of id */      buffer[0] = (char)next_char;      result.append(buffer,0,1);      advance();      /* collect up characters while they fit in id */       while(id_char(next_char))	{          buffer[0] = (char)next_char;	  result.append(buffer,0,1);	  advance();	}      /* extract a string and try to look it up as a keyword */      result_str = result.toString();      keyword_num = (Integer)keywords.get(result_str);      /* if we found something, return that keyword */      if (keyword_num != null)	return new Symbol(keyword_num.intValue());      /* otherwise build and return an id Symbol with an attached string */      return new Symbol(sym.ID, result_str);    }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** Return one Symbol.  This is the main external interface to the scanner.   *  It consumes sufficient characters to determine the next input Symbol   *  and returns it.  To help with debugging, this routine actually calls   *  real_next_token() which does the work.  If you need to debug the    *  parser, this can be changed to call debug_next_token() which prints   *  a debugging message before returning the Symbol.   */  public static Symbol next_token() throws java.io.IOException    {      return real_next_token();    }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** Debugging version of next_token().  This routine calls the real scanning   *  routine, prints a message on System.out indicating what the Symbol is,   *  then returns it.   */  public static Symbol debug_next_token() throws java.io.IOException    {      Symbol result = real_next_token();      System.out.println("# next_Symbol() => " + result.sym);      return result;    }  /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/  /** The actual routine to return one Symbol.  This is normally called from   *  next_token(), but for debugging purposes can be called indirectly from   *  debug_next_token().    */  protected static Symbol real_next_token() throws java.io.IOException    {      int sym_num;      for (;;)	{	  /* look for white space */	  if (next_char == ' ' || next_char == '\t' || next_char == '\n' ||	      next_char == '\f' ||  next_char == '\r')	    {	      /* advance past it and try the next character */	      advance();	      continue;	    }	  /* look for a single character symbol */	  sym_num = find_single_char(next_char);	  if (sym_num != -1)	    {	      /* found one -- advance past it and return a Symbol for it */	      advance();	      return new Symbol(sym_num);	    }	  /* look for : or ::= */	  if (next_char == ':')	    {	      /* if we don't have a second ':' return COLON */	      if (next_char2 != ':') 		{		  advance();		  return new Symbol(sym.COLON);		}	      /* move forward and look for the '=' */	      advance();	      if (next_char2 == '=') 		{		  advance(); advance();		  return new Symbol(sym.COLON_COLON_EQUALS);		}	      else		{		  /* return just the colon (already consumed) */		  return new Symbol(sym.COLON);		}	    }	  /* find a "%prec" string and return it.  otherwise, a '%' was found,	     which has no right being in the specification otherwise */	  if (next_char == '%') {	    advance();	    if ((next_char == 'p') && (next_char2 == 'r') && (next_char3 == 'e') && 		(next_char4 == 'c')) {	      advance();	      advance();	      advance();	      advance();	      return new Symbol(sym.PERCENT_PREC);	    } else {	      emit_error("Found extraneous percent sign");	    }	  }	  /* look for a comment */	  if (next_char == '/' && (next_char2 == '*' || next_char2 == '/'))	    {	      /* swallow then continue the scan */	      swallow_comment();	      continue;	    }	  /* look for start of code string */	  if (next_char == '{' && next_char2 == ':')	    return do_code_string();	  /* look for an id or keyword */	  if (id_start_char(next_char)) return do_id();	  /* look for EOF */	  if (next_char == EOF_CHAR) return new Symbol(sym.EOF);	  /* if we get here, we have an unrecognized character */	  emit_warn("Unrecognized character '" + 	    new Character((char)next_char) + "'(" + next_char + 	    ") -- ignored");	  /* advance past it */	  advance();	}    }  /*-----------------------------------------------------------*/}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -