Compiler/Sample programs: Difference between revisions

Additional samples
(Additional samples)
(Additional samples)
Line 9:
<hr>
__TOC__
 
=={{header|Hello World}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<lang c>
/*
Hello world
*/
print("Hello, World!\n");
</lang>
 
 
| style="vertical-align:top" |
<b><pre>
4 1 Keyword_print
4 6 LeftParen
4 7 String "Hello, World!\n"
4 24 RightParen
4 25 Semicolon
5 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Sequence
;
Sequence
;
Prts
String "Hello, World!\n"
;
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Datasize: 0 Strings: 1
"Hello, World!\n"
0 push 0
5 prts
6 halt
</pre></b>
|}
 
;And the output is:
<pre>
>lex ..\hello.t | parse | gen | vm
Hello, World!
</pre>
 
=={{header|Phoenix number}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<lang c>
/*
Show Ident and Integers
*/
phoenix_number = 142857;
print(phoenix_number, "\n");
</lang>
 
 
| style="vertical-align:top" |
<b><pre>
4 1 Identifier phoenix_number
4 16 Op_assign
4 18 Integer 142857
4 24 Semicolon
5 1 Keyword_print
5 6 LeftParen
5 7 Identifier phoenix_number
5 21 Comma
5 23 String "\n"
5 27 RightParen
5 28 Semicolon
6 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Sequence
Sequence
;
Assign
Identifier phoenix_number
Integer 142857
Sequence
Sequence
;
Prti
Identifier phoenix_number
;
Prts
String "\n"
;
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Datasize: 1 Strings: 1
"\n"
0 push 142857
5 store [0]
10 fetch [0]
15 prti
16 push 0
21 prts
22 halt
</pre></b>
|}
 
;And the output is:
<pre>
>lex ..\phoenix_number.t | parse | gen | vm
142857
</pre>
 
 
=={{header|All symbols}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<lang c>
/*
All lexical tokens - not syntactically correct, but that will
have to wait until syntax analysis
*/
/* Print */ print /* Sub */ -
/* Putc */ putc /* Lss */ <
/* If */ if /* Gtr */ >
/* Else */ else /* Leq */ <=
/* While */ while /* Geq */ >=
/* Lbrace */ { /* Eq */ ==
/* Rbrace */ } /* Neq */ !=
/* Lparen */ ( /* And */ &&
/* Rparen */ ) /* Or */ ||
/* Uminus */ - /* Semi */ ;
/* Not */ ! /* Comma */ ,
/* Mul */ * /* Assign */ =
/* Div */ / /* Integer */ 42
/* Mod */ % /* String */ "String literal"
/* Add */ + /* Ident */ variable_name
/* character literal */ '\n'
/* character literal */ '\\'
/* character literal */ ' '
</lang>
 
 
| style="vertical-align:top" |
<b><pre>
5 16 Keyword_print
5 40 Op_subtract
6 16 Keyword_putc
6 40 Op_less
7 16 Keyword_if
7 40 Op_greater
8 16 Keyword_else
8 40 Op_lessequal
9 16 Keyword_while
9 40 Op_greaterequal
10 16 LeftBrace
10 40 Op_equal
11 16 RightBrace
11 40 Op_notequal
12 16 LeftParen
12 40 Op_and
13 16 RightParen
13 40 Op_or
14 16 Op_subtract
14 40 Semicolon
15 16 Op_not
15 40 Comma
16 16 Op_multiply
16 40 Op_assign
17 16 Op_divide
17 40 Integer 42
18 16 Op_mod
18 40 String "String literal"
19 16 Op_add
19 40 Identifier variable_name
20 26 Integer 10
21 26 Integer 92
22 26 Integer 32
23 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
(5, 40) Print: Expecting '(', found '-'
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
</pre></b>
|}
 
