Jump to content

Compiler/lexical analyzer: Difference between revisions

Line 1,047:
#define RESERVED_WORD_HASHTAB_SIZE 9
 
#define token_elseTOKEN_ELSE 0
#define token_ifTOKEN_IF 1
#define token_printTOKEN_PRINT 2
#define token_putcTOKEN_PUTC 3
#define token_whileTOKEN_WHILE 4
#define token_multiplyTOKEN_MULTIPLY 5
#define token_divideTOKEN_DIVIDE 6
#define token_modTOKEN_MOD 7
#define token_addTOKEN_ADD 8
#define token_subtractTOKEN_SUBTRACT 9
#define token_negateTOKEN_NEGATE 10
#define token_lessTOKEN_LESS 11
#define token_lessequalTOKEN_LESSEQUAL 12
#define token_greaterTOKEN_GREATER 13
#define token_greaterequalTOKEN_GREATEREQUAL 14
#define token_equalTOKEN_EQUAL 15
#define token_notequalTOKEN_NOTEQUAL 16
#define token_notTOKEN_NOT 17
#define token_assignTOKEN_ASSIGN 18
#define token_andTOKEN_AND 19
#define token_orTOKEN_OR 20
#define token_LeftParenTOKEN_LEFTPAREN 21
#define token_RightParenTOKEN_RIGHTPAREN 22
#define token_LeftBraceTOKEN_LEFTBRACE 23
#define token_RightBraceTOKEN_RIGHTBRACE 24
#define token_SemicolonTOKEN_SEMICOLON 25
#define token_CommaTOKEN_COMMA 26
#define token_IdentifierTOKEN_IDENTIFIER 27
#define token_IntegerTOKEN_INTEGER 28
#define token_StringTOKEN_STRING 29
#define token_End_of_inputTOKEN_END_OF_INPUT 30
 
typedef token_t =
[i : int | token_elseTOKEN_ELSE <= i; i <= token_End_of_inputTOKEN_END_OF_INPUT]
int i
typedef tokentuple_t = (token_t, String, ullint, ullint)
Line 1,110:
column_no : ullint) : tokentuple_t =
if string_length s < 2 then
(token_IdentifierTOKEN_IDENTIFIER, s, line_no, column_no)
else
let
Line 1,120:
val token = toktab[hashval]
in
if token = token_IdentifierTOKEN_IDENTIFIER || s <> wordtab[hashval] then
(token_IdentifierTOKEN_IDENTIFIER, s, line_no, column_no)
else
(token, s, line_no, column_no)
Line 1,386:
val _ = check_they_are_all_digits lst
in
((token_IntegerTOKEN_INTEGER, s, ch.line_no, ch.column_no), inp)
end
 
Line 1,421:
val s = ichar2integer_literal (char2i '\n')
in
((token_IntegerTOKEN_INTEGER, s, ch.line_no, ch.column_no), inp)
end
else if (ch2.ichar) = char2i '\\' then
Line 1,427:
val s = ichar2integer_literal (char2i '\\')
in
((token_IntegerTOKEN_INTEGER, s, ch.line_no, ch.column_no), inp)
end
else
Line 1,437:
val s = ichar2integer_literal (ch1.ichar)
in
((token_IntegerTOKEN_INTEGER, s, ch.line_no, ch.column_no), inp)
end
end
Line 1,527:
val s = reverse_list_to_string lst
in
((token_StringTOKEN_STRING, s, ch.line_no, ch.column_no), inp)
end
 
Line 1,540:
in
case+ int2char0 (ch.ichar) of
| ',' => ((token_CommaTOKEN_COMMA, ",", ln, cn), inp)
| ';' => ((token_SemicolonTOKEN_SEMICOLON, ";", ln, cn), inp)
| '\(' => ((token_LeftParenTOKEN_LEFTPAREN, "(", ln, cn), inp)
| ')' => ((token_RightParenTOKEN_RIGHTPAREN, ")", ln, cn), inp)
| '\{' => ((token_LeftBraceTOKEN_LEFTBRACE, "{", ln, cn), inp)
| '}' => ((token_RightBraceTOKEN_RIGHTBRACE, "}", ln, cn), inp)
| '*' => ((token_multiplyTOKEN_MULTIPLY, "*", ln, cn), inp)
| '/' => ((token_divideTOKEN_DIVIDE, "/", ln, cn), inp)
| '%' => ((token_modTOKEN_MOD, "%", ln, cn), inp)
| '+' => ((token_addTOKEN_ADD, "+", ln, cn), inp)
| '-' => ((token_subtractTOKEN_SUBTRACT, "-", ln, cn), inp)
| '<' =>
let
Line 1,556:
in
if (ch1.ichar) = char2i '=' then
((token_lessequalTOKEN_LESSEQUAL, "<=", ln, cn), inp)
else
let
val inp = push_back_ch (ch1, inp)
in
((token_lessTOKEN_LESS, "<", ln, cn), inp)
end
end
Line 1,569:
in
if (ch1.ichar) = char2i '=' then
((token_greaterequalTOKEN_GREATEREQUAL, ">=", ln, cn), inp)
else
let
val inp = push_back_ch (ch1, inp)
in
((token_greaterTOKEN_GREATER, ">", ln, cn), inp)
end
end
Line 1,582:
in
if (ch1.ichar) = char2i '=' then
((token_equalTOKEN_EQUAL, "==", ln, cn), inp)
else
let
val inp = push_back_ch (ch1, inp)
in
((token_assignTOKEN_ASSIGN, "=", ln, cn), inp)
end
end
Line 1,595:
in
if (ch1.ichar) = char2i '=' then
((token_notequalTOKEN_NOTEQUAL, "!=", ln, cn), inp)
else
let
val inp = push_back_ch (ch1, inp)
in
((token_notTOKEN_NOT, "!", ln, cn), inp)
end
end
Line 1,608:
in
if (ch1.ichar) = char2i '&' then
((token_andTOKEN_AND, "&&", ln, cn), inp)
else
$raise unexpected_character (ch.line_no, ch.column_no,
Line 1,618:
in
if (ch1.ichar) = char2i '|' then
((token_orTOKEN_OR, "||", ln, cn), inp)
else
$raise unexpected_character (ch.line_no, ch.column_no,
Line 1,684:
begin
case+ toktup.0 of
| token_IdentifierTOKEN_IDENTIFIER => fprint! (outf, " ", str)
| token_IntegerTOKEN_INTEGER => fprint! (outf, " ", str)
| token_StringTOKEN_STRING => fprint! (outf, " ", str)
| _ => ()
end;
Line 1,708:
in
if (ch.ichar) < 0 then
print_token (outf, (token_End_of_inputTOKEN_END_OF_INPUT, "", ln, cn),
lookups)
else
Line 1,737:
var reserved_word_tokens =
@[token_t][RESERVED_WORD_HASHTAB_SIZE]
(token_ifTOKEN_IF, token_printTOKEN_PRINT, token_elseTOKEN_ELSE, token_IdentifierTOKEN_IDENTIFIER,
token_putcTOKEN_PUTC, token_IdentifierTOKEN_IDENTIFIER, token_IdentifierTOKEN_IDENTIFIER, token_whileTOKEN_WHILE,
token_IdentifierTOKEN_IDENTIFIER)
 
var token_names =
1,448

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.