Skip to content

Commit ac952c6

Browse files
committed
Add python3 backend.
1 parent f17de09 commit ac952c6

6 files changed

Lines changed: 66 additions & 1 deletion

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ clean-java:
1212
rm -f java/*
1313

1414
python3: $(GRAMMAR) $(GRAMMAR_FILES)
15-
$(ANTLR) -Dlanguage=Python3 $< -o python3/
15+
$(ANTLR) -Dlanguage=Python3 $< -o python3/antlr
1616

1717
clean-python3:
1818
rm -f python3/*

python3/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
__pycache__/
2+
dist/
3+
MANIFEST
4+
.cache/
5+
*.pyc
6+
Pipfile.lock
7+
antlr/ElectroGrammar*

python3/Pipfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[[source]]
2+
3+
url = "https://pypi.python.org/simple"
4+
verify_ssl = true
5+
name = "pypi"
6+
7+
8+
[packages]
9+
10+
"antlr4-python3-runtime" = "*"
11+
12+
13+
[dev-packages]
14+
15+
16+
17+
[requires]
18+
19+
python_version = "3.6"

python3/antlr/__init__.py

Whitespace-only changes.

python3/electro_grammar.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from antlr4 import *
2+
from antlr.ElectroGrammarLexer import ElectroGrammarLexer
3+
from antlr.ElectroGrammarListener import ElectroGrammarListener
4+
from antlr.ElectroGrammarParser import ElectroGrammarParser
5+
6+
7+
class ElectroGrammarToObjectListener(ElectroGrammarListener):
8+
def __init__(self):
9+
self.obj = {}
10+
11+
def enterCapacitance(self, ctx):
12+
cprefix_lookup = {'u': 10e-6, 'n': 10e-9, 'p': 10e-12}
13+
number = float(str(ctx.NUMBER()))
14+
cprefix = str(ctx.CPREFIX())
15+
self.obj['capacitance'] = number * cprefix
16+
17+
18+
def parse(input):
19+
lexer = ElectroGrammarLexer(input)
20+
stream = CommonTokenStream(lexer)
21+
parser = ElectroGrammarParser(stream)
22+
tree = parser.electro_grammar()
23+
listener = ElectroGrammarToObjectListener()
24+
walker = ParseTreeWalker()
25+
walker.walk(listener, tree)
26+
return listener.obj
27+
28+
29+
def main(argv):
30+
input = InputStream(argv[1])
31+
tree = parse(input)
32+
print(tree)
33+
34+
35+
if __name__ == '__main__':
36+
import sys
37+
main(sys.argv)

src/ElectroGrammar.g4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
grammar ElectroGrammar;
66
import Alphabet;
77

8+
electro_grammar : capacitance;
9+
810
capacitance : NUMBER CPREFIX FARAD?;
911

1012
fragment DIGIT: [0-9];

0 commit comments

Comments
 (0)