=={{header|Test case 4}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<lang c>
/*** test printing, embedded \n and comments with lots of '*' ***/
print(42);
print("\nHello World\nGood Bye\nok\n");
print("Print a slash n - \\n.\n");
</lang>
 
 
| style="vertical-align:top" |
<b><pre>
2 1 Keyword_print
2 6 LeftParen
2 7 Integer 42
2 9 RightParen
2 10 Semicolon
3 1 Keyword_print
3 6 LeftParen
3 7 String "\nHello World\nGood Bye\nok\n"
3 38 RightParen
3 39 Semicolon
4 1 Keyword_print
4 6 LeftParen
4 7 String "Print a slash n - \\n.\n"
4 33 RightParen
4 34 Semicolon
5 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Sequence
Sequence
Sequence
;
Sequence
;
Prti
Integer 42
;
Sequence
;
Prts
String "\nHello World\nGood Bye\nok\n"
;
Sequence
;
Prts
String "Print a slash n - \\n.\n"
;
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Datasize: 0 Strings: 2
"\nHello World\nGood Bye\nok\n"
"Print a slash n - \\n.\n"
0 push 42
5 prti
6 push 0
11 prts
12 push 1
17 prts
18 halt
</pre></b>
|}
 
;And the output is:
<pre>
>lex ..\testcase4.t | parse | gen | vm
42
Hello World
Good Bye
ok
Print a slash n - \n.
</pre>
 
=={{header|Count}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<lang c>
count = 1;
while (count < 10) {
print("count is: ", count, "\n");
count = count + 1;
}
</lang>
 
 
| style="vertical-align:top" |
<b><pre>
1 1 Identifier count
1 7 Op_assign
1 9 Integer 1
1 10 Semicolon
2 1 Keyword_while
2 7 LeftParen
2 8 Identifier count
2 14 Op_less
2 16 Integer 10
2 18 RightParen
2 20 LeftBrace
3 5 Keyword_print
3 10 LeftParen
3 11 String "count is: "
3 23 Comma
3 25 Identifier count
3 30 Comma
3 32 String "\n"
3 36 RightParen
3 37 Semicolon
4 5 Identifier count
4 11 Op_assign
4 13 Identifier count
4 19 Op_add
4 21 Integer 1
4 22 Semicolon
5 1 RightBrace
6 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Sequence
Sequence
;
Assign
Identifier count
Integer 1
While
Less
Identifier count
Integer 10
Sequence
Sequence
;
Sequence
Sequence
Sequence
;
Prts
String "count is: "
;
Prti
Identifier count
;
Prts
String "\n"
;
Assign
Identifier count
Add
Identifier count
Integer 1
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Datasize: 1 Strings: 2
"count is: "
"\n"
0 push 1
5 store [0]
10 fetch [0]
15 push 10
20 lt
21 jz (43) 65
26 push 0
31 prts
32 fetch [0]
37 prti
38 push 1
43 prts
44 fetch [0]
49 push 1
54 add
55 store [0]
60 jmp (-51) 10
65 halt
</pre></b>
|}
 
;And the output is:
<pre>
>lex ..\count.t | parse | gen | vm
count is: 1
count is: 2
count is: 3
count is: 4
count is: 5
count is: 6
count is: 7
count is: 8
count is: 9
</pre>
 
