Skip to content

Latest commit

 

History

History
47 lines (34 loc) · 2.9 KB

TODO.md

File metadata and controls

47 lines (34 loc) · 2.9 KB

TODO

I. Create the Chat Scanner that scans a .chat source code file and groups keywords, identifiers, etc into tokens which can be understood by the parser II. Create the Chat Parser that reads the tokens from the scanner and outputs an Abstract Syntax Tree (AST). III. Traverse the AST and output valid JavaScript.

I. The Chat Scanner

The scanner reads in the source code and groups it into tokens - the meaningful "words" and "punctuation" that make up Chat's grammar. Think of it like a big switch statement.

Write the Lexer (Scanner) using Lex or Flex:

Start by creating a Lex or Flex file (typically with a .l extension) where you define your lexer rules. Define regular expressions that match the tokens of your language (e.g., keywords, identifiers, literals, operators, etc.). Specify actions to be taken when a token is recognized, such as returning the token type or value. Test your lexer with sample input to ensure it correctly tokenizes your language.

Resources:

https://web.mit.edu/gnu/doc/html/flex_1.html https://begriffs.com/posts/2021-11-28-practical-parsing.html

II. The Chat Parser

Write the Parser using Yacc or Bison:

Create a Yacc or Bison file (usually with a .y extension) where you define your grammar rules using a context-free grammar (CFG). Specify the production rules for your language's syntax, incorporating the tokens generated by your lexer. Implement semantic actions associated with each production rule. These actions build the abstract syntax tree (AST) or perform other necessary tasks. Handle error recovery and reporting. Define how to handle syntax errors gracefully. Generate code to build the AST data structure as you parse the input.

Define the Abstract Syntax Tree (AST) Structure:

Determine the structure of your AST nodes. Each node represents a part of your language's syntax. Create data structures or classes to represent these nodes in your programming language. Each node should store relevant information, such as the type, value, and child nodes. Implement functions or methods to construct the AST as you parse the input.

III. Outputting valid JavaScript

Traverse the AST: Start by implementing a tree traversal algorithm (e.g., depth-first or breadth-first) to visit each node in the AST. As you traverse the tree, you'll need to interpret each node and generate corresponding JavaScript code for it.

Map AST Nodes to JavaScript Constructs: Define a mapping between the nodes in your language's AST and the corresponding JavaScript constructs. This mapping will guide you in generating the appropriate JavaScript code.

Generate JavaScript Code: For each node in the AST, write code that generates the equivalent JavaScript code. This may involve emitting JavaScript expressions, statements, functions, etc., depending on your language's features. Consider how variables, functions, and scoping are handled in your language and map them to JavaScript constructs accordingly.