/yarl

Yet Another Regular Language is a project which aims to compile Chocopy-Python syntax

Primary LanguagePythonMIT LicenseMIT

YARL - Yet Another Regular Language

Instalation

It is recommended to build a virtual environment before installing dependencies for this project as it can reconfigure environment variables.

$ virtualenv env
$ source env/bin/activate

or

$ ./env/Scripts/Activate.ps1

According the OS you're currently using.

After this, install the requirements (as we are using rich, typer, anytree, click and some other libraries in this project) and YARL in developer mode with as it can have some changes later

$ pip install -r requirements.txt
$ python setup.py develop

Use

If you're placed in the main folder try running the next command to see if YARL was successfully installed:

$ python .\src.\main.py .\samples\parser\simple_sample.yarl --debug

This will get you in prompt the Lexemes and Tokens scanned in a debug table as following

Scanning

The proper Abstract Syntax Tree will also be printed and saved in /output folder:

$ python .\src.\main.py .\samples\parser\simple_sample.yarl --debug

Program
└── DEF_STATEMENT
    ├── def
    ├── add
    ├── (
    ├── TYPED_VAR
    │   ├── a
    │   ├── :
    │   └── int
    ├── ,
    ├── TYPED_VAR
    │   ├── b
    │   ├── :
    │   └── int
    ├── )
    ├── :
    └── BLOCK
        ├── STATEMENT
        │   ├── EXPR
        │   │   └── result
        │   ├── =
        │   └── EXPR
        │       ├── a
        │       ├── +
        │       └── b
        └── STATEMENT
            ├── return
            └── result

--> Accepted Program

Its proper AST image will be the following:

AST

Recovery Technique

The Recursive Descent Parser has a proper method to recover from error, the current technique used is known as Panic Mode, so if you create a program with fails, the parser will force the parsing to be finished despite the errors as the following example.

$ python .\src\main.py .\samples\parser\simple_fail_sample.yarl --debug
...
Program
├── DEF_STATEMENT
│   ├── def
│   ├── add
│   ├── ERROR
│   └── BLOCK
│       ├── STATEMENT
│       │   ├── EXPR
│       │   │   └── result
│       │   ├── =
│       │   └── EXPR
│       │       └── a
│       ├── ERROR
│       └── STATEMENT
│           ├── return
│           └── result
└── DEF_STATEMENT
    ├── def
    ├── substraction
    ├── (
    ├── TYPED_VAR
    │   ├── a
    │   ├── :
    │   └── int
    ├── )
    ├── :
    └── BLOCK
        └── ERROR

And the errors will be forced to be displayed:

Errors

And the respective AST will be the following

AST_Error

You can try with your own examples and try if they are a correct program according to this modified Chocopy Grammar.