Kinomo-JVM-Language

49 339 0
Kinomo-JVM-Language

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Đồ án tạo ngôn ngữ lập trình mới và trình biên dịch [FULL-PROJECT] (Lexer + Parser + Semantic) Kinomo JVM Language + Compilers/Interpreters + full code and docs Kinomo là ngôn ngữ có một cú pháp đặc trưng khác với Java, hay C#, nó giống với JS hay Ruby hơn nên chúng ta có thể tích hơp cả biên dịch và thông dịch cùng lúc trong Compiler của Kinomo​

Kinomo JVM Language Đồ án trình biên dịch Trần Phú Quy – Trần Chí Vĩ – Nguyễn Lại Tam Vũ Kinomo JVM Language Có bao nhiều ngơn ngữ lập trình nay? Kinomo JVM Language Leo núi NNLT = Tạo NNLT Kinomo JVM Language Lexing - Scanning Là bước tạo NNLT, phần tích văn theo hướng tuyến tính để trích xuất từ hay gọi token Kinomo JVM Language Parsing – Phân tích cú pháp Bước parsing Một ngôn ngữ xây dựng từ từ - token đơn giản thành thứ phức tạp (biểu thức, câu lệnh) theo cấu trúc ngữ pháp – grammar cho trước Một trình phần tích cú pháp – parser phân tích token dựa theo grammar có để xây dựng cú pháp - abstract syntax tree (AST) Kinomo JVM Language Parsing – Phân tích cú pháp Parsing có lịch sử lâu dài phong phú ngành khoa học máy tính gắn liền với trí tuệ nhân tạo Các kỹ thuật phân tích NNLT ngày dùng để phân tích ngơn ngữ tự nhiên người nhà nghiên cứu AI Parsing bao gồm cho biết lỗi cú pháp – Syntax error Kinomo JVM Language Static analysis Đối với biểu thức đơn giản a + b, hiểu a cộng cho b Nhưng khơng biết đại diện cho đối tượng nào, hay nói dễ hiểu chúng biến toàn cục hay biến cục chúng định nghĩa đâu? Hai thuộc tính phân tích ràng buộc & phân giải Đối với định danh I dentifier phải tìm nơi định nghĩa nối chúng lại với Việc giúp biết kiểu chúng ném lỗi sai kiểu – type error Kinomo JVM Language Code generation – Tạo mã Đây giai đoạn cuối mà bước phía thực xong Tạo mã trình tạo tệp nhị phân để CPU Virtual machine hiểu Các tệp nhị phân binary file byte code Trước thập niên 60, NNLT gắn với compiler cố định tảng cố định (ARM, x86 …) Kể từ năm 60 với bùng nổ máy tính vấn đề di động quan tâm Và byte code đời Byte code đoạn mã trung gian biên dịch source code machine code Mục đích byte code đơn giản giúp NNLT chạy mà khơng quan tâm đến kiến trúc máy tính với giúp sức từ máy ảo – virtual machine Tiêu biểu kể đến Java – JVM, Csharp – CLR, Python – PVM Kinomo JVM Language Scanning Kinomo JVM Language Scanning - Main Kinomo ngơn ngữ có cú pháp đặc trưng khác với Java, hay C#, giống với   JS hay Ruby nên tích hơp cả biên dịch thơng dịch lúc trong Compiler Kinomo p u b lic static void m ain(S trin g [] arg s) th row s IO Excep tion {    if (arg s.len g th > 1) { System exit(64); // [64]    } else if (arg s.leng th = = 1) {      runFile(args[0]); // Lấấ y địa chiỉ fi le code    } else {      runProm pt(); //REPL    }  } //H àm khở iđộng Com piler Kinomo JVM Language Parsing Expression Với Tokens, có enum với TokenType, với kiể u Expression phải sử dụng lớp cho kiểu Với kiểu Expression có phương thức thuộc tính khác n hau, để kiểm tra kiểu Expression có đoạn mã tượng tr ưng bên if (expr instanceof Expr.Binary) { // } else if (expr instanceof Expr.Grouping) { // } else // Với kiểu kiểm tra chậm, khó kiểm tra luồng thực thi Vậy nên ý tưởng tồi lập trình Kinomo JVM Language Parsing Expression Vậy có vấn đề cần giải cấu trúc Expression mở rộng thêm kiểu(mở rộng thêm class): mở rộng thêm phương thức class: …trong tương lai cách dễ dàng, mà khơng phá hủy cấu trúc có chương trình Kinomo JVM Language Parsing Expression Okay! Để giải vấn đề sử dụng mẫu Visitor Desi gn Pattern Đại khái để link bên dưới: [Tìm hiểu design pattern visistor qua ví dụ Viblo]( https://viblo.asia/p/tim-hieu-visitor-pattern-qua-vi-du-ZabG9zedvzY6) Theo đó, ta có abstract class Expression với phương thức ảo accept(), i nterface khai báo visitExpression Vậy ta excute expression mà khơng cần biết kiểu expres sion, ta cần gọi phương thức accept(), thực thi expression theo kiểu định sẵn Kinomo JVM Language Parsing Expression - Evaluating Expressions Có nhiều cách để dịch mã nguồn sang mã máy để máy tính có th ể hiểu thực thi Một số kể dịch trực tiếp sang mã máy, chuyển thành ngơn ngữ cấp cao hơn, chuyển sang bytecode để ch ạy máy ảo (Java…) Nhưng trình biên dịch thực thi trực tiếp c ú pháp Để giải điều tiếp cận đến Design Pattern Interpreter Pattern (Dẫn nguồn: https:// gpcoder.com/4702-huong-dan-java-design-pattern-interpreter/) Kinomo JVM Language Parsing Expression - Evaluating Expressions Interpreter Pattern giúp người lập trình “xây dựng” đối tượn g “động” cách đọc mô tả đối tượng sau “xây dựng” đối tượng theo mơ tả Cài đặt Interpreter: Kinomo JVM Language Parsing Expression - Evaluating Expressions – Interpreter Pattern Ví dụ: Sau xây dựng chương trình tính 20 cộng = 28 h ay 10 trừ = 6, theo Interpreter Pattern Kinomo JVM Language Parsing Expression - Evaluating Expressions – Interpreter Pattern public class InterpreterEngineContext { public int add(String input) { String[] tokens = interpret(input); int num1 = Integer.parseInt(tokens[0]); int num2 = Integer.parseInt(tokens[1]); return (num1 + num2); } public int subtract(String input) { String[] tokens = interpret(input); int num1 = Integer.parseInt(tokens[0]); int num2 = Integer.parseInt(tokens[1]); return (num1 - num2); } private String[] interpret(String input) { String str = input.replaceAll("[^0-9]", " "); str = str.replaceAll("( )+", " ").trim(); return str.split(" "); } } Kinomo JVM Language Parsing Expression - Evaluating Expressions – Interpreter Pattern public interface Expression { int interpret(InterpreterEngineContext context); } public class AddExpression implements Expression { private String expression; public AddExpression(String expression) { this.expression = expression; } @Override public int interpret(InterpreterEngineContext context) { return context.add(expression); } } Kinomo JVM Language Parsing Expression - Evaluating Expressions – Interpreter Pattern public class SubtractExpression implements Expression { private String expression; public SubtractExpression(String expression) { this.expression = expression; } @Override public int interpret(InterpreterEngineContext context) { return context.subtract(expression); } } Kinomo JVM Language Parsing Expression - Evaluating Expressions – Interpreter Pattern Tương tự có class Interpreter implements Expr.Visitor, Stmt.Visitor {} @Override public Object visitLiteralExpr(Expr.Literal expr) { return expr.value; } //Literal @Override public Object visitGroupingExpr(Expr.Grouping expr) { return evaluate(expr.expression); }//Group private Object evaluate(Expr expr) { return expr.accept(this); } Kinomo JVM Language Parsing Expression - Evaluating Expressions – Interpreter Pattern Chúng ta xem ví dụ đơn giản biểu thức Unary (vd: !true, -5, ) @Override public Object visitUnaryExpr(Expr.Unary expr) { Object right = evaluate(expr.right); switch (expr.operator.type) { case BANG: return !isTruthy(right); case MINUS: checkNumberOperand(expr.operator, right); return -(double)right; } return null; } Kinomo JVM Language Parsing Expression - Evaluating Expressions – Interpreter Pattern Chúng ta xem ví dụ đơn giản biểu thức Binary (vd: +,-,*) @Override public Object visitBinaryExpr(Expr.Binary expr) { Object left = evaluate(expr.left); Object right = evaluate(expr.right); switch (expr.operator.type) { case MINUS: return (double)left - (double)right; case SLASH: return (double)left / (double)right; case STAR: return (double)left * (double)right; } return null; } Kinomo JVM Language Statement program → statement* EOF ; statement → exprStmt | printStmt ; exprStmt → expression ";" ; printStmt → "print" expression ";" ; Free PPT _ Click to add title Click to edit Master text styles - Standard (4:3) This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments With this many slides you are able to make a complete PowerP oint Presentation that best suit your needs This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments With this many slides you are able to make a complete PowerP oint Presentation that best suit your needs This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments With this many slides you are able to make a complete PowerP oint Presentation that best suit your needs This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments With this many slides you are able to make a complete PowerP oint Presentation that best suit your needs Free PPT _ Click to add title Click to edit Master text styles - Standard (4:3) This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments With this many slides y ou are able to make a complete PowerPoint Presentation that best suit your needs This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments With this many slides y ou are able to make a complete PowerPoint Presentation that best suit your needs This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments With this many slides y ou are able to make a complete PowerPoint Presentation that best suit your needs This PowerPoint Template has clean and neutral design that can be adapted to any content and meets various market segments

Ngày đăng: 11/06/2019, 17:56

Từ khóa liên quan

Mục lục

  • Slide 1

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

  • Kinomo JVM Language

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan