Jlox解释器的源代码和词法扫描

时间: 2024-03-23 18:43:06 浏览: 12
Jlox是一个用Java编写的lox语言解释器,它包含了词法扫描和语法分析等功能。以下是Jlox的源代码和词法扫描器部分的示例代码: ### Lox.java ```java import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class Lox { static boolean hadError = false; public static void main(String[] args) throws IOException { if (args.length > 1) { System.out.println("Usage: jlox [script]"); System.exit(64); } else if (args.length == 1) { runFile(args[0]); } else { runPrompt(); } } private static void runFile(String path) throws IOException { byte[] bytes = Files.readAllBytes(Paths.get(path)); run(new String(bytes, Charset.defaultCharset())); if (hadError) System.exit(65); } private static void runPrompt() throws IOException { System.out.println("Welcome to the JLox REPL!"); for (;;) { System.out.print("> "); run(new java.util.Scanner(System.in).nextLine()); hadError = false; } } private static void run(String source) { Scanner scanner = new Scanner(source); List<Token> tokens = scanner.scanTokens(); // For now, just print the tokens. for (Token token : tokens) { System.out.println(token); } } static void error(int line, String message) { report(line, "", message); } private static void report(int line, String where, String message) { System.err.println( "[line " + line + "] Error" + where + ": " + message); hadError = true; } } ``` ### Scanner.java ```java import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static TokenType.*; class Scanner { private final String source; private final List<Token> tokens = new ArrayList<>(); private int start = 0; private int current = 0; private int line = 1; private static final Map<String, TokenType> keywords; static { keywords = new HashMap<>(); keywords.put("and", AND); keywords.put("class", CLASS); keywords.put("else", ELSE); keywords.put("false", FALSE); keywords.put("for", FOR); keywords.put("fun", FUN); keywords.put("if", IF); keywords.put("nil", NIL); keywords.put("or", OR); keywords.put("print", PRINT); keywords.put("return", RETURN); keywords.put("super", SUPER); keywords.put("this", THIS); keywords.put("true", TRUE); keywords.put("var", VAR); keywords.put("while", WHILE); } Scanner(String source) { this.source = source; } List<Token> scanTokens() { while (!isAtEnd()) { // We are at the beginning of the next lexeme. start = current; scanToken(); } tokens.add(new Token(EOF, "", null, line)); return tokens; } private void scanToken() { char c = advance(); switch (c) { case '(': addToken(LEFT_PAREN); break; case ')': addToken(RIGHT_PAREN); break; case '{': addToken(LEFT_BRACE); break; case '}': addToken(RIGHT_BRACE); break; case ',': addToken(COMMA); break; case '.': addToken(DOT); break; case '-': addToken(MINUS); break; case '+': addToken(PLUS); break; case ';': addToken(SEMICOLON); break; case '*': addToken(STAR); break; case '!': addToken(match('=') ? BANG_EQUAL : BANG); break; case '=': addToken(match('=') ? EQUAL_EQUAL : EQUAL); break; case '<': addToken(match('=') ? LESS_EQUAL : LESS); break; case '>': addToken(match('=') ? GREATER_EQUAL : GREATER); break; case '/': if (match('/')) { // A comment goes until the end of the line. while (peek() != '\n' && !isAtEnd()) advance(); } else { addToken(SLASH); } break; case ' ': case '\r': case '\t': // Ignore whitespace. break; case '\n': line++; break; case '"': string(); break; default: if (isDigit(c)) { number(); } else if (isAlpha(c)) { identifier(); } else { Lox.error(line, "Unexpected character."); } break; } } private void identifier() { while (isAlphaNumeric(peek())) advance(); // See if the identifier is a reserved word. String text = source.substring(start, current); TokenType type = keywords.get(text); if (type == null) type = IDENTIFIER; addToken(type); } private void number() { while (isDigit(peek())) advance(); // Look for a fractional part. if (peek() == '.' && isDigit(peekNext())) { // Consume the "." advance(); while (isDigit(peek())) advance(); } addToken(NUMBER, Double.parseDouble(source.substring(start, current))); } private void string() { while (peek() != '"' && !isAtEnd()) { if (peek() == '\n') line++; advance(); } // Unterminated string. if (isAtEnd()) { Lox.error(line, "Unterminated string."); return; } // The closing ". advance(); // Trim the surrounding quotes. String value = source.substring(start + 1, current - 1); addToken(STRING, value); } private boolean match(char expected) { if (isAtEnd()) return false; if (source.charAt(current) != expected) return false; current++; return true; } private char peek() { if (isAtEnd()) return '\0'; return source.charAt(current); } private char peekNext() { if (current + 1 >= source.length()) return '\0'; return source.charAt(current + 1); } private boolean isAlpha(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'; } private boolean isAlphaNumeric(char c) { return isAlpha(c) || isDigit(c); } private boolean isDigit(char c) { return c >= '0' && c <= '9'; } private boolean isAtEnd() { return current >= source.length(); } private char advance() { current++; return source.charAt(current - 1); } private void addToken(TokenType type) { addToken(type, null); } private void addToken(TokenType type, Object literal) { String text = source.substring(start, current); tokens.add(new Token(type, text, literal, line)); } } ``` 这些代码只是Jlox解释器的一部分,但它们展示了如何实现词法扫描器和一些简单的语法分析。

相关推荐

最新推荐

recommend-type

编译原理 词法分析器 源代码 实验报告

自己上编译原理课程上写的源代码(报告后附)和实验报告 花了不少时间 现在不上课了 传上来共享 大家互相学习
recommend-type

c语言 词法分析器实验报告及源代码

一个详细的由c++ 编写的c语言词法分析器的实验报告,报告后附带有C++源代码。在vc++6.0 及visual studio 2010环境中调试通过的win32 应用程序。
recommend-type

词法分析器实验报告及源代码

编译原理涉及词法分析,语法分析,语义分析及优化设计等各方面。...执行词法分析的程序称为词法分析器,也称为扫描器。词法分析是所有分析优化的基础,涉及的知识较少,如状态转换图等,易于实现。
recommend-type

编译原理实验一——C 语言词法分析器设计与实现

通过设计、编制、调试一个具体的词法分析程序,加深对词法分析原理的理解,并掌握在对程序设计语言源程序进行扫描过程中将其分解为各类单词的词法分析方法。
recommend-type

词法分析器 编译原理 C语言(内含设计报告和源代码)

用C语言编写的词法分析器,内部含有完成的程序源代码,拷贝出来即可使用。还有报告设计文档,供大家参考一下。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

如何用python编写api接口

在Python中编写API接口可以使用多种框架,其中比较流行的有Flask和Django。这里以Flask框架为例,简单介绍如何编写API接口。 1. 安装Flask框架 使用pip命令安装Flask框架: ``` pip install flask ``` 2. 编写API接口 创建一个Python文件,例如app.py,编写以下代码: ```python from flask import Flask, jsonify app = Flask(__name__) @app.route('/api/hello', methods=['GET']) def hello():
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。