diff options
author | Erik Oosting | 2023-12-11 15:25:24 +0100 |
---|---|---|
committer | Erik Oosting | 2023-12-11 15:25:24 +0100 |
commit | 0e96e69659c7c31927b7eee5c40d017b035a6471 (patch) | |
tree | 8d037de425102e8ba6ebf0921aa7f58bacab1976 |
Initial commit
Adding grammar
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | .idea/.gitignore | 3 | ||||
-rw-r--r-- | .idea/inspectionProfiles/profiles_settings.xml | 6 | ||||
-rw-r--r-- | .idea/misc.xml | 23 | ||||
-rw-r--r-- | .idea/modules.xml | 8 | ||||
-rw-r--r-- | .idea/pythonProject.iml | 10 | ||||
-rw-r--r-- | .idea/vcs.xml | 6 | ||||
-rw-r--r-- | gen/ANF.interp | 42 | ||||
-rw-r--r-- | gen/ANF.tokens | 25 | ||||
-rw-r--r-- | gen/ANFLexer.interp | 61 | ||||
-rw-r--r-- | gen/ANFLexer.py | 87 | ||||
-rw-r--r-- | gen/ANFLexer.tokens | 25 | ||||
-rw-r--r-- | gen/ANFListener.py | 39 | ||||
-rw-r--r-- | gen/ANFParser.py | 433 | ||||
-rw-r--r-- | gen/ANFVisitor.py | 28 | ||||
-rw-r--r-- | grammar/ANF.g4 | 28 | ||||
-rw-r--r-- | main.py | 16 |
17 files changed, 841 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f21b54 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/venv/ diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ +<component name="InspectionProjectProfileManager"> + <settings> + <option name="USE_PROJECT_PROFILE" value="false" /> + <version value="1.0" /> + </settings> +</component> \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..716a352 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ANTLRGenerationPreferences"> + <option name="perGrammarGenerationSettings"> + <list> + <PerGrammarGenerationSettings> + <option name="fileName" value="*" /> + <option name="autoGen" value="true" /> + <option name="outputDir" value="" /> + <option name="libDir" value="" /> + <option name="encoding" value="" /> + <option name="pkg" value="" /> + <option name="language" value="Python3" /> + <option name="generateVisitor" value="true" /> + </PerGrammarGenerationSettings> + </list> + </option> + </component> + <component name="Black"> + <option name="sdkName" value="Python 3.11 (pythonProject)" /> + </component> + <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (pythonProject)" project-jdk-type="Python SDK" /> +</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e15ec35 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/.idea/pythonProject.iml" filepath="$PROJECT_DIR$/.idea/pythonProject.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/pythonProject.iml b/.idea/pythonProject.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/.idea/pythonProject.iml @@ -0,0 +1,10 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="PYTHON_MODULE" version="4"> + <component name="NewModuleRootManager"> + <content url="file://$MODULE_DIR$"> + <excludeFolder url="file://$MODULE_DIR$/venv" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/gen/ANF.interp b/gen/ANF.interp new file mode 100644 index 0000000..ff21a22 --- /dev/null +++ b/gen/ANF.interp @@ -0,0 +1,42 @@ +token literal names: +null +'+' +'-' +'*' +'/' +'(' +':' +')' +',' +'let' +'=' +'in' +null +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +IDENT +NUMBER +WS + +rule names: +aexp +funcall +cexp + + +atn: +[4, 1, 14, 60, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 3, 0, 16, 8, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 5, 0, 30, 8, 0, 10, 0, 12, 0, 33, 9, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 40, 8, 1, 10, 1, 12, 1, 43, 9, 1, 1, 1, 1, 1, 1, 1, 3, 1, 48, 8, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 58, 8, 2, 1, 2, 0, 1, 0, 3, 0, 2, 4, 0, 0, 65, 0, 15, 1, 0, 0, 0, 2, 47, 1, 0, 0, 0, 4, 57, 1, 0, 0, 0, 6, 7, 6, 0, -1, 0, 7, 16, 5, 12, 0, 0, 8, 16, 5, 13, 0, 0, 9, 10, 5, 5, 0, 0, 10, 11, 5, 12, 0, 0, 11, 12, 5, 6, 0, 0, 12, 13, 3, 4, 2, 0, 13, 14, 5, 7, 0, 0, 14, 16, 1, 0, 0, 0, 15, 6, 1, 0, 0, 0, 15, 8, 1, 0, 0, 0, 15, 9, 1, 0, 0, 0, 16, 31, 1, 0, 0, 0, 17, 18, 10, 5, 0, 0, 18, 19, 5, 1, 0, 0, 19, 30, 3, 0, 0, 6, 20, 21, 10, 4, 0, 0, 21, 22, 5, 2, 0, 0, 22, 30, 3, 0, 0, 5, 23, 24, 10, 3, 0, 0, 24, 25, 5, 3, 0, 0, 25, 30, 3, 0, 0, 4, 26, 27, 10, 2, 0, 0, 27, 28, 5, 4, 0, 0, 28, 30, 3, 0, 0, 3, 29, 17, 1, 0, 0, 0, 29, 20, 1, 0, 0, 0, 29, 23, 1, 0, 0, 0, 29, 26, 1, 0, 0, 0, 30, 33, 1, 0, 0, 0, 31, 29, 1, 0, 0, 0, 31, 32, 1, 0, 0, 0, 32, 1, 1, 0, 0, 0, 33, 31, 1, 0, 0, 0, 34, 35, 5, 12, 0, 0, 35, 36, 5, 5, 0, 0, 36, 41, 3, 0, 0, 0, 37, 38, 5, 8, 0, 0, 38, 40, 3, 0, 0, 0, 39, 37, 1, 0, 0, 0, 40, 43, 1, 0, 0, 0, 41, 39, 1, 0, 0, 0, 41, 42, 1, 0, 0, 0, 42, 44, 1, 0, 0, 0, 43, 41, 1, 0, 0, 0, 44, 45, 5, 7, 0, 0, 45, 48, 1, 0, 0, 0, 46, 48, 3, 0, 0, 0, 47, 34, 1, 0, 0, 0, 47, 46, 1, 0, 0, 0, 48, 3, 1, 0, 0, 0, 49, 50, 5, 9, 0, 0, 50, 51, 5, 12, 0, 0, 51, 52, 5, 10, 0, 0, 52, 53, 3, 2, 1, 0, 53, 54, 5, 11, 0, 0, 54, 55, 3, 4, 2, 0, 55, 58, 1, 0, 0, 0, 56, 58, 3, 2, 1, 0, 57, 49, 1, 0, 0, 0, 57, 56, 1, 0, 0, 0, 58, 5, 1, 0, 0, 0, 6, 15, 29, 31, 41, 47, 57] \ No newline at end of file diff --git a/gen/ANF.tokens b/gen/ANF.tokens new file mode 100644 index 0000000..9a3d551 --- /dev/null +++ b/gen/ANF.tokens @@ -0,0 +1,25 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +IDENT=12 +NUMBER=13 +WS=14 +'+'=1 +'-'=2 +'*'=3 +'/'=4 +'('=5 +':'=6 +')'=7 +','=8 +'let'=9 +'='=10 +'in'=11 diff --git a/gen/ANFLexer.interp b/gen/ANFLexer.interp new file mode 100644 index 0000000..9be78fb --- /dev/null +++ b/gen/ANFLexer.interp @@ -0,0 +1,61 @@ +token literal names: +null +'+' +'-' +'*' +'/' +'(' +':' +')' +',' +'let' +'=' +'in' +null +null +null + +token symbolic names: +null +null +null +null +null +null +null +null +null +null +null +null +IDENT +NUMBER +WS + +rule names: +T__0 +T__1 +T__2 +T__3 +T__4 +T__5 +T__6 +T__7 +T__8 +T__9 +T__10 +IDENT +NUMBER +Letter +Digit +WS + +channel names: +DEFAULT_TOKEN_CHANNEL +HIDDEN + +mode names: +DEFAULT_MODE + +atn: +[4, 0, 14, 82, 6, -1, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 2, 8, 7, 8, 2, 9, 7, 9, 2, 10, 7, 10, 2, 11, 7, 11, 2, 12, 7, 12, 2, 13, 7, 13, 2, 14, 7, 14, 2, 15, 7, 15, 1, 0, 1, 0, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 3, 1, 4, 1, 4, 1, 5, 1, 5, 1, 6, 1, 6, 1, 7, 1, 7, 1, 8, 1, 8, 1, 8, 1, 8, 1, 9, 1, 9, 1, 10, 1, 10, 1, 10, 1, 11, 1, 11, 1, 11, 5, 11, 62, 8, 11, 10, 11, 12, 11, 65, 9, 11, 1, 12, 4, 12, 68, 8, 12, 11, 12, 12, 12, 69, 1, 13, 1, 13, 1, 14, 1, 14, 1, 15, 4, 15, 77, 8, 15, 11, 15, 12, 15, 78, 1, 15, 1, 15, 0, 0, 16, 1, 1, 3, 2, 5, 3, 7, 4, 9, 5, 11, 6, 13, 7, 15, 8, 17, 9, 19, 10, 21, 11, 23, 12, 25, 13, 27, 0, 29, 0, 31, 14, 1, 0, 2, 2, 0, 65, 90, 97, 122, 3, 0, 9, 10, 13, 13, 32, 32, 83, 0, 1, 1, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 5, 1, 0, 0, 0, 0, 7, 1, 0, 0, 0, 0, 9, 1, 0, 0, 0, 0, 11, 1, 0, 0, 0, 0, 13, 1, 0, 0, 0, 0, 15, 1, 0, 0, 0, 0, 17, 1, 0, 0, 0, 0, 19, 1, 0, 0, 0, 0, 21, 1, 0, 0, 0, 0, 23, 1, 0, 0, 0, 0, 25, 1, 0, 0, 0, 0, 31, 1, 0, 0, 0, 1, 33, 1, 0, 0, 0, 3, 35, 1, 0, 0, 0, 5, 37, 1, 0, 0, 0, 7, 39, 1, 0, 0, 0, 9, 41, 1, 0, 0, 0, 11, 43, 1, 0, 0, 0, 13, 45, 1, 0, 0, 0, 15, 47, 1, 0, 0, 0, 17, 49, 1, 0, 0, 0, 19, 53, 1, 0, 0, 0, 21, 55, 1, 0, 0, 0, 23, 58, 1, 0, 0, 0, 25, 67, 1, 0, 0, 0, 27, 71, 1, 0, 0, 0, 29, 73, 1, 0, 0, 0, 31, 76, 1, 0, 0, 0, 33, 34, 5, 43, 0, 0, 34, 2, 1, 0, 0, 0, 35, 36, 5, 45, 0, 0, 36, 4, 1, 0, 0, 0, 37, 38, 5, 42, 0, 0, 38, 6, 1, 0, 0, 0, 39, 40, 5, 47, 0, 0, 40, 8, 1, 0, 0, 0, 41, 42, 5, 40, 0, 0, 42, 10, 1, 0, 0, 0, 43, 44, 5, 58, 0, 0, 44, 12, 1, 0, 0, 0, 45, 46, 5, 41, 0, 0, 46, 14, 1, 0, 0, 0, 47, 48, 5, 44, 0, 0, 48, 16, 1, 0, 0, 0, 49, 50, 5, 108, 0, 0, 50, 51, 5, 101, 0, 0, 51, 52, 5, 116, 0, 0, 52, 18, 1, 0, 0, 0, 53, 54, 5, 61, 0, 0, 54, 20, 1, 0, 0, 0, 55, 56, 5, 105, 0, 0, 56, 57, 5, 110, 0, 0, 57, 22, 1, 0, 0, 0, 58, 63, 3, 27, 13, 0, 59, 62, 3, 27, 13, 0, 60, 62, 3, 29, 14, 0, 61, 59, 1, 0, 0, 0, 61, 60, 1, 0, 0, 0, 62, 65, 1, 0, 0, 0, 63, 61, 1, 0, 0, 0, 63, 64, 1, 0, 0, 0, 64, 24, 1, 0, 0, 0, 65, 63, 1, 0, 0, 0, 66, 68, 3, 29, 14, 0, 67, 66, 1, 0, 0, 0, 68, 69, 1, 0, 0, 0, 69, 67, 1, 0, 0, 0, 69, 70, 1, 0, 0, 0, 70, 26, 1, 0, 0, 0, 71, 72, 7, 0, 0, 0, 72, 28, 1, 0, 0, 0, 73, 74, 2, 48, 57, 0, 74, 30, 1, 0, 0, 0, 75, 77, 7, 1, 0, 0, 76, 75, 1, 0, 0, 0, 77, 78, 1, 0, 0, 0, 78, 76, 1, 0, 0, 0, 78, 79, 1, 0, 0, 0, 79, 80, 1, 0, 0, 0, 80, 81, 6, 15, 0, 0, 81, 32, 1, 0, 0, 0, 5, 0, 61, 63, 69, 78, 1, 6, 0, 0] \ No newline at end of file diff --git a/gen/ANFLexer.py b/gen/ANFLexer.py new file mode 100644 index 0000000..5200e78 --- /dev/null +++ b/gen/ANFLexer.py @@ -0,0 +1,87 @@ +# Generated from /home/erik/PycharmProjects/pythonProject/grammar/ANF.g4 by ANTLR 4.13.1 +from antlr4 import * +from io import StringIO +import sys +if sys.version_info[1] > 5: + from typing import TextIO +else: + from typing.io import TextIO + + +def serializedATN(): + return [ + 4,0,14,82,6,-1,2,0,7,0,2,1,7,1,2,2,7,2,2,3,7,3,2,4,7,4,2,5,7,5,2, + 6,7,6,2,7,7,7,2,8,7,8,2,9,7,9,2,10,7,10,2,11,7,11,2,12,7,12,2,13, + 7,13,2,14,7,14,2,15,7,15,1,0,1,0,1,1,1,1,1,2,1,2,1,3,1,3,1,4,1,4, + 1,5,1,5,1,6,1,6,1,7,1,7,1,8,1,8,1,8,1,8,1,9,1,9,1,10,1,10,1,10,1, + 11,1,11,1,11,5,11,62,8,11,10,11,12,11,65,9,11,1,12,4,12,68,8,12, + 11,12,12,12,69,1,13,1,13,1,14,1,14,1,15,4,15,77,8,15,11,15,12,15, + 78,1,15,1,15,0,0,16,1,1,3,2,5,3,7,4,9,5,11,6,13,7,15,8,17,9,19,10, + 21,11,23,12,25,13,27,0,29,0,31,14,1,0,2,2,0,65,90,97,122,3,0,9,10, + 13,13,32,32,83,0,1,1,0,0,0,0,3,1,0,0,0,0,5,1,0,0,0,0,7,1,0,0,0,0, + 9,1,0,0,0,0,11,1,0,0,0,0,13,1,0,0,0,0,15,1,0,0,0,0,17,1,0,0,0,0, + 19,1,0,0,0,0,21,1,0,0,0,0,23,1,0,0,0,0,25,1,0,0,0,0,31,1,0,0,0,1, + 33,1,0,0,0,3,35,1,0,0,0,5,37,1,0,0,0,7,39,1,0,0,0,9,41,1,0,0,0,11, + 43,1,0,0,0,13,45,1,0,0,0,15,47,1,0,0,0,17,49,1,0,0,0,19,53,1,0,0, + 0,21,55,1,0,0,0,23,58,1,0,0,0,25,67,1,0,0,0,27,71,1,0,0,0,29,73, + 1,0,0,0,31,76,1,0,0,0,33,34,5,43,0,0,34,2,1,0,0,0,35,36,5,45,0,0, + 36,4,1,0,0,0,37,38,5,42,0,0,38,6,1,0,0,0,39,40,5,47,0,0,40,8,1,0, + 0,0,41,42,5,40,0,0,42,10,1,0,0,0,43,44,5,58,0,0,44,12,1,0,0,0,45, + 46,5,41,0,0,46,14,1,0,0,0,47,48,5,44,0,0,48,16,1,0,0,0,49,50,5,108, + 0,0,50,51,5,101,0,0,51,52,5,116,0,0,52,18,1,0,0,0,53,54,5,61,0,0, + 54,20,1,0,0,0,55,56,5,105,0,0,56,57,5,110,0,0,57,22,1,0,0,0,58,63, + 3,27,13,0,59,62,3,27,13,0,60,62,3,29,14,0,61,59,1,0,0,0,61,60,1, + 0,0,0,62,65,1,0,0,0,63,61,1,0,0,0,63,64,1,0,0,0,64,24,1,0,0,0,65, + 63,1,0,0,0,66,68,3,29,14,0,67,66,1,0,0,0,68,69,1,0,0,0,69,67,1,0, + 0,0,69,70,1,0,0,0,70,26,1,0,0,0,71,72,7,0,0,0,72,28,1,0,0,0,73,74, + 2,48,57,0,74,30,1,0,0,0,75,77,7,1,0,0,76,75,1,0,0,0,77,78,1,0,0, + 0,78,76,1,0,0,0,78,79,1,0,0,0,79,80,1,0,0,0,80,81,6,15,0,0,81,32, + 1,0,0,0,5,0,61,63,69,78,1,6,0,0 + ] + +class ANFLexer(Lexer): + + atn = ATNDeserializer().deserialize(serializedATN()) + + decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] + + T__0 = 1 + T__1 = 2 + T__2 = 3 + T__3 = 4 + T__4 = 5 + T__5 = 6 + T__6 = 7 + T__7 = 8 + T__8 = 9 + T__9 = 10 + T__10 = 11 + IDENT = 12 + NUMBER = 13 + WS = 14 + + channelNames = [ u"DEFAULT_TOKEN_CHANNEL", u"HIDDEN" ] + + modeNames = [ "DEFAULT_MODE" ] + + literalNames = [ "<INVALID>", + "'+'", "'-'", "'*'", "'/'", "'('", "':'", "')'", "','", "'let'", + "'='", "'in'" ] + + symbolicNames = [ "<INVALID>", + "IDENT", "NUMBER", "WS" ] + + ruleNames = [ "T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", + "T__7", "T__8", "T__9", "T__10", "IDENT", "NUMBER", "Letter", + "Digit", "WS" ] + + grammarFileName = "ANF.g4" + + def __init__(self, input=None, output:TextIO = sys.stdout): + super().__init__(input, output) + self.checkVersion("4.13.1") + self._interp = LexerATNSimulator(self, self.atn, self.decisionsToDFA, PredictionContextCache()) + self._actions = None + self._predicates = None + + diff --git a/gen/ANFLexer.tokens b/gen/ANFLexer.tokens new file mode 100644 index 0000000..9a3d551 --- /dev/null +++ b/gen/ANFLexer.tokens @@ -0,0 +1,25 @@ +T__0=1 +T__1=2 +T__2=3 +T__3=4 +T__4=5 +T__5=6 +T__6=7 +T__7=8 +T__8=9 +T__9=10 +T__10=11 +IDENT=12 +NUMBER=13 +WS=14 +'+'=1 +'-'=2 +'*'=3 +'/'=4 +'('=5 +':'=6 +')'=7 +','=8 +'let'=9 +'='=10 +'in'=11 diff --git a/gen/ANFListener.py b/gen/ANFListener.py new file mode 100644 index 0000000..cbceaab --- /dev/null +++ b/gen/ANFListener.py @@ -0,0 +1,39 @@ +# Generated from /home/erik/PycharmProjects/pythonProject/grammar/ANF.g4 by ANTLR 4.13.1 +from antlr4 import * +if "." in __name__: + from .ANFParser import ANFParser +else: + from ANFParser import ANFParser + +# This class defines a complete listener for a parse tree produced by ANFParser. +class ANFListener(ParseTreeListener): + + # Enter a parse tree produced by ANFParser#aexp. + def enterAexp(self, ctx:ANFParser.AexpContext): + pass + + # Exit a parse tree produced by ANFParser#aexp. + def exitAexp(self, ctx:ANFParser.AexpContext): + pass + + + # Enter a parse tree produced by ANFParser#funcall. + def enterFuncall(self, ctx:ANFParser.FuncallContext): + pass + + # Exit a parse tree produced by ANFParser#funcall. + def exitFuncall(self, ctx:ANFParser.FuncallContext): + pass + + + # Enter a parse tree produced by ANFParser#cexp. + def enterCexp(self, ctx:ANFParser.CexpContext): + pass + + # Exit a parse tree produced by ANFParser#cexp. + def exitCexp(self, ctx:ANFParser.CexpContext): + pass + + + +del ANFParser \ No newline at end of file diff --git a/gen/ANFParser.py b/gen/ANFParser.py new file mode 100644 index 0000000..834bbb8 --- /dev/null +++ b/gen/ANFParser.py @@ -0,0 +1,433 @@ +# Generated from /home/erik/PycharmProjects/pythonProject/grammar/ANF.g4 by ANTLR 4.13.1 +# encoding: utf-8 +from antlr4 import * +from io import StringIO +import sys +if sys.version_info[1] > 5: + from typing import TextIO +else: + from typing.io import TextIO + +def serializedATN(): + return [ + 4,1,14,60,2,0,7,0,2,1,7,1,2,2,7,2,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, + 0,1,0,3,0,16,8,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0, + 5,0,30,8,0,10,0,12,0,33,9,0,1,1,1,1,1,1,1,1,1,1,5,1,40,8,1,10,1, + 12,1,43,9,1,1,1,1,1,1,1,3,1,48,8,1,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1, + 2,3,2,58,8,2,1,2,0,1,0,3,0,2,4,0,0,65,0,15,1,0,0,0,2,47,1,0,0,0, + 4,57,1,0,0,0,6,7,6,0,-1,0,7,16,5,12,0,0,8,16,5,13,0,0,9,10,5,5,0, + 0,10,11,5,12,0,0,11,12,5,6,0,0,12,13,3,4,2,0,13,14,5,7,0,0,14,16, + 1,0,0,0,15,6,1,0,0,0,15,8,1,0,0,0,15,9,1,0,0,0,16,31,1,0,0,0,17, + 18,10,5,0,0,18,19,5,1,0,0,19,30,3,0,0,6,20,21,10,4,0,0,21,22,5,2, + 0,0,22,30,3,0,0,5,23,24,10,3,0,0,24,25,5,3,0,0,25,30,3,0,0,4,26, + 27,10,2,0,0,27,28,5,4,0,0,28,30,3,0,0,3,29,17,1,0,0,0,29,20,1,0, + 0,0,29,23,1,0,0,0,29,26,1,0,0,0,30,33,1,0,0,0,31,29,1,0,0,0,31,32, + 1,0,0,0,32,1,1,0,0,0,33,31,1,0,0,0,34,35,5,12,0,0,35,36,5,5,0,0, + 36,41,3,0,0,0,37,38,5,8,0,0,38,40,3,0,0,0,39,37,1,0,0,0,40,43,1, + 0,0,0,41,39,1,0,0,0,41,42,1,0,0,0,42,44,1,0,0,0,43,41,1,0,0,0,44, + 45,5,7,0,0,45,48,1,0,0,0,46,48,3,0,0,0,47,34,1,0,0,0,47,46,1,0,0, + 0,48,3,1,0,0,0,49,50,5,9,0,0,50,51,5,12,0,0,51,52,5,10,0,0,52,53, + 3,2,1,0,53,54,5,11,0,0,54,55,3,4,2,0,55,58,1,0,0,0,56,58,3,2,1,0, + 57,49,1,0,0,0,57,56,1,0,0,0,58,5,1,0,0,0,6,15,29,31,41,47,57 + ] + +class ANFParser ( Parser ): + + grammarFileName = "ANF.g4" + + atn = ATNDeserializer().deserialize(serializedATN()) + + decisionsToDFA = [ DFA(ds, i) for i, ds in enumerate(atn.decisionToState) ] + + sharedContextCache = PredictionContextCache() + + literalNames = [ "<INVALID>", "'+'", "'-'", "'*'", "'/'", "'('", "':'", + "')'", "','", "'let'", "'='", "'in'" ] + + symbolicNames = [ "<INVALID>", "<INVALID>", "<INVALID>", "<INVALID>", + "<INVALID>", "<INVALID>", "<INVALID>", "<INVALID>", + "<INVALID>", "<INVALID>", "<INVALID>", "<INVALID>", + "IDENT", "NUMBER", "WS" ] + + RULE_aexp = 0 + RULE_funcall = 1 + RULE_cexp = 2 + + ruleNames = [ "aexp", "funcall", "cexp" ] + + EOF = Token.EOF + T__0=1 + T__1=2 + T__2=3 + T__3=4 + T__4=5 + T__5=6 + T__6=7 + T__7=8 + T__8=9 + T__9=10 + T__10=11 + IDENT=12 + NUMBER=13 + WS=14 + + def __init__(self, input:TokenStream, output:TextIO = sys.stdout): + super().__init__(input, output) + self.checkVersion("4.13.1") + self._interp = ParserATNSimulator(self, self.atn, self.decisionsToDFA, self.sharedContextCache) + self._predicates = None + + + + + class AexpContext(ParserRuleContext): + __slots__ = 'parser' + + def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): + super().__init__(parent, invokingState) + self.parser = parser + + def IDENT(self): + return self.getToken(ANFParser.IDENT, 0) + + def NUMBER(self): + return self.getToken(ANFParser.NUMBER, 0) + + def cexp(self): + return self.getTypedRuleContext(ANFParser.CexpContext,0) + + + def aexp(self, i:int=None): + if i is None: + return self.getTypedRuleContexts(ANFParser.AexpContext) + else: + return self.getTypedRuleContext(ANFParser.AexpContext,i) + + + def getRuleIndex(self): + return ANFParser.RULE_aexp + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterAexp" ): + listener.enterAexp(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitAexp" ): + listener.exitAexp(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitAexp" ): + return visitor.visitAexp(self) + else: + return visitor.visitChildren(self) + + + + def aexp(self, _p:int=0): + _parentctx = self._ctx + _parentState = self.state + localctx = ANFParser.AexpContext(self, self._ctx, _parentState) + _prevctx = localctx + _startState = 0 + self.enterRecursionRule(localctx, 0, self.RULE_aexp, _p) + try: + self.enterOuterAlt(localctx, 1) + self.state = 15 + self._errHandler.sync(self) + token = self._input.LA(1) + if token in [12]: + self.state = 7 + self.match(ANFParser.IDENT) + pass + elif token in [13]: + self.state = 8 + self.match(ANFParser.NUMBER) + pass + elif token in [5]: + self.state = 9 + self.match(ANFParser.T__4) + self.state = 10 + self.match(ANFParser.IDENT) + self.state = 11 + self.match(ANFParser.T__5) + self.state = 12 + self.cexp() + self.state = 13 + self.match(ANFParser.T__6) + pass + else: + raise NoViableAltException(self) + + self._ctx.stop = self._input.LT(-1) + self.state = 31 + self._errHandler.sync(self) + _alt = self._interp.adaptivePredict(self._input,2,self._ctx) + while _alt!=2 and _alt!=ATN.INVALID_ALT_NUMBER: + if _alt==1: + if self._parseListeners is not None: + self.triggerExitRuleEvent() + _prevctx = localctx + self.state = 29 + self._errHandler.sync(self) + la_ = self._interp.adaptivePredict(self._input,1,self._ctx) + if la_ == 1: + localctx = ANFParser.AexpContext(self, _parentctx, _parentState) + self.pushNewRecursionContext(localctx, _startState, self.RULE_aexp) + self.state = 17 + if not self.precpred(self._ctx, 5): + from antlr4.error.Errors import FailedPredicateException + raise FailedPredicateException(self, "self.precpred(self._ctx, 5)") + self.state = 18 + self.match(ANFParser.T__0) + self.state = 19 + self.aexp(6) + pass + + elif la_ == 2: + localctx = ANFParser.AexpContext(self, _parentctx, _parentState) + self.pushNewRecursionContext(localctx, _startState, self.RULE_aexp) + self.state = 20 + if not self.precpred(self._ctx, 4): + from antlr4.error.Errors import FailedPredicateException + raise FailedPredicateException(self, "self.precpred(self._ctx, 4)") + self.state = 21 + self.match(ANFParser.T__1) + self.state = 22 + self.aexp(5) + pass + + elif la_ == 3: + localctx = ANFParser.AexpContext(self, _parentctx, _parentState) + self.pushNewRecursionContext(localctx, _startState, self.RULE_aexp) + self.state = 23 + if not self.precpred(self._ctx, 3): + from antlr4.error.Errors import FailedPredicateException + raise FailedPredicateException(self, "self.precpred(self._ctx, 3)") + self.state = 24 + self.match(ANFParser.T__2) + self.state = 25 + self.aexp(4) + pass + + elif la_ == 4: + localctx = ANFParser.AexpContext(self, _parentctx, _parentState) + self.pushNewRecursionContext(localctx, _startState, self.RULE_aexp) + self.state = 26 + if not self.precpred(self._ctx, 2): + from antlr4.error.Errors import FailedPredicateException + raise FailedPredicateException(self, "self.precpred(self._ctx, 2)") + self.state = 27 + self.match(ANFParser.T__3) + self.state = 28 + self.aexp(3) + pass + + + self.state = 33 + self._errHandler.sync(self) + _alt = self._interp.adaptivePredict(self._input,2,self._ctx) + + except RecognitionException as re: + localctx.exception = re + self._errHandler.reportError(self, re) + self._errHandler.recover(self, re) + finally: + self.unrollRecursionContexts(_parentctx) + return localctx + + + class FuncallContext(ParserRuleContext): + __slots__ = 'parser' + + def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): + super().__init__(parent, invokingState) + self.parser = parser + + def IDENT(self): + return self.getToken(ANFParser.IDENT, 0) + + def aexp(self, i:int=None): + if i is None: + return self.getTypedRuleContexts(ANFParser.AexpContext) + else: + return self.getTypedRuleContext(ANFParser.AexpContext,i) + + + def getRuleIndex(self): + return ANFParser.RULE_funcall + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterFuncall" ): + listener.enterFuncall(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitFuncall" ): + listener.exitFuncall(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitFuncall" ): + return visitor.visitFuncall(self) + else: + return visitor.visitChildren(self) + + + + + def funcall(self): + + localctx = ANFParser.FuncallContext(self, self._ctx, self.state) + self.enterRule(localctx, 2, self.RULE_funcall) + self._la = 0 # Token type + try: + self.state = 47 + self._errHandler.sync(self) + la_ = self._interp.adaptivePredict(self._input,4,self._ctx) + if la_ == 1: + self.enterOuterAlt(localctx, 1) + self.state = 34 + self.match(ANFParser.IDENT) + self.state = 35 + self.match(ANFParser.T__4) + self.state = 36 + self.aexp(0) + self.state = 41 + self._errHandler.sync(self) + _la = self._input.LA(1) + while _la==8: + self.state = 37 + self.match(ANFParser.T__7) + self.state = 38 + self.aexp(0) + self.state = 43 + self._errHandler.sync(self) + _la = self._input.LA(1) + + self.state = 44 + self.match(ANFParser.T__6) + pass + + elif la_ == 2: + self.enterOuterAlt(localctx, 2) + self.state = 46 + self.aexp(0) + pass + + + except RecognitionException as re: + localctx.exception = re + self._errHandler.reportError(self, re) + self._errHandler.recover(self, re) + finally: + self.exitRule() + return localctx + + + class CexpContext(ParserRuleContext): + __slots__ = 'parser' + + def __init__(self, parser, parent:ParserRuleContext=None, invokingState:int=-1): + super().__init__(parent, invokingState) + self.parser = parser + + def IDENT(self): + return self.getToken(ANFParser.IDENT, 0) + + def funcall(self): + return self.getTypedRuleContext(ANFParser.FuncallContext,0) + + + def cexp(self): + return self.getTypedRuleContext(ANFParser.CexpContext,0) + + + def getRuleIndex(self): + return ANFParser.RULE_cexp + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterCexp" ): + listener.enterCexp(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitCexp" ): + listener.exitCexp(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitCexp" ): + return visitor.visitCexp(self) + else: + return visitor.visitChildren(self) + + + + + def cexp(self): + + localctx = ANFParser.CexpContext(self, self._ctx, self.state) + self.enterRule(localctx, 4, self.RULE_cexp) + try: + self.state = 57 + self._errHandler.sync(self) + token = self._input.LA(1) + if token in [9]: + self.enterOuterAlt(localctx, 1) + self.state = 49 + self.match(ANFParser.T__8) + self.state = 50 + self.match(ANFParser.IDENT) + self.state = 51 + self.match(ANFParser.T__9) + self.state = 52 + self.funcall() + self.state = 53 + self.match(ANFParser.T__10) + self.state = 54 + self.cexp() + pass + elif token in [5, 12, 13]: + self.enterOuterAlt(localctx, 2) + self.state = 56 + self.funcall() + pass + else: + raise NoViableAltException(self) + + except RecognitionException as re: + localctx.exception = re + self._errHandler.reportError(self, re) + self._errHandler.recover(self, re) + finally: + self.exitRule() + return localctx + + + + def sempred(self, localctx:RuleContext, ruleIndex:int, predIndex:int): + if self._predicates == None: + self._predicates = dict() + self._predicates[0] = self.aexp_sempred + pred = self._predicates.get(ruleIndex, None) + if pred is None: + raise Exception("No predicate with index:" + str(ruleIndex)) + else: + return pred(localctx, predIndex) + + def aexp_sempred(self, localctx:AexpContext, predIndex:int): + if predIndex == 0: + return self.precpred(self._ctx, 5) + + + if predIndex == 1: + return self.precpred(self._ctx, 4) + + + if predIndex == 2: + return self.precpred(self._ctx, 3) + + + if predIndex == 3: + return self.precpred(self._ctx, 2) + + + + + diff --git a/gen/ANFVisitor.py b/gen/ANFVisitor.py new file mode 100644 index 0000000..982fa58 --- /dev/null +++ b/gen/ANFVisitor.py @@ -0,0 +1,28 @@ +# Generated from /home/erik/PycharmProjects/pythonProject/grammar/ANF.g4 by ANTLR 4.13.1 +from antlr4 import * +if "." in __name__: + from .ANFParser import ANFParser +else: + from ANFParser import ANFParser + +# This class defines a complete generic visitor for a parse tree produced by ANFParser. + +class ANFVisitor(ParseTreeVisitor): + + # Visit a parse tree produced by ANFParser#aexp. + def visitAexp(self, ctx:ANFParser.AexpContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#funcall. + def visitFuncall(self, ctx:ANFParser.FuncallContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#cexp. + def visitCexp(self, ctx:ANFParser.CexpContext): + return self.visitChildren(ctx) + + + +del ANFParser \ No newline at end of file diff --git a/grammar/ANF.g4 b/grammar/ANF.g4 new file mode 100644 index 0000000..95ab835 --- /dev/null +++ b/grammar/ANF.g4 @@ -0,0 +1,28 @@ +grammar ANF; + +aexp + : IDENT + | NUMBER + | aexp '+' aexp + | aexp '-' aexp + | aexp '*' aexp + | aexp '/' aexp + | '(' IDENT ':' cexp ')' + ; + +funcall + : IDENT '(' aexp (',' aexp)* ')' + | aexp + ; +cexp + : 'let' IDENT '=' funcall 'in' cexp + | funcall + ; + +IDENT: Letter (Letter | Digit)*; +NUMBER: Digit+; + +fragment Letter: 'A' .. 'Z' | 'a' .. 'z'; +fragment Digit: '0'..'9'; + +WS: [ \t\n\r]+ -> skip; diff --git a/main.py b/main.py new file mode 100644 index 0000000..68aa93d --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +# This is a sample Python script. + +# Press Shift+F10 to execute it or replace it with your code. +# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. + + +def print_hi(name): + # Use a breakpoint in the code line below to debug your script. + print(f'Hi, {name}') # Press Ctrl+8 to toggle the breakpoint. + + +# Press the green button in the gutter to run the script. +if __name__ == '__main__': + print_hi('PyCharm') + +# See PyCharm help at https://www.jetbrains.com/help/pycharm/ |