Skip to content
Snippets Groups Projects
Commit 0fe34220 authored by Claire Girot's avatar Claire Girot
Browse files

lexer_v1

parent 1567192a
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,21 @@
open Parser
open Ast
type token =
| EOF | PUSH | POP | SWAP | ADD | SUB | MUL | DIV | REM | INT of int
let print_token = function
| EOF -> print_string "EOF"
| INT i -> print_int i
| PUSH -> print_string "PUSH"
| SWAP -> print_string "SWAP"
| POP -> print_string "POP"
| ADD -> print_string "ADD"
| SUB -> print_string "SUB"
| MUL -> print_string "MUL"
| DIV -> print_string "DIV"
| REM -> print_string "REM"
let mk_int nb =
try INT (int_of_string nb)
with Failure _ -> failwith (Printf.sprintf "Illegal integer '%s': " nb)
......@@ -24,13 +39,36 @@ rule token = parse
(* integers *)
| digit+ as nb { mk_int nb }
(* commands *)
| "push" {Push}
| "pop" {Pop}
| "swap" {Swap}
| "add" {Add}
| "sub" {Sub}
| "mul" {Mul}
| "div" {Div}
| "rem" {Rem}
| "push" {PUSH}
| "pop" {POP}
| "swap" {SWAP}
| "add" {ADD}
| "sub" {SUB}
| "mul" {MUL}
| "div" {DIV}
| "rem" {REM}
(* illegal characters *)
| _ as c { failwith (Printf.sprintf "Illegal character '%c': " c) }
{
let rec examine_all lexbuf =
let result = token lexbuf in
print_token result;
print_string " ";
match result with
| EOF -> ()
| _ -> examine_all lexbuf
let compile file =
print_string ("File "^file^" is being treated!\n");
try
let input_file = open_in file in
let lexbuf = Lexing.from_channel input_file in
examine_all lexbuf;
print_newline ();
close_in (input_file)
with Sys_error _ ->
print_endline ("Can't find file '" ^ file ^ "'")
let _ = Arg.parse [] compile ""
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment