Skip to content

Commit d14855c

Browse files
committed
Merge branch 'main' of github.com:OrcaLinux/Design-of-Compilers
2 parents 04bda59 + c53d6b7 commit d14855c

File tree

9 files changed

+322
-208
lines changed

9 files changed

+322
-208
lines changed

scanner/build/scanner_test

-724 KB
Binary file not shown.

scanner/examples/example1.txt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
1-
# Example 1: TINY language code sample
1+
{Example 1, test all known tokens and
2+
multiple spaces and the end}
3+
x := 5;
4+
if x<10 then() {simple if
5+
condition }
6+
x := 7;
7+
END {End if}
8+
9+
if (x=7) write x;
10+
11+
read d;
12+
13+
repeat50040;
14+
repeat 50 until 66
15+
y = d+9/5
16+
x=f +y
17+
newVar
18+
19+
20+

scanner/examples/example2.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
# Example 2: TINY language code sample
1+
{Exampl 2, genral test}
2+
x := 5; { this is a comment }
3+
write x;
4+
read y;
5+
y : /x; {: is an unknown token}
6+

scanner/examples/example3.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{Example 3, test IDENTIFIERs with underscore
2+
and more complex lines}
3+
abc := 123;
4+
z := (a + b) * (c / d);
5+
{ This should be ignored }
6+
7+
{an IDENTIFIER with underscore
8+
underscore is an unknown token
9+
}
10+
aa_bb = 343 aa55

scanner/examples/example4.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{Exmaple 4, nested comments and some invalid tokens}
2+
{ Outer comment { Inner comment } }
3+
x := 5;
4+
if x = 10 then y := y + 1;
5+
x : @$ # ! _ & *^~||| ;
6+
7+
8+

scanner/examples/example5.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{Example 5, test empty file}

scanner/include/app.hpp

Lines changed: 214 additions & 205 deletions
Large diffs are not rendered by default.

scanner/output/output.txt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
# Output file for scanner results
1+
x, IDENTIFIER
2+
:=, ASSIGN
3+
5, NUMBER
4+
;, SEMICOLON
5+
if, IF
6+
x, IDENTIFIER
7+
=, EQUAL
8+
10, NUMBER
9+
then, THEN
10+
y, IDENTIFIER
11+
:=, ASSIGN
12+
y, IDENTIFIER
13+
+, PLUS
14+
1, NUMBER
15+
;, SEMICOLON
16+
x, IDENTIFIER
17+
:, UNKNOWN
18+
@, UNKNOWN
19+
$, UNKNOWN
20+
#, UNKNOWN
21+
!, UNKNOWN
22+
_, UNKNOWN
23+
&, UNKNOWN
24+
*, MULT
25+
^, UNKNOWN
26+
~, UNKNOWN
27+
|, UNKNOWN
28+
|, UNKNOWN
29+
|, UNKNOWN
30+
;, SEMICOLON

scanner/src/app.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ void App::processTokens(std::string &inputFileContent) {
9595
tokenStreamBuilder.build();
9696
std::vector<Token> tokens = tokenStreamBuilder.getTokens();
9797

98+
// if no tokens are generated, throw an exception
99+
if (tokens.empty()) {
100+
throw std::runtime_error("No tokens generated. Please check the input file.");
101+
}
102+
103+
// any unknown tokens printed as errors to the console
104+
catchUnkonwnTokens(tokens);
105+
98106
// write tokens to output file if specified
99107
if (hasOutputFile) {
100108
// set color to orange
@@ -112,6 +120,31 @@ void App::processTokens(std::string &inputFileContent) {
112120
}
113121
}
114122

123+
void App::catchUnkonwnTokens(std::vector<TINY::SCANNER::Token> &tokens) {
124+
bool hasUnknownTokens = false;
125+
for (const Token &token : tokens) {
126+
if (token.getType() == TokenType::UNKNOWN) {
127+
// for the first unknown token, set color to red
128+
if (!hasUnknownTokens) {
129+
hasUnknownTokens = true;
130+
// set color to red
131+
std::cout << "\033[1;31m";
132+
std::cerr << "The input contains unexpected tokens, please check the input file."
133+
<< std::endl
134+
<< "List of unexpected tokens:"
135+
<< std::endl;
136+
}
137+
138+
std::cerr << "-\tError: unexpected token '" << token.getValue()
139+
<< "' at line " << token.getLine()
140+
<< ", column " << token.getColumn()
141+
<< std::endl;
142+
}
143+
}
144+
// reset color
145+
std::cout << "\033[0m";
146+
}
147+
115148
void App::printTokens(const std::vector<Token> &tokens, bool includePosition) {
116149
// set color to Green
117150
std::cout << "\033[1;32m";

0 commit comments

Comments
 (0)