Compiler/lexical analyzer: Difference between revisions

Content added Content deleted
Line 9,749: Line 9,749:
</pre>
</pre>
=={{header|kotlin}}==
=={{header|kotlin}}==
{{trans|Java}}
<lang kotlin>// Input: command line argument of file to process or console input. A two or
<lang kotlin>// Input: command line argument of file to process or console input. A two or
// three character console input of digits followed by a new line will be
// three character console input of digits followed by a new line will be
Line 10,101: Line 10,102:
Lexer(
Lexer(
"""/*
"""/*
All lexical tokens - not syntactically correct, but that will
All lexical tokens - not syntactically correct, but that will
have to wait until syntax analysis
have to wait until syntax analysis
*/
*/
/* Print */ print /* Sub */ -
/* Print */ print /* Sub */ -
/* Putc */ putc /* Lss */ <
/* Putc */ putc /* Lss */ <
/* If */ if /* Gtr */ >
/* If */ if /* Gtr */ >
/* Else */ else /* Leq */ <=
/* Else */ else /* Leq */ <=
/* While */ while /* Geq */ >=
/* While */ while /* Geq */ >=
/* Lbrace */ { /* Eq */ ==
/* Lbrace */ { /* Eq */ ==
/* Rbrace */ } /* Neq */ !=
/* Rbrace */ } /* Neq */ !=
/* Lparen */ ( /* And */ &&
/* Lparen */ ( /* And */ &&
/* Rparen */ ) /* Or */ ||
/* Rparen */ ) /* Or */ ||
/* Uminus */ - /* Semi */ ;
/* Uminus */ - /* Semi */ ;
/* Not */ ! /* Comma */ ,
/* Not */ ! /* Comma */ ,
/* Mul */ * /* Assign */ =
/* Mul */ * /* Assign */ =
/* Div */ / /* Integer */ 42
/* Div */ / /* Integer */ 42
/* Mod */ % /* String */ "String literal"
/* Mod */ % /* String */ "String literal"
/* Add */ + /* Ident */ variable_name
/* Add */ + /* Ident */ variable_name
/* character literal */ '\n'
/* character literal */ '\n'
/* character literal */ '\\'
/* character literal */ '\\'
/* character literal */ ' '""").printTokens()
/* character literal */ ' '""").printTokens()
} // symbols
} // symbols


Line 10,138: Line 10,139:
"""count = 1;
"""count = 1;
while (count < 10) {
while (count < 10) {
print("count is: ", count, "\n");
print("count is: ", count, "\n");
count = count + 1;
count = count + 1;
}""").printTokens()
}""").printTokens()
} // count
} // count
Line 10,149: Line 10,150:
i = 1;
i = 1;
while (i * i <= 100) {
while (i * i <= 100) {
print("door ", i * i, " is open\n");
print("door ", i * i, " is open\n");
i = i + 1;
i = i + 1;
}""").printTokens()
}""").printTokens()
} // doors
} // doors
Line 10,183: Line 10,184:
 
 
while (b != 0) {
while (b != 0) {
new_a = b;
new_a = b;
b = a % b;
b = a % b;
a = new_a;
a = new_a;
}
}
print(a);""").printTokens()
print(a);""").printTokens()
Line 10,199: Line 10,200:
i = 1;
i = 1;
while (i <= n) {
while (i <= n) {
result = result * i;
result = result * i;
i = i + 1;
i = i + 1;
}
}
print(result);""").printTokens()
print(result);""").printTokens()
Line 10,215: Line 10,216:
b = 1;
b = 1;
while (i < n) {
while (i < n) {
w = a + b;
w = a + b;
a = b;
a = b;
b = w;
b = w;
i = i + 1;
i = i + 1;
}
}
print(w, "\n");""").printTokens()
print(w, "\n");""").printTokens()
Line 10,229: Line 10,230:
i = 1;
i = 1;
while (i <= 100) {
while (i <= 100) {
if (!(i % 15))
if (!(i % 15))
print("FizzBuzz");
print("FizzBuzz");
else if (!(i % 3))
else if (!(i % 3))
print("Fizz");
print("Fizz");
else if (!(i % 5))
else if (!(i % 5))
print("Buzz");
print("Buzz");
else
else
print(i);
print(i);
 
 
print("\n");
print("\n");
i = i + 1;
i = i + 1;
}""").printTokens()
}""").printTokens()
} // fizzbuzz
} // fizzbuzz
Line 10,249: Line 10,250:
bottles = 99;
bottles = 99;
while (bottles > 0) {
while (bottles > 0) {
print(bottles, " bottles of beer on the wall\n");
print(bottles, " bottles of beer on the wall\n");
print(bottles, " bottles of beer\n");
print(bottles, " bottles of beer\n");
print("Take one down, pass it around\n");
print("Take one down, pass it around\n");
bottles = bottles - 1;
bottles = bottles - 1;
print(bottles, " bottles of beer on the wall\n\n");
print(bottles, " bottles of beer on the wall\n\n");
}""").printTokens()
}""").printTokens()
} // bottles
} // bottles
Line 10,423: Line 10,424:
} // try
} // try
} // main</lang>
} // main</lang>
{{out|case=test case 3}}
{{out|case=test case 3: All Symbols}}
<b>
<b>
<pre>
<pre>