Giter VIP home page Giter VIP logo

danialkeimasi / python-compiler-regex-based-scanner Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 2.0 18 KB

a "zero dependency" implementation of a lexical analyzer that tokenize a program using only official "re" package. you can use this repo for your "compiler design" course.

License: MIT License

Python 97.75% C++ 2.25%
scanner tokenizer tokenization programming-language natural-language compiler-cli python-regex

python-compiler-regex-based-scanner's Introduction

Python Regex Based Scanner

this repo contains implementation of a "zero dependency" implementation of a lexical analyzer that tokenize a program using only official "re" package.

Requirements

Python 3.8

How to use

you can just put your rules in to the compiler_cli.py file and use it for yourself.

this is how you can compile your code using compiler_cli.py

PS: you need to install typer to run the compiler_cli.

python compiler_cli.py path/to/file

Simple example

you can also skip using compiler_cli.py if you want and write a script like this:

from scanner import Scanner, UnknownTokenError


rules = [
    ('IF_KW',          r'if'),
    ('ELSE_KW',        r'else'),
    ('FOR_KW',         r'for'),
    ('CONST_STR',      r'".*?"|\'.*?\''),
    ('CONST_NUMBER',   r'\d+'),

    ('PLUS_OP',        r'\+'),
    ('MINUS_OP',       r'\-'),
    ('MULTIPLY_OP',    r'\*'),
    ('DIVIDE_OP',      r'\/'),
    ('LP',             r'\('),
    ('LCB',            r'\{'),
    ('RP',             r'\)'),
    ('RCB',            r'\}'),

    ('EQUAL_OP',       r'=='),
    ('ASSIGNMENT_OP',  r'='),
    ('SEMICOLON',      r';'),
    ('IDENTIFIER',     r'[a-zA-Z_]\w*'),
]

scanner = Scanner(rules, "i = 5 + 3;")
try:
    for token in scanner.token_generator():
        print(token)

except UnknownTokenError as error:
    print(error)

Credits

  • Danial Keimasi

python-compiler-regex-based-scanner's People

Contributors

danialkeimasi avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.