Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

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

NotificationsYou must be signed in to change notification settings

danialkeimasi/python-compiler-regex-based-scanner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 installtyper 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:

fromscannerimportScanner,UnknownTokenErrorrules= [    ('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:fortokeninscanner.token_generator():print(token)exceptUnknownTokenErroraserror:print(error)

Credits

  • Danial Keimasi

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp