Compiler/lexical analyzer: Difference between revisions

m
no edit summary
m (Formatting)
mNo edit summary
Line 14:
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
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
Line 150 ⟶ 151:
;Test Cases
 
'''Test Case 1'''
<lang c>
/*
Line 170 ⟶ 172:
</b>
 
'''Test Case 2'''
<lang c>
/*
Line 197 ⟶ 200:
</b>
 
'''Test Case 3'''
<lang c>
/*
Line 271 ⟶ 275:
 
=={{header|C}}==
Tested with gcc 4.81 and later, compiles warning free with -Wall -Wextra
<lang C>
#include <stdlib.h>
Line 493 ⟶ 498:
}
</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}}==
Tested with Euphoria 4.05.
<lang euphoria>
include std/io.e
Line 708 ⟶ 750:
main(command_line())
</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}}==
Tested with Flex 2.5.4.
<lang C>
%{
Line 876 ⟶ 954:
}
</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}}==
Tested with FreeBASIC 1.05
<lang FreeBASIC>
enum Token_type
Line 1,147 ⟶ 1,260:
system
</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}}==
Tested with Python 2.7 and 3.x
<lang Python>
 
from __future__ import print_function
import sys
Line 1,314 ⟶ 1,463:
break
</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>
155

edits