Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| creating:syntax [2025/03/27 17:44] – Added starting code snippet ahelwer | creating:syntax [2025/04/06 23:20] (current) – Deleting page ahelwer | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Parsing TLA⁺ Syntax ====== | ||
| - | TLA⁺ is a large, complicated, | ||
| - | This is assisted by [[https:// | ||
| - | This tutorial only uses a minimal subset of TLA⁺ syntax, just enough to handle [[https:// | ||
| - | While that may seem limiting, this tutorial tries to focus on the difficult & interesting parts of parsing the language instead of more mundane drudgework like handling all hundred-some user-definable operator symbols. | ||
| - | You are encouraged to extend this minimal core as you wish; language tooling is best developed incrementally! | ||
| - | Slowly filling in the details of this rough language sketch has a satisfying meditative aspect. | ||
| - | |||
| - | Here is what our minimal language subset includes: | ||
| - | * The '' | ||
| - | * Named parameterized operator definitions like '' | ||
| - | * Comments, both single-line like '' | ||
| - | * Declaration of '' | ||
| - | * The '' | ||
| - | * Finite set literals, like '' | ||
| - | * Some infix operators: '' | ||
| - | * The variable-priming suffix operator | ||
| - | * Vertically-aligned conjunction & disjunction lists | ||
| - | * The '' | ||
| - | * Natural numbers like '' | ||
| - | * Boolean values '' | ||
| - | |||
| - | Notably, the following will not be covered in this tutorial: | ||
| - | * PlusCal | ||
| - | * Declaring '' | ||
| - | * Nesting modules or importing other modules through '' | ||
| - | * Set map & set filter syntax, like '' | ||
| - | * Universal & existential quantification, | ||
| - | * The '' | ||
| - | * Functions, like '' | ||
| - | * Sequences, like ''<< | ||
| - | * The '' | ||
| - | * Temporal- or action-level operators like '' | ||
| - | * Higher-order operator parameters like '' | ||
| - | * The TLA⁺ proof language | ||
| - | |||
| - | As outlined above, you are free to add these missing features (or others) as you wish. | ||
| - | |||
| - | ===== Preparation ===== | ||
| - | |||
| - | Read section I (the first three chapters) of free online textbook // | ||
| - | We will be closely following the material in this book, modifying it to our uses. | ||
| - | The first two chapters are a nice introduction and overview of language implementation generally. | ||
| - | Chapter three specifies a toy language called Lox, to be used as an object of study for the remainder of the book. | ||
| - | Our minimal TLA⁺ subset has some similarity to Lox with regard to expressions involving integers and booleans, but also differences - you can skip the section on closures (unless you want to implement higher-order operator parameters yourself) and the section on classes. | ||
| - | What's important is that it's similar enough for all the fundamentals to still apply! | ||
| - | |||
| - | [[https:// | ||
| - | Everything up to [[https:// | ||
| - | You should thus have followed along and arrived at the following file '' | ||
| - | <code java> | ||
| - | package com.craftinginterpreters.TlaPlus; | ||
| - | |||
| - | import java.io.BufferedReader; | ||
| - | import java.io.IOException; | ||
| - | import java.io.InputStreamReader; | ||
| - | import java.nio.charset.Charset; | ||
| - | import java.nio.file.Files; | ||
| - | import java.nio.file.Paths; | ||
| - | import java.util.List; | ||
| - | |||
| - | public class TlaPlus { | ||
| - | public static void main(String[] args) throws IOException { | ||
| - | if (args.length > 1) { | ||
| - | System.out.println(" | ||
| - | 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, | ||
| - | } | ||
| - | |||
| - | private static void runPrompt() throws IOException { | ||
| - | InputStreamReader input = new InputStreamReader(System.in); | ||
| - | BufferedReader reader = new BufferedReader(input); | ||
| - | |||
| - | for (;;) { | ||
| - | System.out.print("> | ||
| - | String line = reader.readLine(); | ||
| - | if (line == null) break; | ||
| - | run(line); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | private static void run(String source) { | ||
| - | Scanner scanner = new Scanner(source); | ||
| - | List< | ||
| - | |||
| - | // For now, just print the tokens. | ||
| - | for (Token token : tokens) { | ||
| - | System.out.println(token); | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||