My first compiler symbol table
Symbol table
A symbol table is a data structure used by the compiler to save various information about the source program structure. This information is gradually collected and placed into the symbol table during the compiler analysis stage.
If we enter
{Int x; char y; {bool y; x; y ;}
Expected generation:
{X: int; y: bool;} x: int; y: char ;}
X of the inner block comes from the external.
Set a symbol table for each scope
package com.bigbear.main;public class Code { public static String content = "{int x;char y;{bool y; x; y; }x; y;}"; private static int index = 0; public static char read() { return content.charAt(index++); } public static boolean isEnd() { return index > content.length() - 1; }}
Package com. bigbear. symbols; import java. util. hashtable;/*** @ author winney * linkedSymbols link symbol table */public class Env {@ SuppressWarnings ("rawtypes") private Hashtable table; protected Env prev; @ SuppressWarnings ("rawtypes") public Env (Env p) {table = new Hashtable (); prev = p;} public void put (String s, Symbol sym) {table. put (s, sym);} public Symbol get (String s) {for (Env e = this; e! = Null; e = e. prev) {Symbol found = (Symbol) (e. table. get (s); if (found! = Null) return found;} return null ;}}
Use of symbol tables
Package com. bigbear. lexer; import java. io. IOException; import java. util. hashtable; import com. bigbear. enums. tagVM_INSTRUCTION; import com. bigbear. main. code; import com. bigbear. symbols. env; import com. bigbear. symbols. symbol;/*** @ author winney lexical analyzer **/public class Lexer {public int line = 1; private char peek = ''; @ SuppressWarnings (" rawtypes ") private Hashtable words = new Hashtable (); private Env t Op = null; private Env saved = null; private StringBuffer result = new StringBuffer (); @ SuppressWarnings ({"unchecked"}) private void reserve (Word t) {words. put (t. lexeme, t);} public Lexer () {reserve (new Word (Tag. TRUE, "true"); reserve (new Word (Tag. FALSE, "false"); reserve (new Word (Tag. TYPE, "int"); reserve (new Word (Tag. TYPE, "char"); reserve (new Word (Tag. TYPE, "bool");} @ SuppressWarning S ("unchecked") public Token scan () throws IOException {if (Code. isEnd () {return null ;}for (; peek = (char) Code. read () {if (peek = ''| peek = '\ t') continue; else if (peek =' \ n') line ++; else break;} if (Character. isDigit (peek) {int v = 0; do {v = 10 * v + Character. digit (peek, 10); peek = (char) Code. read ();} while (Character. isDigit (peek); return new Num (v);} if (Character. IsLetter (peek) {StringBuffer B = new StringBuffer (); do {B. append (peek); peek = (char) Code. read ();} while (Character. isLetterOrDigit (peek); String s = B. toString (); Word w = (Word) words. get (s); if (w! = Null) return w; w = new Word (Tag. ID, s); words. put (s, w); return w;} Token t = new Token (peek); peek = ''; return t;} public String Parse () throws IOException {Token token = scan (); Word w; Symbol s; while (token! = Null) {switch (token. tag) {case '{': saved = top; top = new Env (top); result. append ('{'); break; case '}': top = saved; result. append ('}'); break; case Tag. TYPE: w = (Word) token; s = new Symbol (w. lexeme); w = (Word) scan (); if (w. tag = Tag. ID) top. put (w. lexeme, s); break; case Tag. ID: w = (Word) token; s = top. get (w. lexeme); result. append (w. lexeme); result. append (':'); result. append (s. type); if (scan (). tag = ';') result. append (';'); break; default: break;} token = scan ();} return result. toString ();} public static void main (String [] args) {Lexer lx = new Lexer (); try {System. out. println (lx. parse ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
Here I see the pre-read technique.
Input: {int x; char y; {bool y; x; y ;}
Output: {x: int; y: bool;} x: int; y: char ;}