詞法分析器

2021-10-10 04:00:00 字數 3181 閱讀 7069

詞法分析器用來給語法分析器提供token,每個token都有乙個屬性tokentype用來指明其類別。

public enum tokentype

public class token

public token(tokentype type)

=> this.type = type;

public override string tostring()

=> $"";

}

integer token是**輸入的整數值,char token是**輸入的字元值。

public class integer : token

public integer(int value) : base(tokentype.integer)

=> this.value = value;

public override string tostring()

=> $"integer\t";;

}public class char : token

public char(char value) : base(tokentype.char)

=> this.value = value;

public override string tostring()

=> $"char\t";

}

word token是運算子、關鍵字和變數名,type token是基本型別bool、int、char。

public class type : word

// width is used for storage allocation

public type(tokentype type, string lexeme, int width) : base(type, lexeme)

=> this.width = width;

}public class word : token

public word(tokentype type, string lexeme) : base(type)

=> this.lexeme = lexeme;

public override string tostring()

=> $"\t";

}

public class lexer

private stringreader reader;

private idictionaryreserveds = new dictionary();

public lexer()

private void reserve(word word) => reserveds.add(word.lexeme, word);

public void loadcode(string code)

public token scan()

else if (curchar == '\n') ++linenumber;

else break;

}switch (curchar)

':case ';':

return new word(tokentype.delimiter, curchar.tostring());

case '\'':

var buffer = new stringbuilder();

while (true)

return new char(buffer.tostring()[0]);

}if (char.isdigit(curchar))

return new integer(int.parse(buffer.tostring()));

}else if (char.isletter(curchar))

var str = buffer.tostring();

if (reserveds.containskey(str))

return reserveds[str];

var id = new word(tokentype.id, str);

reserveds.add(str, id);

return id;

}else

return null;

}private bool readchar(char ch)

}

[testmethod]

public void test1()

else

while(true)

";var lexer = new lexer();

lexer.loadcode(code);

while (true)

\t");

}}輸出如下:

line1 primitive char

line1 id ch

line1 assign =

line1 char a

line1 delimiter ;

line2 primitive bool

line2 id test

line2 assign =

line2 false false

line2 delimiter ;

line3 primitive int

line3 id a

line3 assign =

line3 integer 1

line3 delimiter ;

line4 primitive int

line4 id b

line4 assign =

line4 integer 20

line4 delimiter ;

line5 if if

line5 delimiter (

line5 id a

line5 lt <

line5 id b

line5 delimiter )

line6 delimiter

line9 else else

line10 delimiter

line13 while while

line13 delimiter (

line13 true true

line13 delimiter )

line14 delimiter

詞法分析器

這是我自己的第一篇部落格,就分享一下最近才做完的編譯原理實驗,詞法分析器。本次實驗中我用mysql資料庫儲存自動機狀態表,這樣做的目的只是為了在後續的課設中可以繼續使用現在的 這一段 並不是太完善,發出來只是為了太完善。裡面還有很多問題,比如對字元和字串的識別,不知道為什麼資料庫無法將 和 轉換到我...

詞法分析器

include using namespace std const int maxn 1e3 10 int n 輸入文字的行數 char buffer maxn maxn 緩衝區 int len maxn 輸入文字每行的列數 struct out 輸出格式 out string a,int b re...

詞法分析器

簡單的詞法分析器 標題 南山荒野客 20 05 2019 詞法分析器 編譯原理 c 語言 include include includechar prog 80 token 6 char ch int syn,p,m 0,n,row,sum 0 int biaoji 0 char rwtab 18 ...