Compiler/lexical analyzer: Difference between revisions
→{{header|ATS}}
Line 1,023:
One interesting feature of this implementation is my liberal use of a pushback buffer for input characters. This kept the code modular and easier to write.
(One point of note: the C "EOF" pseudo-character is detected in the following code by looking for a negative number. That EOF has to be negative and the other characters non-negative is implied by the ISO C standard.)
<lang ATS>(********************************************************************)
Line 1,539 ⟶ 1,541:
val cn = ch.column_no
in
| ';' => ((TOKEN_SEMICOLON, ";", ln, cn), inp)▼
| '
| '
| '
| '
| '
| '
| '
| '
| '
let▼
| '-'
▲ let
val (
((TOKEN_LESS, "<", ln, cn), inp)
| '>' =>▼
val (
((TOKEN_GREATER, ">", ln, cn), inp)
| '=' =>▼
let▼
val (
((TOKEN_ASSIGN, "=", ln, cn), inp)
let▼
val (
((TOKEN_NOT, "!", ln, cn), inp)
let▼
val (
((TOKEN_AND, "&&", ln, cn),
ch.ichar)▼
▲ let
let▼
val (
((TOKEN_OR, "||", ln, cn),
$raise unexpected_character (ch.line_no, ch.column_no,
ch.ichar)
▲ let
val inp = push_back_ch (ch, inp)▼
▲ val inp = push_back_ch (ch, inp)
scan_string_literal inp
▲ let
▲ end
val inp = push_back_ch (ch, inp)▼
▲ val inp = push_back_ch (ch, inp)
scan_character_literal inp
▲ let
▲ end
val inp = push_back_ch (ch, inp)▼
let▼
scan_integer_literal (inp, lookups)▼
▲ val inp = push_back_ch (ch, inp)
▲ | _ when is_ident_start (ch.ichar) =>
▲ scan_integer_literal (inp, lookups)
▲ let
▲ end
val inp = push_back_ch (ch, inp)▼
| _ when is_ident_start (ch.ichar) =>
scan_identifier_or_reserved_word (inp, lookups)▼
▲ val inp = push_back_ch (ch, inp)
▲ | _ => $raise unexpected_character (ch.line_no, ch.column_no,
▲ scan_identifier_or_reserved_word (inp, lookups)
▲ ch.ichar)
▲ end
| _ => $raise unexpected_character (ch.line_no, ch.column_no,
ch.ichar)
end
Line 1,702 ⟶ 1,707:
lookups : !lookups_vt) : void =
let
val (toktup, inp) =
in
if toktup.0
loop
▲ else
▲ let
▲ in
▲ end
end
in
|