コード例 #1
0
ファイル: Lexer.java プロジェクト: aensley89/ast_parser
 /**
  * build the token for operators (+ -) or separators (parens, braces) filter out comments which
  * begin with two slashes
  *
  * @param s is the String representing the token
  * @param startPosition is the column in the source file where the token begins
  * @param endPosition is the column in the source file where the token ends
  * @return the Token just found
  */
 public Token makeToken(String s, int startPosition, int endPosition) {
   if (s.equals("//")) { // filter comment
     try {
       int oldLine = source.getLineno();
       do {
         ch = source.read();
       } while (oldLine == source.getLineno());
     } catch (Exception e) {
       atEOF = true;
     }
     return nextToken();
   }
   Symbol sym = Symbol.symbol(s, Tokens.BogusToken); // be sure it's a valid token
   if (sym == null) {
     System.out.println("******** illegal character: " + s + " " + "Line: " + source.getLineno());
     atEOF = true;
     return nextToken();
   }
   return new Token(startPosition, endPosition, sym);
 }
コード例 #2
0
ファイル: Env.java プロジェクト: IvyHan2013/Tiger
  public void initialize() {
    venv = new Table();
    tenv = new Table();

    NAME nameint = new NAME(Symbol.symbol("int"));
    nameint.bind(new INT());
    tenv.put(nameint.name, nameint);

    NAME namestring = new NAME(Symbol.symbol("string"));
    namestring.bind(new STRING());
    tenv.put(namestring.name, namestring);

    NAME namenil = new NAME(Symbol.symbol("nil"));
    namenil.bind(new NIL());
    tenv.put(namenil.name, namenil);

    Symbol print = Symbol.symbol("print");
    RECORD printargu = new RECORD(Symbol.symbol("s"), newstring, null);
    venv.put(
        print,
        new FunEntry(
            new Level(Semant.globalLevZero, print, new BoolList(true, null)),
            new Label(),
            printargu,
            newvoid,
            true));

    Symbol printi = Symbol.symbol("printi");
    RECORD printiargu = new RECORD(Symbol.symbol("i"), newint, null);
    venv.put(
        printi,
        new FunEntry(
            new Level(Semant.globalLevZero, printi, new BoolList(true, null)),
            new Label(),
            printiargu,
            newvoid,
            true));

    Symbol flush = Symbol.symbol("flush");
    RECORD flushargu = null;
    venv.put(
        flush,
        new FunEntry(
            new Level(Semant.globalLevZero, flush, null), new Label(), flushargu, newvoid, true));

    Symbol getchar = Symbol.symbol("getchar");
    RECORD getcharargu = null;
    venv.put(
        getchar,
        new FunEntry(
            new Level(Semant.globalLevZero, getchar, null),
            new Label(),
            getcharargu,
            newstring,
            true));

    Symbol ord = Symbol.symbol("ord");
    RECORD ordargu = new RECORD(Symbol.symbol("s"), newstring, null);
    venv.put(
        ord,
        new FunEntry(
            new Level(Semant.globalLevZero, ord, new BoolList(true, null)),
            new Label(),
            ordargu,
            newint,
            true));

    Symbol chr = Symbol.symbol("chr");
    RECORD chrargu = new RECORD(Symbol.symbol("i"), newint, null);
    venv.put(
        chr,
        new FunEntry(
            new Level(Semant.globalLevZero, chr, new BoolList(true, null)),
            new Label(),
            chrargu,
            newstring,
            true));

    Symbol size = Symbol.symbol("size");
    RECORD sizeargu = new RECORD(Symbol.symbol("s"), newstring, null);
    venv.put(
        size,
        new FunEntry(
            new Level(Semant.globalLevZero, size, new BoolList(true, null)),
            new Label(),
            sizeargu,
            newint,
            true));

    Symbol substring = Symbol.symbol("substring");
    RECORD substringargu3 = new RECORD(Symbol.symbol("n"), newint, null);
    RECORD substringargu2 = new RECORD(Symbol.symbol("first"), newint, substringargu3);
    RECORD substringargu1 = new RECORD(Symbol.symbol("s"), newstring, substringargu2);
    RECORD substringargu = substringargu1;
    venv.put(
        substring,
        new FunEntry(
            new Level(
                Semant.globalLevZero,
                substring,
                new BoolList(true, new BoolList(true, new BoolList(true, null)))),
            new Label(),
            substringargu,
            newstring,
            true));

    Symbol concat = Symbol.symbol("concat");
    RECORD concatargu2 = new RECORD(Symbol.symbol("s2"), newstring, null);
    RECORD concatargu1 = new RECORD(Symbol.symbol("s1"), newstring, concatargu2);
    RECORD concatargu = concatargu1;
    venv.put(
        concat,
        new FunEntry(
            new Level(Semant.globalLevZero, concat, new BoolList(true, new BoolList(true, null))),
            new Label(),
            concatargu,
            newstring,
            true));

    Symbol not = Symbol.symbol("not");
    RECORD notargu = new RECORD(Symbol.symbol("i"), newint, null);
    venv.put(
        not,
        new FunEntry(
            new Level(Semant.globalLevZero, not, new BoolList(true, null)),
            new Label(),
            notargu,
            newint,
            true));

    Symbol exit = Symbol.symbol("exit");
    RECORD exitargu = new RECORD(Symbol.symbol("i"), newint, null);
    venv.put(
        exit,
        new FunEntry(
            new Level(Semant.globalLevZero, exit, new BoolList(true, null)),
            new Label(),
            exitargu,
            newvoid,
            true));
  }
コード例 #3
0
ファイル: Lexer.java プロジェクト: aensley89/ast_parser
 public Token newCharToken(String number, int startPosition2, int endPosition2) {
   return new Token(startPosition, endPosition, Symbol.symbol(number, Tokens.Char));
 }
コード例 #4
0
ファイル: Lexer.java プロジェクト: aensley89/ast_parser
 public Token newSciToken(String number, int startPosition2, int endPosition2) {
   return new Token(startPosition, endPosition, Symbol.symbol(number, Tokens.ScientificN));
 }
コード例 #5
0
ファイル: Lexer.java プロジェクト: aensley89/ast_parser
 public Token newFloatToken(String number, int startPosition, int endPosition) {
   return new Token(startPosition, endPosition, Symbol.symbol(number, Tokens.Float));
 }
コード例 #6
0
ファイル: Lexer.java プロジェクト: aensley89/ast_parser
 /**
  * number tokens are inserted in the symbol table; we don't convert the numeric strings to numbers
  * until we load the bytecodes for interpreting; this ensures that any machine numeric
  * dependencies are deferred until we actually run the program; i.e. the numeric constraints of
  * the hardware used to compile the source program are not used
  *
  * @param number is the int String just scanned
  * @param startPosition is the column in the source file where the int begins
  * @param endPosition is the column in the source file where the int ends
  * @return the int Token
  */
 public Token newNumberToken(String number, int startPosition, int endPosition) {
   return new Token(startPosition, endPosition, Symbol.symbol(number, Tokens.INTeger));
 }
コード例 #7
0
ファイル: Lexer.java プロジェクト: aensley89/ast_parser
 /**
  * newIdTokens are either ids or reserved words; new id's will be inserted in the symbol table
  * with an indication that they are id's
  *
  * @param id is the String just scanned - it's either an id or reserved word
  * @param startPosition is the column in the source file where the token begins
  * @param endPosition is the column in the source file where the token ends
  * @return the Token; either an id or one for the reserved words
  */
 public Token newIdToken(String id, int startPosition, int endPosition) {
   return new Token(startPosition, endPosition, Symbol.symbol(id, Tokens.Identifier));
 }
