Context-free grammars and parsing
Project description
cfgrammar : Context-free grammars, parsing and semantic
Table of Contents
Main features
- Grammar properties
- accessible and productive rules / variables
- ε-productive rules / variables
- reduced grammar
- "First" and "Follow" sets
- LL1 parsing
- computes and displays LL(1) table
- constructs parser
- LR parsing
- computes and displays LR(0) automaton
- constructs LR(0), SLR(1), LALR(1) tables and parsers
- Semantic
- parsing with semantic actions
- Abstract Syntax Tree
- predefined semantic classes to produce AST
- Graphviz / dot output
- Latex + Tikz output
- predefined semantic classes to produce AST
Example
Code :
from cfgrammar import Grammar
g = Grammar.from_string('S -> ( S ) S | a')
print(g)
print('productives variables : ', g.productive.vars)
print('Follow sets : ', g.follow)
print(g.tableLL1().to_markdown())
Output:
Grammar(
terminals : ( ) a
variables : S
axiom : S
rules : ['S → ( S ) S', 'S → a']
)
productives variables : {'S'}
Follow sets : {'S': {')', '#'}}
| | S |
|:---|:------------|
| ( | S → ( S ) S |
| ) | |
| a | S → a |
| # | |
API overall graph
Parsing example
from cfgrammar.builder import ParserBuilder
parser = ParserBuilder.from_dict({
'E -> E + T ': lambda p,*_ : p.E + p.T,
'E -> T' : lambda p,*_: p.T,
'T -> T * F ': lambda p,*_ : p.T * p.F,
'T -> F' : lambda p,*_: p.F,
'F -> ( E )' : lambda p,*_ : p.E,
'F -> 0|1|2|3|4|5|6|7|8|9' : lambda p,*_ : int(p[0])
})
print('Value: ',parser.parse('2*(4+5*(1+1))'))
print(parser.grammar)
print(parser.method)
output :
Value: 28
GrammarBase(
terminals : ( ) * + 0 1 2 3 4 5 6 7 8 9
variables : F E T
axiom : E
rules : ['E → E + T', 'E → T', 'T → T * F', 'T → F', 'F → ( E )', 'F → 0', 'F → 1', 'F → 2', 'F → 3', 'F → 4', 'F → 5', 'F → 6', 'F → 7', 'F → 8', 'F → 9']
)
LRMethod.LALR1
Abstract Syntax Tree
Graphviz AST
Predefined semantic producing graphviz (dot) text.
from cfgrammar.semantic import GraphvizAST
display_ast = GraphvizAST()
dot_source = parser.parse('2*(4+5*(1+1))',display_ast)
from graphviz import Source
Source(dot_source).view()
Latex AST
LatexForestAST produces latex source (needs forest latex package)
from cfgrammar.semantic import LatexForestAST
display_latex_ast = LatexForestAST()
latex_source = parser.parse('2*(4+5*(1+1))',display_latex_ast)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cfgrammar-0.1.2.2.tar.gz
(39.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cfgrammar-0.1.2.2.tar.gz.
File metadata
- Download URL: cfgrammar-0.1.2.2.tar.gz
- Upload date:
- Size: 39.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
263529b05851e73025cdf93bfdf1b77bf2626fbd73fc65b046cad0ff3cc8e241
|
|
| MD5 |
c7a3d87b8ed2bc14ac2c40db6f55d1dd
|
|
| BLAKE2b-256 |
44ba00230b45ce7449c64e305022df81629a6a19c7207c3fb42da5024663900d
|
File details
Details for the file cfgrammar-0.1.2.2-py3-none-any.whl.
File metadata
- Download URL: cfgrammar-0.1.2.2-py3-none-any.whl
- Upload date:
- Size: 42.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
251362383499459b9489ba586a0e6672f2e769993437bcc19790405c19b92bb6
|
|
| MD5 |
24d8a48fc1b63fce3d9420858ac7f813
|
|
| BLAKE2b-256 |
634101ba711761da807813fc19590a785e6e64aad63a816986892f2955323e4d
|