=={{header|Negative tests}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<lang c>
a = (-1 * ((-1 * (5 * 15)) / 10));
print(a, "\n");
b = -a;
print(b, "\n");
print(-b, "\n");
print(-(1), "\n");
</lang>
 
 
| style="vertical-align:top" |
<b><pre>
1 1 Identifier a
1 3 Op_assign
1 5 LeftParen
1 6 Op_subtract
1 7 Integer 1
1 9 Op_multiply
1 11 LeftParen
1 12 LeftParen
1 13 Op_subtract
1 14 Integer 1
1 16 Op_multiply
1 18 LeftParen
1 19 Integer 5
1 21 Op_multiply
1 23 Integer 15
1 25 RightParen
1 26 RightParen
1 28 Op_divide
1 30 Integer 10
1 32 RightParen
1 33 RightParen
1 34 Semicolon
2 1 Keyword_print
2 6 LeftParen
2 7 Identifier a
2 8 Comma
2 10 String "\n"
2 14 RightParen
2 15 Semicolon
3 1 Identifier b
3 3 Op_assign
3 5 Op_subtract
3 6 Identifier a
3 7 Semicolon
4 1 Keyword_print
4 6 LeftParen
4 7 Identifier b
4 8 Comma
4 10 String "\n"
4 14 RightParen
4 15 Semicolon
5 1 Keyword_print
5 6 LeftParen
5 7 Op_subtract
5 8 Identifier b
5 9 Comma
5 11 String "\n"
5 15 RightParen
5 16 Semicolon
6 1 Keyword_print
6 6 LeftParen
6 7 Op_subtract
6 8 LeftParen
6 9 Integer 1
6 10 RightParen
6 11 Comma
6 13 String "\n"
6 17 RightParen
6 18 Semicolon
7 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Sequence
Sequence
Sequence
Sequence
Sequence
Sequence
;
Assign
Identifier a
Multiply
Negate
Integer 1
;
Divide
Multiply
Negate
Integer 1
;
Multiply
Integer 5
Integer 15
Integer 10
Sequence
Sequence
;
Prti
Identifier a
;
Prts
String "\n"
;
Assign
Identifier b
Negate
Identifier a
;
Sequence
Sequence
;
Prti
Identifier b
;
Prts
String "\n"
;
Sequence
Sequence
;
Prti
Negate
Identifier b
;
;
Prts
String "\n"
;
Sequence
Sequence
;
Prti
Negate
Integer 1
;
;
Prts
String "\n"
;
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Datasize: 2 Strings: 1
"\n"
0 push 1
5 neg
6 push 1
11 neg
12 push 5
17 push 15
22 mul
23 mul
24 push 10
29 div
30 mul
31 store [0]
36 fetch [0]
41 prti
42 push 0
47 prts
48 fetch [0]
53 neg
54 store [1]
59 fetch [1]
64 prti
65 push 0
70 prts
71 fetch [1]
76 neg
77 prti
78 push 0
83 prts
84 push 1
89 neg
90 prti
91 push 0
96 prts
97 halt
</pre></b>
|}
 
;And the output is:
<pre>
>lex ..\negative.t | parse | gen | vm
7
-7
7
-1
</pre>
 
=={{header|Deep}}==
 
{| class="wikitable"
|-
! Input to lex
! Output from lex, input to parse
! Output from parse
! Output from gen, input to VM
|-
| style="vertical-align:top" |
<lang c>
print(---------------------------------+++5, "\n");
print(((((((((3 + 2) * ((((((2))))))))))))), "\n");
 
if (1) { if (1) { if (1) { if (1) { if (1) { print(15, "\n"); } } } } }
</lang>
 
 
| style="vertical-align:top" |
<b><pre>
1 1 Keyword_print
1 6 LeftParen
1 7 Op_subtract
1 8 Op_subtract
1 9 Op_subtract
1 10 Op_subtract
1 11 Op_subtract
1 12 Op_subtract
1 13 Op_subtract
1 14 Op_subtract
1 15 Op_subtract
1 16 Op_subtract
1 17 Op_subtract
1 18 Op_subtract
1 19 Op_subtract
1 20 Op_subtract
1 21 Op_subtract
1 22 Op_subtract
1 23 Op_subtract
1 24 Op_subtract
1 25 Op_subtract
1 26 Op_subtract
1 27 Op_subtract
1 28 Op_subtract
1 29 Op_subtract
1 30 Op_subtract
1 31 Op_subtract
1 32 Op_subtract
1 33 Op_subtract
1 34 Op_subtract
1 35 Op_subtract
1 36 Op_subtract
1 37 Op_subtract
1 38 Op_subtract
1 39 Op_subtract
1 40 Op_add
1 41 Op_add
1 42 Op_add
1 43 Integer 5
1 44 Comma
1 46 String "\n"
1 50 RightParen
1 51 Semicolon
2 1 Keyword_print
2 6 LeftParen
2 7 LeftParen
2 8 LeftParen
2 9 LeftParen
2 10 LeftParen
2 11 LeftParen
2 12 LeftParen
2 13 LeftParen
2 14 LeftParen
2 15 Integer 3
2 17 Op_add
2 19 Integer 2
2 20 RightParen
2 22 Op_multiply
2 24 LeftParen
2 25 LeftParen
2 26 LeftParen
2 27 LeftParen
2 28 LeftParen
2 29 LeftParen
2 30 Integer 2
2 31 RightParen
2 32 RightParen
2 33 RightParen
2 34 RightParen
2 35 RightParen
2 36 RightParen
2 37 RightParen
2 38 RightParen
2 39 RightParen
2 40 RightParen
2 41 RightParen
2 42 RightParen
2 43 RightParen
2 44 Comma
2 46 String "\n"
2 50 RightParen
2 51 Semicolon
4 1 Keyword_if
4 4 LeftParen
4 5 Integer 1
4 6 RightParen
4 8 LeftBrace
4 10 Keyword_if
4 13 LeftParen
4 14 Integer 1
4 15 RightParen
4 17 LeftBrace
4 19 Keyword_if
4 22 LeftParen
4 23 Integer 1
4 24 RightParen
4 26 LeftBrace
4 28 Keyword_if
4 31 LeftParen
4 32 Integer 1
4 33 RightParen
4 35 LeftBrace
4 37 Keyword_if
4 40 LeftParen
4 41 Integer 1
4 42 RightParen
4 44 LeftBrace
4 46 Keyword_print
4 51 LeftParen
4 52 Integer 15
4 54 Comma
4 56 String "\n"
4 60 RightParen
4 61 Semicolon
4 63 RightBrace
4 65 RightBrace
4 67 RightBrace
4 69 RightBrace
4 71 RightBrace
5 1 End_of_input
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Sequence
Sequence
Sequence
;
Sequence
Sequence
;
Prti
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Negate
Integer 5
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
;
Prts
String "\n"
;
Sequence
Sequence
;
Prti
Multiply
Add
Integer 3
Integer 2
Integer 2
;
Prts
String "\n"
;
If
Integer 1
If
Sequence
;
If
Integer 1
If
Sequence
;
If
Integer 1
If
Sequence
;
If
Integer 1
If
Sequence
;
If
Integer 1
If
Sequence
;
Sequence
Sequence
;
Prti
Integer 15
;
Prts
String "\n"
;
;
;
;
;
;
</pre></b>
 
 
| style="vertical-align:top" |
<b><pre>
Datasize: 0 Strings: 1
"\n"
0 push 5
5 neg
6 neg
7 neg
8 neg
9 neg
10 neg
11 neg
12 neg
13 neg
14 neg
15 neg
16 neg
17 neg
18 neg
19 neg
20 neg
21 neg
22 neg
23 neg
24 neg
25 neg
26 neg
27 neg
28 neg
29 neg
30 neg
31 neg
32 neg
33 neg
34 neg
35 neg
36 neg
37 neg
38 prti
39 push 0
44 prts
45 push 3
50 push 2
55 add
56 push 2
61 mul
62 prti
63 push 0
68 prts
69 push 1
74 jz (56) 131
79 push 1
84 jz (46) 131
89 push 1
94 jz (36) 131
99 push 1
104 jz (26) 131
109 push 1
114 jz (16) 131
119 push 15
124 prti
125 push 0
130 prts
131 halt
</pre></b>
|}
 
;And the output is:
<pre>
>lex ..\deep.t | parse | gen | vm
-5
10
15
</pre>
 
 
=={{header|GCD}}==
155

edits