type2/readme.md

82 lines
2.2 KiB
Markdown
Raw Normal View History

2022-03-19 19:23:38 +01:00
## Description
2022-03-20 14:39:50 +01:00
`type2` is a slow, but versatile reader for arbitrary context free languages (Chomsky hierarchy type 2).
2022-03-19 19:23:38 +01:00
2022-03-20 14:39:50 +01:00
Being fed with a JSON file, which specifies the language, and a string via `stdin`, it analyzes the input, decomposes it and writes its corresponding abstract syntax tree to `stdout`.
2022-03-19 19:23:38 +01:00
2022-03-20 14:39:50 +01:00
A typical call looks like this: `cat input.txt | type2 language.tp2.json`
## Working principle
```mermaid
flowchart LR
input_string[input string]
terminal_symbol_chain[terminal symbol chain]
abstract_syntax_tree[abstract syntax tree]
output_string[output string]
input_string -- lexical analysis --> terminal_symbol_chain
terminal_symbol_chain -- parsing --> abstract_syntax_tree
abstract_syntax_tree -- JSON encoding --> output_string
```
2022-03-19 19:23:38 +01:00
## Language specification
2022-03-20 14:39:50 +01:00
A language is specified by a JSON file, which represents a value of the following type:
2022-03-19 19:23:38 +01:00
```
record<
2022-03-20 14:39:50 +01:00
version:string,
2022-03-19 19:23:38 +01:00
lexer_rules:list<
record<
type:string,
parameters:map<string,any>
>
>,
parser_rules:list<
record<
label:string,
premise:string,
conclusion:list<
record<
type:string,
parameters:map<string,any>
>
>
>
>,
parser_start:string
>
```
2022-03-20 14:39:50 +01:00
where:
- `parser_start` is one of the parser rule `label` values.
- `lexer_rules.*.type` is one of `ignore`, `void`, `boolean`, `int`, `float`, `string` (see section _Lexer rule types_)
- `parser_rules.*.conclusion.*.type` is one of `terminal`, `variable` (see section _Conclusion element types_)
2022-03-19 19:23:38 +01:00
The recommended extension for such files is `.tp2.json`.
### Lexer rule types
| type | meaning | parameters |
|-- |-- |-- |
| `ignore` | disregard matching strings | `pattern` |
2022-03-20 14:39:50 +01:00
| `void` | do not assign a value to matching strings | `pattern`, `id` |
| `boolean` | interpret matching strings as boolean values | `pattern`, `id`, `value` |
| `int` | interpret matching strings as integer number values | `pattern`, `id` |
| `float` | interpret matching strings as floating point number values | `pattern`, `id` |
| `string` | interpret matching strings as string values | `pattern`, `id` |
2022-03-19 19:23:38 +01:00
### Conclusion element types
| type | meaning | parameters |
|-- |-- |-- |
2022-03-20 14:39:50 +01:00
| `terminal` | the part is a terminal symbol | `id` |
| `variable` | the part is a syntactic variable | `id` |
2022-03-19 19:23:38 +01:00