{ open Lexing exception SyntaxError of string let next_line lexbuf = let pos = lexbuf.lex_curr_p in lexbuf.lex_curr_p <- { pos with pos_bol = lexbuf.lex_curr_pos; pos_lnum = pos.pos_lnum + 1 } (* type token = | LPAREN | RPAREN | PLUS | MINUS | INT of int *) } (* An integer is a non-empty sequence of digits between '0' and '9' *) let int = ['0'-'9']+ let white = [ ' ' '\t' ]+ let newline = '\n' | "\r\n" | '\r' rule read = parse | white { read lexbuf } | newline { next_line lexbuf; read lexbuf } | int { INT (int_of_string (Lexing.lexeme lexbuf)) } | '+' { PLUS } | '-' { MINUS } | '(' { LPAREN } | ')' { RPAREN } | eof { EOF } | _ { raise (SyntaxError ("Unexpected character: " ^ Lexing.lexeme lexbuf)) }