Compiler/lexical analyzer: Difference between revisions
Content added Content deleted
m (Formatting) |
mNo edit summary |
||
Line 14: | Line 14: | ||
Create a lexical analyzer for the simple programming language specified below. The |
Create a lexical analyzer for the simple programming language specified below. The |
||
program should read input from a file and/or stdin, and write output to a file and/or |
program should read input from a file and/or stdin, and write output to a file and/or |
||
stdout. If the language being used has a lexer module/library/class, it would be great |
|||
stdout. |
|||
if two versions of the solution are provided: One without the lexer module, and one with. |
|||
;Specification |
;Specification |
||
Line 150: | Line 151: | ||
;Test Cases |
;Test Cases |
||
'''Test Case 1''' |
|||
<lang c> |
<lang c> |
||
/* |
/* |
||
Line 170: | Line 172: | ||
</b> |
</b> |
||
'''Test Case 2''' |
|||
<lang c> |
<lang c> |
||
/* |
/* |
||
Line 197: | Line 200: | ||
</b> |
</b> |
||
'''Test Case 3''' |
|||
<lang c> |
<lang c> |
||
/* |
/* |
||
Line 271: | Line 275: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Tested with gcc 4.81 and later, compiles warning free with -Wall -Wextra |
|||
<lang C> |
<lang C> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 493: | Line 498: | ||
} |
} |
||
</lang> |
</lang> |
||
Output from test case 3: |
|||
<b> |
|||
<pre> |
|||
line 5 col 15 Print |
|||
line 5 col 41 Sub |
|||
line 6 col 15 Putc |
|||
line 6 col 41 Lss |
|||
line 7 col 15 If |
|||
line 7 col 41 Gtr |
|||
line 8 col 15 While |
|||
line 8 col 41 Leq |
|||
line 9 col 15 Lbrace |
|||
line 9 col 41 Neq |
|||
line 10 col 15 Rbrace |
|||
line 10 col 41 And |
|||
line 11 col 15 Lparen |
|||
line 11 col 41 Semi |
|||
line 12 col 15 Rparen |
|||
line 12 col 41 Comma |
|||
line 13 col 15 Sub |
|||
line 13 col 41 Assign |
|||
line 14 col 15 Mul |
|||
line 14 col 41 Integer 42 |
|||
line 15 col 15 Div |
|||
line 15 col 41 String "String literal" |
|||
line 16 col 15 Add |
|||
line 16 col 41 Ident variable_name |
|||
line 17 col 26 Integer 10 |
|||
line 18 col 26 Integer 32 |
|||
line 19 col 1 EOI |
|||
</pre> |
|||
</b> |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
Tested with Euphoria 4.05. |
|||
<lang euphoria> |
<lang euphoria> |
||
include std/io.e |
include std/io.e |
||
Line 708: | Line 750: | ||
main(command_line()) |
main(command_line()) |
||
</lang> |
</lang> |
||
Output from test case 3: |
|||
<b> |
|||
<pre> |
|||
line 5 col 15 Print |
|||
line 5 col 41 Sub |
|||
line 6 col 15 Putc |
|||
line 6 col 41 Lss |
|||
line 7 col 15 If |
|||
line 7 col 41 Gtr |
|||
line 8 col 15 While |
|||
line 8 col 41 Leq |
|||
line 9 col 15 Lbrace |
|||
line 9 col 41 Neq |
|||
line 10 col 15 Rbrace |
|||
line 10 col 41 And |
|||
line 11 col 15 Lparen |
|||
line 11 col 41 Semi |
|||
line 12 col 15 Rparen |
|||
line 12 col 41 Comma |
|||
line 13 col 15 Sub |
|||
line 13 col 41 Assign |
|||
line 14 col 15 Mul |
|||
line 14 col 41 Integer 42 |
|||
line 15 col 15 Div |
|||
line 15 col 41 String "String literal" |
|||
line 16 col 15 Add |
|||
line 16 col 41 Ident variable_name |
|||
line 17 col 26 Integer 10 |
|||
line 18 col 26 Integer 32 |
|||
line 19 col 1 EOI |
|||
</pre> |
|||
</b> |
|||
=={{header|Flex}}== |
=={{header|Flex}}== |
||
Tested with Flex 2.5.4. |
|||
<lang C> |
<lang C> |
||
%{ |
%{ |
||
Line 876: | Line 954: | ||
} |
} |
||
</lang> |
</lang> |
||
Output from test case 3: |
|||
<b> |
|||
<pre> |
|||
line 5 col 15 Print |
|||
line 5 col 41 Sub |
|||
line 6 col 15 Putc |
|||
line 6 col 41 Lss |
|||
line 7 col 15 If |
|||
line 7 col 41 Gtr |
|||
line 8 col 15 While |
|||
line 8 col 41 Leq |
|||
line 9 col 15 Lbrace |
|||
line 9 col 41 Neq |
|||
line 10 col 15 Rbrace |
|||
line 10 col 41 And |
|||
line 11 col 15 Lparen |
|||
line 11 col 41 Semi |
|||
line 12 col 15 Rparen |
|||
line 12 col 41 Comma |
|||
line 13 col 15 Sub |
|||
line 13 col 41 Assign |
|||
line 14 col 15 Mul |
|||
line 14 col 41 Integer 42 |
|||
line 15 col 15 Div |
|||
line 15 col 41 String "String literal" |
|||
line 16 col 15 Add |
|||
line 16 col 41 Ident variable_name |
|||
line 17 col 26 Integer 10 |
|||
line 18 col 26 Integer 32 |
|||
line 18 col 29 EOI |
|||
</pre> |
|||
</b> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Tested with FreeBASIC 1.05 |
|||
<lang FreeBASIC> |
<lang FreeBASIC> |
||
enum Token_type |
enum Token_type |
||
Line 1,147: | Line 1,260: | ||
system |
system |
||
</lang> |
</lang> |
||
Output from test case 3: |
|||
<b> |
|||
<pre> |
|||
line 5 col 15 Print |
|||
line 5 col 41 Sub |
|||
line 6 col 15 Putc |
|||
line 6 col 41 Lss |
|||
line 7 col 15 If |
|||
line 7 col 41 Gtr |
|||
line 8 col 15 While |
|||
line 8 col 41 Leq |
|||
line 9 col 15 Lbrace |
|||
line 9 col 41 Neq |
|||
line 10 col 15 Rbrace |
|||
line 10 col 41 And |
|||
line 11 col 15 Lparen |
|||
line 11 col 41 Semi |
|||
line 12 col 15 Rparen |
|||
line 12 col 41 Comma |
|||
line 13 col 15 Sub |
|||
line 13 col 41 Assign |
|||
line 14 col 15 Mul |
|||
line 14 col 41 Integer 42 |
|||
line 15 col 15 Div |
|||
line 15 col 41 String "String literal" |
|||
line 16 col 15 Add |
|||
line 16 col 41 Ident variable_name |
|||
line 17 col 26 Integer 10 |
|||
line 18 col 26 Integer 32 |
|||
line 18 col 30 EOI |
|||
</pre> |
|||
</b> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Tested with Python 2.7 and 3.x |
|||
<lang Python> |
<lang Python> |
||
from __future__ import print_function |
from __future__ import print_function |
||
import sys |
import sys |
||
Line 1,314: | Line 1,463: | ||
break |
break |
||
</lang> |
</lang> |
||
Output from test case 3: |
|||
<b> |
|||
<pre> |
|||
line 5 col 15 Print |
|||
line 5 col 41 Sub |
|||
line 6 col 15 Putc |
|||
line 6 col 41 Lss |
|||
line 7 col 15 If |
|||
line 7 col 41 Gtr |
|||
line 8 col 15 While |
|||
line 8 col 41 Leq |
|||
line 9 col 15 Lbrace |
|||
line 9 col 41 Neq |
|||
line 10 col 15 Rbrace |
|||
line 10 col 41 And |
|||
line 11 col 15 Lparen |
|||
line 11 col 41 Semi |
|||
line 12 col 15 Rparen |
|||
line 12 col 41 Comma |
|||
line 13 col 15 Sub |
|||
line 13 col 41 Assign |
|||
line 14 col 15 Mul |
|||
line 14 col 41 Integer 42 |
|||
line 15 col 15 Div |
|||
line 15 col 41 String "String literal" |
|||
line 16 col 15 Add |
|||
line 16 col 41 Ident variable_name |
|||
line 17 col 26 Integer 10 |
|||
line 18 col 26 Integer 32 |
|||
line 19 col 1 EOI |
|||
</pre> |
|||
</b> |