: gettoken(1) module P = Printf exception End_of_system (* *) let _ISTREAM = ref stdin let ch = ref ( ) let read () = (let c =!ch in ch := inp

Similar documents
Parametric Polymorphism

jssst-ocaml.mgp

ML Edinburgh LCF ML Curry-Howard ML ( ) ( ) ( ) ( ) 1

Jacques Garrigue

# let rec sigma (f, n) = # if n = 0 then 0 else f n + sigma (f, n-1);; val sigma : (int -> int) * int -> int = <fun> sigma f n ( : * -> * ) sqsum cbsu

プログラミング言語 8 字句解析器(lexer)と構文解析器(parser)

( ) ( ) lex LL(1) LL(1)

Objective Caml 3.12 Jacques Garrigue ( ) with Alain Frisch (Lexifi), OCaml developper team (INRIA)

r02.dvi

ohp02.dvi

1 (2 * 3) 1 2 * 3 Preorder In order Post order 1 * 1 * Breadth-first Depth-first * * 3 Preorder: 1 * 2 3 In order: 1 2 * 3 Post orde

compiler-text.dvi

ML λ λ 1 λ 1.1 λ λ λ e (λ ) ::= x ( ) λx.e (λ ) e 1 e 2 ( ) ML λx.e Objective Caml fun x -> e x e let 1

r9.dvi

# let st1 = {name = "Taro Yamada"; id = };; val st1 : student = {name="taro Yamada"; id=123456} { 1 = 1 ;...; n = n } # let string_of_student {n

untitled

() / (front end) (back end) (phase) (pass) 1 2

I. Backus-Naur BNF : N N 0 N N N N N N 0, 1 BNF N N 0 11 (parse tree) 11 (1) (2) (3) (4) II. 0(0 101)* (

ohp07.dvi

listings-ext

:30 12:00 I. I VI II. III. IV. a d V. VI

I. Backus-Naur BNF S + S S * S S x S +, *, x BNF S (parse tree) : * x + x x S * S x + S S S x x (1) * x x * x (2) * + x x x (3) + x * x + x x (4) * *

org/ghc/ Windows Linux RPM 3.2 GHCi GHC gcc javac ghc GHCi(ghci) GHCi Prelude> GHCi :load file :l file :also file :a file :reload :r :type expr :t exp

Microsoft PowerPoint - Compiler06note.pptx

:30 12:00 I. I VI II. III. IV. a d V. VI

VDM-SL VDM VDM-SL Toolbox VDM++ Toolbox 1 VDM-SL VDM++ Web bool

untitled

Dive into Algebraic Effects

II 3 yacc (2) 2005 : Yacc 0 ~nakai/ipp2 1 C main main 1 NULL NULL for 2 (a) Yacc 2 (b) 2 3 y

Microsoft PowerPoint - 04SyntaxAnalysis.ppt [互換モード]

Microsoft PowerPoint - Compiler06.pptx

連載 構文解析器結合子 山下伸夫 (( 株 ) タイムインターメディア ) 文字列の分解 1 1 Haskell lines Prelude lines :: String -> [String] lines "" = [] lines s = let (l,s'

(ver. 1.3 (2018) ) yacc 1 1 yacc yacc (Yet Another Compiler Compiler) UNIX yacc yacc y *.y yacc ) yacc *.tab.h *.tab.c C C yacc yacc UNIX yacc bison 2

slide9.dvi

Int Int 29 print Int fmt tostring 2 2 [19] ML ML [19] ML Emacs Standard ML M M ::= x c λx.m M M let x = M in M end (M) x c λx.

ex01.dvi


yacc.dvi

Copyright c 2008 Zhenjiang Hu, All Right Reserved.

A/B (2018/10/19) Ver kurino/2018/soft/soft.html A/B

Minimum C Minimum C Minimum C BNF T okenseq W hite Any D

「プログラミング言語」 SICP 第4章 ~超言語的抽象~ その6

ex01.dvi

parser.y 3. node.rb 4. CD-ROM

/ SCHEDULE /06/07(Tue) / Basic of Programming /06/09(Thu) / Fundamental structures /06/14(Tue) / Memory Management /06/1

5 IO Haskell return (>>=) Haskell Haskell Haskell 5.1 Util Util (Util: Tiny Imperative Language) 1 UtilErr, UtilST, UtilCont, Haskell C

TEX American Mathematical Society PostScript Adobe Systems Incorporated

untitled

つくって学ぶプログラミング言語 RubyによるScheme処理系の実装

fp.gby

test.gby

3 3.1 algebraic datatype data k = 1 1,1... 1,n1 2 2,1... 2,n2... m m,1... m,nm 1 m m m,1,..., m,nm m 1, 2,..., k 1 data Foo x y = Alice x [y] B

shift/reset [13] 2 shift / reset shift reset k call/cc reset shift k shift (...) k 1 + shift(fun k -> 2 * (k 3)) k 2 * (1 + 3) 8 reset shift reset (..

koba/class/soft-kiso/ 1 λ if λx.λy.y false 0 λ typed λ-calculus λ λ 1.1 λ λ, syntax τ (types) ::= b τ 1 τ 2 τ 1

B演習(言語処理系演習)第一回

2005 D Pascal CASL ( ) Pascal C 3. A A Pascal TA TA TA

HPB16_表1-表4.ai

Java演習(4) -- 変数と型 --

ML 演習 第 4 回

橡挿入法の実践

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

r07.dvi

「消える」金融、「創る」金融-2010年のリテール金融

ohp07.dvi

FS_handbook.indd

第10回 コーディングと統合(WWW用).PDF

untitled

「計算と論理」 Software Foundations その4

Condition DAQ condition condition 2 3 XML key value

第12回 モナドパーサ

lexex.dvi

1. A0 A B A0 A : A1,...,A5 B : B1,...,B

超初心者用

DA100データアクイジションユニット通信インタフェースユーザーズマニュアル

PL : pl0 ( ) 1 SableCC ( sablecc ) 1.1 sablecc sablecc Étienne Gagnon [1] Java sablecc sablecc ( ) Visitor DepthFirstAdapter 1 (Depth

Terminal ocaml $ ocaml Objective Caml version # 1+2;;<ret> # 1+2;; - : int = 3 ;; OCaml <ret> - : int = 3 OC

Microsoft PowerPoint - Compiler05.pptx

haskell.gby

() () (parse tree) ( (( ) * 50) ) ( ( NUM 10 + NUM 30 ) * NUM 50 ) ( * ) ( + ) NUM 50 NUM NUM (abstract syntax tree, AST) ( (( ) * 5

新・明解Java入門

ohp03.dvi

Appendix A BASIC BASIC Beginner s All-purpose Symbolic Instruction Code FORTRAN COBOL C JAVA PASCAL (NEC N88-BASIC Windows BASIC (1) (2) ( ) BASIC BAS

2009 D Pascal CASL II ( ) Pascal C 3. A A Pascal TA TA

A/B (2010/10/08) Ver kurino/2010/soft/soft.html A/B

1 # include < stdio.h> 2 # include < string.h> 3 4 int main (){ 5 char str [222]; 6 scanf ("%s", str ); 7 int n= strlen ( str ); 8 for ( int i=n -2; i

PBASIC 2.5 PBASIC 2.5 $PBASIC directive PIN type New DEBUG control characters DEBUGIN Line continuation for comma-delimited lists IF THEN ELSE * SELEC

1 C STL(1) C C C libc C C C++ STL(Standard Template Library ) libc libc C++ C STL libc STL iostream Algorithm libc STL string vector l

2011 D Pascal CASL II ( ) Pascal C 3. A A Pascal TA TA enshu-

JEB Plugin 開発チュートリアル 第4回

Platypus-QM β ( )

2.2 Sage I 11 factor Sage Sage exit quit 1 sage : exit 2 Exiting Sage ( CPU time 0m0.06s, Wall time 2m8.71 s). 2.2 Sage Python Sage 1. Sage.sage 2. sa

Microsoft Word - no15.docx

AN 100: ISPを使用するためのガイドライン

SML#³«È¯ºÇÁ°Àþ

PowerPoint Presentation

r03.dvi

Microsoft PowerPoint - lectureNote6.ppt

untitled

6-1

橡ソート手順比較

Transcription:

7 OCaml () 1. 2. () (compiler) (interpreter) 2 OCaml (syntax) (BNF,backus normal form ) 1 + 2; let x be 2-1 in x; ::= ; let be in ; ::= + - ::= * / ::= 7.1 ( (printable characters) (tokens) 1 (lexical analizer) let twice x=x*2; lettwicex=x*2; let (reserved word)twicex (identifier) =* (symbol)2 56: token type token = LET BE IN EOF ID of string NUM of int ONE of char 1 79

80 7. 57: gettoken(1) module P = Printf exception End_of_system (* *) let _ISTREAM = ref stdin let ch = ref ( ) let read () = (let c =!ch in ch := input_char!_istream; c) let skip () = (ch := input_char!_istream) let lookahead () =!ch (* *) let rec integer i = let c = lookahead () in if (c >= 0 && c <= 9 ) then integer (10*i+(Char.compare (read ()) 0 )) else i (* *) and identifier id = let c = lookahead () in if ((c >= a && c <= z ) (c >= A && c <= Z ) (c >= 0 && c <= 9 ) c == _ ) then identifier (id^(char.escaped (read ()))) else id (* *) and native_token () = let c = lookahead () in if ((c >= a && c <= z ) (c >= A && c <= Z ) c == _ ) then let id = identifier "" in match id with "let" -> LET "be" -> BE "in" -> IN _ -> ID (id) else if (c >= 0 && c <= 9 ) then NUM (integer 0) else ONE (read ()) (* *) and gettoken () = try let token = native_token () in match token with ONE -> gettoken () ONE \t -> gettoken () ONE \n -> gettoken () _ -> token with End_of_file -> EOF

7.1. 81 token LETBEEOFIN stringint char IDNUM ONE ID("fun")Num(1001)ONE( ; ) 58: type = 1 of 1 n of n i of i i ( i ) type i of i i i gettoken lookahead ch (look ahead) ch ch type (variant types) (C union ) (record type) () match with match with 1 -> 1 2 -> 2 59: match of n -> n input char ( ISTREAM 1 lookahead char read read() ch ch () gettoken native token gettoken native token gettoken 83: gettoken # gettoken ();; + - : ONE + # gettoken ();; 1 - : NUM 1 # gettoken ();; abd - : ID "abd"

82 7. gettoken 1 1 run() 60: run0 (* *) let print_token tk = match tk with (ID i) -> P.printf "ID(%s)" i (NUM n) -> P.printf "NUM(%d)" n (LET) -> P.printf "LET" (BE) -> P.printf "BE" (IN) -> P.printf "IN" (EOF) -> P.printf "EOF" (ONE c) -> P.printf "ONE(%c)" c (* *) let rec run () = flush stdout; let rlt = gettoken () in match rlt with (ONE $ ) -> raise End_of_system _ -> (print_token rlt; P.printf "\n"; run()) gettoken token () print token run gettoken while 2 while $ gettoken 2 SML while

7.2. 83 84: run() # run();; 1+2; NUM(1) ONE(+) NUM(2) ONE(;) let x be 10 in x; LET ID(x) BE NUM(10) IN ID(x) ONE(;) $ Exception: End of system. module module Lexer 61: module Lexer module Lexer = struct end 7.2 ( (parser tree) ) ::= ; Expr() let be in ; Def(,, ) ::= + App(Var +,Pair(, )) - App(Var -,Pair(, )) ::= * App( Var *,Pair(, )) / App( Var /,Pair(, )) ::= Num() Var() () (top down parser) (bottom up parser) 2

84 7. Knuth LL(k)(Left-to-right parse, Leftmost-derivation, k-symbol lookahead)lr(k)(left-to-right parse, Rightmost-derivation, k-symbol lookahead) P ET F P let be E in E ; P E ; E T E E + T E E T T F T T F T T / F F F E T E E + T E E T T F T T F T T / F 2, 3 (left recursive) LL(1) LL(1) E T E E + T E E T E E T T F T T F T T / F T T terminal symbolnon-terminal symbol 1.

7.2. 85 2. 1!tok match with tok!tok tok parse advance() x eat(x) eatid eatnum advance tok parse 85: prase # parse ();; let x be 20 in x; $ - : unit = () # parse ();; 1+2; $ - : unit = () $ E0F 1 $

86 7. 62: parse (* *) module L = Lexer (* tok *) let gettoken () = L.gettoken () let tok = ref (L.ONE ) let advance () = tok := gettoken() (* let advance () = (tok := gettoken(); L.print_token (!tok)) *) (* *) exception Syntax_error let error () = raise Syntax_error (* *) let eat t = if (!tok=t) then advance() else error() let eatid () = match!tok with (L.ID _) -> advance() _ -> error() let eatnum () = match!tok with (L.NUM _) -> advance() _ -> error() (* *) let rec parse () = (advance(); p()) and p () = match!tok with L.LET -> (eat(l.let); eatid(); eat(l.be); e(); eat(l.in); e(); eat(l.one ; )) _ -> (e(); eat(l.one ; )) and e () = (t(); e ()) and e () = match!tok with L.ONE + -> (eat(l.one + ); t(); e ()) L.ONE - -> (eat(l.one - ); t(); e ()) _ -> () and t() = (f(); t ()) and t () = match!tok with L.ONE * -> (eat(l.one * ); f(); t ()) L.ONE / -> (eat(l.one / ); f(); t ()) _ -> () and f() = match!tok with L.ID _ -> eatid() L.NUM _ -> eatnum() _ -> error() parse parse () ( ) definition( ) expr (

7.2. 87 ) 63: definition expr type definition = Def of string * expr * expr Expr of expr and expr = Num of int Var of string App of expr * expr Pair of expr * expr module 64: module Ast module Ast = struct end 86: ## let x be 10 in x ; $ Def(x,Num 10,Var x) ## 1+2; $ Expr(App(Var +,Pair(Num 1,Num 2))) ## let y be 10-3 in y/2; $ Def(y,App(Var -,Pair(Num 10,Num 3)),App(Var /,Pair(Var y,num 2))) let x be 10 in x; Def(x,Num 10,Var x) 1+2 Expr(App(Var +,Pair(Num 1,Num 2))) let y be 10-3 in y/2; Def(y,App(Var -,Pair(Num 10,Num 3)), App(Var /,Pair(Var y,num 2))) Def(x,Num 10,Var x) Def / \ x Num 10 Var x

88 7. 65: parse (* *) module L = Lexer module A = Ast (* tok *) let gettoken () = L.gettoken () let tok = ref (L.ONE ) let advance () = tok := gettoken() (* let advance () = (tok := gettoken(); L.print_token (!tok)) *) (* *) exception Syntax_error let error () = raise Syntax_error (* *) let eat t = if (!tok=t) then advance() else error() let eatid () = match!tok with (L.ID id) -> (advance(); id) _ -> error() let eatnum () = match!tok with (L.NUM num) -> (advance(); num) _ -> error() (* *) let rec parse () = (advance(); p()) and p () = match!tok with L.LET -> (eat(l.let); let _str = eatid() in (eat(l.be); let _e1 = e() in eat(l.in); let _e2 = e() in (eat(l.one ; ); A.Def (_str, _e1, _e2)))) _ -> let _e = e() in (eat(l.one ; ); A.Expr _e) and e () = let _t = t() in (e _t) and e _e = match!tok with L.ONE + -> (eat(l.one + ); let _pre = A.App(A.Var "+", A.Pair(_e, t())) in (e _pre)) L.ONE - -> (eat(l.one - ); let _pre = A.App(A.Var "-", A.Pair(_e, t())) in (e _pre)) _ -> _e and t () = let _f = f() in (t _f) and t _t = match!tok with L.ONE * -> (eat(l.one * ); let _pre = A.App(A.Var "*", A.Pair(_t, f())) in (t _pre)) L.ONE / -> (eat(l.one / ); let _pre = A.App(A.Var "/", A.Pair(_t, f())) in (t _pre)) _ -> _t and f() = match!tok with L.ID _ -> A.Var (eatid()) L.NUM _ -> A.Num (eatnum()) _ -> error()

7.2. 89 Expr(App(Var +,Pair(Num 1,Num 2))) Expr App / \ Var + Pair / \ Num 1 Num 2 Def(y,App(Var -,Pair(Num 10,Num 3)), App(Var /,Pair(Var y,num 2))) Def / \ y App App / \ / \ Var - Pair Var / Pair / \ / \ Num 10 Num 3 Var y Num 2 parse let E T E E + T E E T E E E + T E and e _e = match!tok with (L.ONE + ) -> (eat(l.one + ); let _pre = A.App(A.Var "+", A.Pair(_e,t())) in (e _pre)) parse

90 7. 87: parse # parse ();; let x be 20 in x; $ - : definition = Def ("x", Num 20, Var "x")val it = Def ("x",num 20,Var "x") # parse ();; 1+2; $ - : definition = Expr (App(Var "+",Pair (Num 1, Num 2))) module module Parser 66: module Parser module Parser = struct end

7.3. 91 7.3 (referential type) 67: # let x = 10;; val x : int = 10 # let f = fun x -> x;; val f : a -> a = <fun> 10 x (fun x -> x) f (procedural language) (imperative language) () +--------+ x 10 +--------+ x (reference) +---------+ x --> 10 +---------+ --> (C )x x OCaml 3 88: # let x = ref 10;; val x : int ref = contents = 10 # x;; - : int ref = contents = 10 # let y =!x;; val y : int = 10 # y;; - : int = 10 x ref 10 ref! (dereference ) y (assignment) 3 (garbage) (garbage collector) OCaml

92 7. 89: # x := 33;; - : unit = () # x;; - : int ref = contents = 33 #!x;; - : int = 33 # x := "true";; Characters 5-11: x := "true";; ŒŒ Error: This expression has type string but an expression was expected of type int := () ( x int) 4 ref (constructor) OCaml 90: ref # ref;; - : a -> a ref = <fun> 4 let