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

A pure Python LR/GLR parser -http://www.igordejanovic.net/parglare/

License

NotificationsYou must be signed in to change notification settings

igordejanovic/parglare

Repository files navigation

https://raw.githubusercontent.com/igordejanovic/parglare/master/docs/images/parglare-logo.png

build-statuscoveragedocsstatuslicensepython-versions

A pure Python scannerless LR/GLR parser.

For more information seethe docs.

Quick intro

This is just a small example to get the general idea. This example shows how toparse and evaluate expressions with 5 operations with different priority andassociativity. Evaluation is done using semantic/reduction actions.

The whole expression evaluator is done in under 30 lines of code!

fromparglareimportParser,Grammargrammar=r"""E: E '+' E  {left, 1} | E '-' E  {left, 1} | E '*' E  {left, 2} | E '/' E  {left, 2} | E '^' E  {right, 3} | '(' E ')' | number;terminalsnumber: /\d+(\.\d+)?/;"""actions= {"E": [lambda_,n:n[0]+n[2],lambda_,n:n[0]-n[2],lambda_,n:n[0]*n[2],lambda_,n:n[0]/n[2],lambda_,n:n[0]**n[2],lambda_,n:n[1],lambda_,n:n[0]],"number":lambda_,value:float(value),}g=Grammar.from_string(grammar)parser=Parser(g,debug=True,actions=actions)result=parser.parse("34 + 4.6 / 2 * 4^2^2 + 78")print("Result = ",result)# Output# -- Debugging/tracing output with detailed info about grammar, productions,# -- terminals and nonterminals, DFA states, parsing progress,# -- and at the end of the output:# Result = 700.8

Installation

  • Stable version:
$ pip install parglare
  • Development version:
$ git clone git@github.com:igordejanovic/parglare.git$ pip install -e parglare

Citing parglare

If you use parglare in your research please cite this paper:

Igor Dejanović, Parglare: A LR/GLR parser for Python,Science of Computer Programming, issn:0167-6423, p.102734,DOI:10.1016/j.scico.2021.102734, 2021.@article{dejanovic2021b,    author = {Igor Dejanović},    title = {Parglare: A LR/GLR parser for Python},    doi = {10.1016/j.scico.2021.102734},    issn = {0167-6423},    journal = {Science of Computer Programming},    keywords = {parsing, LR, GLR, Python, visualization},    pages = {102734},    url = {https://www.sciencedirect.com/science/article/pii/S0167642321001271},    year = {2021}}

License

MIT

Python versions

Tested with 3.6-3.11

Credits

Initial layout/content of this package was created withCookiecutter and theaudreyr/cookiecutter-pypackage project template.


[8]ページ先頭

©2009-2025 Movatter.jp