Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/3forges/poc-antlr
https://github.com/3forges/poc-antlr
Last synced: 7 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/3forges/poc-antlr
- Owner: 3forges
- Created: 2023-10-11T19:25:18.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2023-10-11T19:27:04.000Z (about 1 year ago)
- Last Synced: 2024-04-01T16:09:30.631Z (9 months ago)
- Language: TypeScript
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
* Install antlr for typescript cli :
```bash
# first install openjdk https://learn.microsoft.com/en-us/java/openjdk/download#openjdk-21
# --
#
# $ java --version
# openjdk 18.0.2 2022-07-19
# OpenJDK Runtime Environment (build 18.0.2+9-61)
# OpenJDK 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing)
#
# --
npm i -g antlr4ts-cli
java --version
antlr4ts --version
```* then create a first language compiler :
```bash
export LANG_EXAMPLE_NAME="coolnthe_lang"
mkdir -p ./${LANG_EXAMPLE_NAME}/
cat <./${LANG_EXAMPLE_NAME}/${LANG_EXAMPLE_NAME}.g4
grammar ${LANG_EXAMPLE_NAME};file: (expr ';' | declaration ';' | assignment ';')+ EOF;
declaration: VAR IDENTIFIER;
expr: IDENTIFIER (OP IDENTIFIER)?;
assignment: IDENTIFIER '=' (NUMBER | expr);
if_statement: IF '(' expr ')' THEN '{' command '}' (ELSE '{' command '}')?;
command: STOP | CONTINUE | if_statement | assignment;
IDENTIFIER: WORD (NUMBER | WORD)*;
IF: 'if';
THEN: 'then';
ELSE: 'else';
OP: '+' | '-' | '*' | '/';
STOP: 'stop';
CONTINUE: 'continue';
VAR: 'var';NUMBER: [1–9]+;
WORD: [a-z]+;
SPACES: [\t\r\n ]+ -> skip;
EOFantlr4ts -visitor -o ./${LANG_EXAMPLE_NAME}/generated ./${LANG_EXAMPLE_NAME}/${LANG_EXAMPLE_NAME}.g4
```