Compiler/lexical analyzer: Difference between revisions

Content added Content deleted
Line 1,675: Line 1,675:
03 error-line pic zzzz9 value 0.
03 error-line pic zzzz9 value 0.
03 error-col pic zzzzzz9 value 0.
03 error-col pic zzzzzz9 value 0.
03 error-message pic x(64) value spaces.
03 error-message pic x(68) value spaces.


01 scan-state pic x(16) value spaces.
01 scan-state pic x(16) value spaces.
Line 1,781: Line 1,781:
move 10 to out-integer
move 10 to out-integer
when out-value(1:2) = '\\'
when out-value(1:2) = '\\'
subtract 1 from ord('\') giving out-integer *> ' (highlighter issue on Rosetta)
subtract 1 from ord('\') giving out-integer *> ' (workaround a Rosetta Code highlighter problem)
when other
when other
string 'in lexer unknown escape sequence ' out-value(1:2)
string 'in lexer unknown escape sequence ' out-value(1:2)
Line 1,852: Line 1,852:


when other
when other
display input-record
string 'in lexer ' trim(scan-state)
string 'in lexer ' trim(scan-state)
' unknown character "' current-character '"'
' unknown character "' current-character '"'
Line 1,929: Line 1,930:
end-perform
end-perform
if scan-state = 'ambiguous'
if scan-state = 'ambiguous'
string 'in lexer unresolved ambiguous "' previous-character '" at end of line'
evaluate previous-character
when '/'
move 'Op_divide' to token
perform process-token

when '='
move 'Op_assign' to token
perform process-token

when '<'
move 'Op_less' to token
perform process-token

when '>'
move 'Op_greater' to token
perform process-token

when '!'
move 'Op_not' to token
perform process-token

when other
string 'in lexer unresolved ambiguous
"' previous-character '" at end of line'
into error-message
into error-message
perform report-error
perform report-error
end-evaluate
end-if
end-if
perform read-input-file
perform read-input-file
Line 2,009: Line 2,034:
end program lexer.</lang>
end program lexer.</lang>


{{out|case=TestCase4}}
{{out|case=AllSymbols}}
<pre>prompt$ ./lexer <testcases/TestCase4
<pre>prompt$ ./lexer <testcase3
2 1 Keyword_print
5 16 Keyword_print
2 6 LeftParen
5 40 Op_subtract
2 7 Integer 42
6 16 Keyword_putc
2 9 RightParen
6 40 Op_less
2 10 Semicolon
7 16 Keyword_if
3 1 Keyword_print
7 40 Op_greater
3 6 LeftParen
8 16 Keyword_else
8 40 Op_lessequal
3 7 String "\nHello World\nGood Bye\nok\n"
3 38 RightParen
9 16 Keyword_while
3 39 Semicolon
9 40 Op_greaterequal
4 1 Keyword_print
10 16 LeftBrace
4 6 LeftParen
10 40 Op_equal
11 16 RightBrace
4 7 String "Print a slash n - \\n.\n"
4 33 RightParen
11 40 Op_notequal
12 16 LeftParen
4 34 Semicolon
5 1 End_of_input</pre>
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>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==