File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ clean-java:
1212 rm -f java/*
1313
1414python3 : $(GRAMMAR ) $(GRAMMAR_FILES )
15- $(ANTLR ) -Dlanguage=Python3 $< -o python3/
15+ $(ANTLR ) -Dlanguage=Python3 $< -o python3/antlr
1616
1717clean-python3 :
1818 rm -f python3/*
Original file line number Diff line number Diff line change 1+ __pycache__ /
2+ dist /
3+ MANIFEST
4+ .cache /
5+ * .pyc
6+ Pipfile.lock
7+ antlr /ElectroGrammar *
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 55grammar ElectroGrammar;
66import Alphabet;
77
8+ electro_grammar : capacitance;
9+
810capacitance : NUMBER CPREFIX FARAD ?;
911
1012fragment DIGIT : [0-9];
You can’t perform that action at this time.
0 commit comments