コード例 #8
0
ファイル: Lexer.java プロジェクト: aensley89/ast_parser
  /** @return the next Token found in the source file */
  public Token nextToken() { // ch is always the next char to process
    if (atEOF) {
      if (source != null) {
        source.close();
        source = null;
      }
      return null;
    }
    try {
      while (Character.isWhitespace(ch)) { // scan past whitespace
        ch = source.read();
      }
    } catch (Exception e) {
      atEOF = true;
      return nextToken();
    }
    startPosition = source.getPosition();
    endPosition = startPosition - 1;

    if (Character.isJavaIdentifierStart(ch)) {
      // return tokens for ids and reserved words
      String id = "";
      try {
        do {
          endPosition++;
          id += ch;
          ch = source.read();
        } while (Character.isJavaIdentifierPart(ch));
      } catch (Exception e) {
        atEOF = true;
      }
      return newIdToken(id, startPosition, endPosition);
    }

    // This block is a translation of state diagram that I drew
    // case number starts with digit
    if (Character.isDigit(ch)) {
      try {
        String number = "";

        // Read the second item
        endPosition++;
        number += ch;
        ch = source.read();
        // check if the second item is digit, decimal or neither

        // second item is digit
        if (Character.isDigit(ch)) {
          // move to section 1 then checke for decimal
          do {
            endPosition++;
            number += ch;
            ch = source.read();
          } while (Character.isDigit(ch));

          if (ch == '.') {

            do {
              endPosition++;
              number += ch;
              ch = source.read();

            } while (Character.isDigit(ch));

            return newFloatToken(number, startPosition, endPosition);
          } else {
            return newNumberToken(number, startPosition, endPosition);
          }

        } // second item is decimal
        else if (ch == '.') {
          // move to section 2 check for scientific notaiton
          do {
            endPosition++;
            number += ch;
            ch = source.read();
          } while (Character.isDigit(ch));

          if (ch == 'E' || ch == 'e') {
            // move to scientific notation
            endPosition++;
            number += ch;
            ch = source.read();

            if (ch == '+' || ch == '-') {
              endPosition++;
              number += ch;
              ch = source.read();

              if (Character.isDigit(ch)) {
                do {
                  endPosition++;
                  number += ch;
                  ch = source.read();
                } while (Character.isDigit(ch));

                return newSciToken(number, startPosition, endPosition);
              } else {
                return newErrorToken(number, startPosition, endPosition);
              }

            } else if (Character.isDigit(ch)) {
              do {
                endPosition++;
                number += ch;
                ch = source.read();
              } while (Character.isDigit(ch));

              return newSciToken(number, startPosition, endPosition);
            } else {
              return newErrorToken(number, startPosition, endPosition);
            }

          } else {
            return newFloatToken(number, startPosition, endPosition);
          }

        } // second item is none
        else {
          return newNumberToken(number, startPosition, endPosition);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // case number starts with decimal
    if (ch == '.') {
      try {
        String number = "";
        endPosition++;
        number += ch;
        ch = source.read();

        // digit followed by decimal
        if (Character.isDigit(ch)) {
          do {
            endPosition++;
            number += ch;
            ch = source.read();
          } while (Character.isDigit(ch));

          return newFloatToken(number, startPosition, endPosition);

        } else {
          return newErrorToken(number, startPosition, endPosition);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // Check for char, if it is followed by '
    if (ch == '\'') {

      try {
        String character = "";
        // Read the second item
        endPosition++;
        character += ch;
        ch = source.read();

        if (ch == '\'') // quote followed by qoute is error
        {
          return newErrorToken(character, startPosition, endPosition);
        } else {
          endPosition++;
          character += ch;
          ch = source.read();

          if (ch == '\'') // quote followed by a char followed by a qoute
          {
            endPosition++;
            character += ch;
            ch = source.read();
            return newCharToken(character, startPosition, endPosition);
          } else // quote followed by no closing qoute after 1 letter throws an error token
          {
            return newErrorToken(character, startPosition, endPosition);
          }
        }

      } catch (IOException e) {
        e.printStackTrace();
      }
    }

    // At this point the only tokens to check for are one or two
    // characters; we must also check for comments that begin with
    // 2 slashes
    String charOld = "" + ch;
    String op = charOld;
    Symbol sym;
    try {
      endPosition++;
      ch = source.read();
      op += ch;
      // check if valid 2 char operator; if it's not in the symbol
      // table then don't insert it since we really have a one char
      // token
      sym = Symbol.symbol(op, Tokens.BogusToken);
      if (sym == null) { // it must be a one char token
        return makeToken(charOld, startPosition, endPosition); // return error token
      }
      endPosition++;
      ch = source.read();
      return makeToken(op, startPosition, endPosition);
    } catch (Exception e) {
    }
    atEOF = true;
    if (startPosition == endPosition) {
      op = charOld;
    }
    return makeToken(op, startPosition, endPosition);
  }
コード例 #9
0
ファイル: Env.java プロジェクト: jiayuan6311/oldcode
  Env(ErrorMsg err) {
    errorMsg = err;
    fenv = new Table();
    tenv = new Table();
    Symbol i, s;

    i = Symbol.symbol("int");
    s = Symbol.symbol("string");
    // Types.RECORD trec=null;
    // Label lab;
    // BoolList formals;
    tenv.put(i, new Types.INT());
    tenv.put(s, new Types.STRING());

    fenv.put(Symbol.symbol("print"), new Types.VOID());
    fenv.put(Symbol.symbol("printi"), new Types.VOID());
    fenv.put(Symbol.symbol("flush"), new Types.VOID());
    fenv.put(Symbol.symbol("getchar"), new Types.STRING());
    fenv.put(Symbol.symbol("ord"), new Types.INT());
    fenv.put(Symbol.symbol("chr"), new Types.STRING());
    fenv.put(Symbol.symbol("size"), new Types.INT());
    fenv.put(Symbol.symbol("substring"), new Types.STRING());
    fenv.put(Symbol.symbol("concat"), new Types.STRING());
    fenv.put(Symbol.symbol("not"), new Types.INT());
    fenv.put(Symbol.symbol("exit"), new Types.VOID());
  }
コード例 #10
0
ファイル: SymbolTableTester.java プロジェクト: MrLenoir/tigc
 public static void main(String[] args) {
   Table<Integer> t = new Table<Integer>();
   t.put(Symbol.symbol("abc"), new Integer(1));
   t.put(Symbol.symbol("efg"), new Integer(2));
   System.out.println(t.get(Symbol.symbol("efg")));
   System.out.println(t.get(Symbol.symbol("")));
   t.put(Symbol.symbol("efg"), new Integer(3));
   System.out.println(t.get(Symbol.symbol("efg")));
   t.beginScope();
   System.out.println(t.get(Symbol.symbol("efg")));
   t.put(Symbol.symbol("efg"), new Integer(4));
   System.out.println(t.get(Symbol.symbol("efg")));
   t.put(Symbol.symbol("efg"), new Integer(5));
   System.out.println(t.get(Symbol.symbol("efg")));
   t.put(Symbol.symbol("new"), new Integer(6));
   System.out.println(t.get(Symbol.symbol("d")));
   System.out.println(t.get(Symbol.symbol("new")));
   t.endScope();
   System.out.println(t.get(Symbol.symbol("efg")));
   System.out.println(t.get(Symbol.symbol("new")));
 }