当前位置:文档之家› 武汉理工大学编译原理实验报告

武汉理工大学编译原理实验报告


{
Token token = new Token(TokenTable.getTokenType(key),
key);
tokens.add(token);
}
else
{
Token
token
=
new
Token(TokenTable.getTokenType("identifier"), key);
tokens.add(token);
public static boolean containsKeyword(String keyword) {
return keywordTable.containsKey(keyword); }
public static boolean containsDelimiter(String delimiter) {
if (line.charAt(index) == '=')
{
String lessThanOrEqual = line.substring(i, index + 1);
Token
token
=
new
Token(TokenTable.getTokenType(lessThanOrEqual), lessThanOrEqual);
static {
identifierOrNumberTable.put("error", 0); identifierOrNumberTable.put("identifier", 1); identifierOrNumberTable.put("number", 2); keywordTable.put("char", 3); keywordTable.put("int", 4); keywordTable.put("float", 5); keywordTable.put("double", 6); keywordTable.put("void", 7); keywordTable.put("main", 8); keywordTable.put("if", 9); keywordTable.put("else", 10); keywordTable.put("for", 11); keywordTable.put("while", 12); keywordTable.put("do", 13); keywordTable.put("switch", 14); keywordTable.put("break", 15); keywordTable.put("continue", 16); keywordTable.put("return", 17); delimiterTable.put("{", 18); delimiterTable.put("}", 19); delimiterTable.put("[", 20); delimiterTable.put("]", 21); delimiterTable.put("(", 22); delimiterTable.put(")", 23); delimiterTable.put(",", 24);
{
index = i + 1;
while
(index
<
line.length()
&&
Character.isLetterOrDigit(line.charAt(index)))
{
index++;
}
String key = line.substring(i, index);
if (TokenTable.containsKeyword(key))
-2-
delimiterTable.put(";", 25); operatorTable.put("<", 26); operatorTable.put("<=", 27); operatorTable.put(">", 28); operatorTable.put(">=", 29); operatorTable.put("==", 30); operatorTable.put("=", 31); operatorTable.put("+", 32); operatorTable.put("-", 33); operatorTable.put("*", 34); operatorTable.put("/", 35); operatorTable.put("+=", 36); operatorTable.put("-=", 37); operatorTable.put("*=", 38); operatorTable.put("/=", 39); }
{
if (!error && Character.isLetter(line.charAt(index)))
{
error = true;
}
index++;
}
String tokenType = error ? "error" : "number";
Token
token
=
new
Token(TokenTable.getTokenType(tokenType), line.substring(i, index));
this.tokenType = tokenType; this.tokenValue = tokenValue; }
public int getTokenType() {
return tokenType; }
-1-
public String getTokenValue() {
return tokenValue; } }
二、实验设计(包括实验方案设计,实验手段的确定,实验步骤,实验过程等,用 硬件逻辑或者算法描述 )
Token.java: public class Token {
private int tokenType; // 单词种别码 private String tokenValue; // 单词值
public Token(int tokenType, String tokenValue) {
return delimiterTable.containsKey(delimiter); }
public static Integer getTokenType(String key)
{
return operatorTable.containsKey(key) ? operatorTable.get(key)
tokens.add(token);
-5-
i = index;
}
ห้องสมุดไป่ตู้
else
{
String lessThan = (line.substring(i, index));
Token
token
=
new
Token(TokenTable.getTokenType(lessThan), lessThan);
String line;
while ((line = in.readLine()) != null)
{
int index = 0; // 若单词是一个以上的字符组成,此下标就会起作用
for (int i = 0; i < line.length(); i++)
{
// 标识符或关键字
if (Character.isLetter(line.charAt(i)))
Token
token
=
new
Token(TokenTable.getTokenType(delimiter), delimiter);
tokens.add(token);
}
// 以下都是运算符..
// 运算符"<"或"<="
else if (line.charAt(i) == '<')
{
index = i + 1;
tokens.add(token);
i = index - 1;
}
// 界符
else
if
(TokenTable.containsDelimiter(String.valueOf(line.charAt(i))))
{
String delimiter = String.valueOf(line.charAt(i));
}
-4-
i = index - 1;
}
// 整型非负数值
else if (Character.isDigit(line.charAt(i)))
{
index = i + 1;
boolean error = false;
while
(index
<
line.length()
&&
Character.isLetterOrDigit(line.charAt(index)))
相关主题