From 2824d28a6fd65e0a1f70a1f8b149a198e7e7fa8d Mon Sep 17 00:00:00 2001 From: Erik Oosting Date: Mon, 11 Dec 2023 16:04:26 +0100 Subject: added labels to each rule --- gen/ANF.interp | 6 +- gen/ANF.tokens | 14 +- gen/ANFLexer.interp | 6 +- gen/ANFLexer.py | 10 +- gen/ANFLexer.tokens | 14 +- gen/ANFListener.py | 96 +++++++-- gen/ANFParser.py | 603 +++++++++++++++++++++++++++++++++++----------------- gen/ANFVisitor.py | 52 ++++- grammar/ANF.g4 | 22 +- 9 files changed, 570 insertions(+), 253 deletions(-) diff --git a/gen/ANF.interp b/gen/ANF.interp index ff21a22..bdbbb73 100644 --- a/gen/ANF.interp +++ b/gen/ANF.interp @@ -1,12 +1,12 @@ token literal names: null +'(' '+' +')' '-' '*' '/' -'(' ':' -')' ',' 'let' '=' @@ -39,4 +39,4 @@ 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 +[4, 1, 14, 66, 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, 1, 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, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 3, 0, 39, 8, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1, 46, 8, 1, 10, 1, 12, 1, 49, 9, 1, 1, 1, 1, 1, 1, 1, 3, 1, 54, 8, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 2, 64, 8, 2, 1, 2, 0, 0, 3, 0, 2, 4, 0, 0, 71, 0, 38, 1, 0, 0, 0, 2, 53, 1, 0, 0, 0, 4, 63, 1, 0, 0, 0, 6, 39, 5, 12, 0, 0, 7, 39, 5, 13, 0, 0, 8, 9, 5, 1, 0, 0, 9, 10, 3, 0, 0, 0, 10, 11, 5, 2, 0, 0, 11, 12, 3, 0, 0, 0, 12, 13, 5, 3, 0, 0, 13, 39, 1, 0, 0, 0, 14, 15, 5, 1, 0, 0, 15, 16, 3, 0, 0, 0, 16, 17, 5, 4, 0, 0, 17, 18, 3, 0, 0, 0, 18, 19, 5, 3, 0, 0, 19, 39, 1, 0, 0, 0, 20, 21, 5, 1, 0, 0, 21, 22, 3, 0, 0, 0, 22, 23, 5, 5, 0, 0, 23, 24, 3, 0, 0, 0, 24, 25, 5, 3, 0, 0, 25, 39, 1, 0, 0, 0, 26, 27, 5, 1, 0, 0, 27, 28, 3, 0, 0, 0, 28, 29, 5, 6, 0, 0, 29, 30, 3, 0, 0, 0, 30, 31, 5, 3, 0, 0, 31, 39, 1, 0, 0, 0, 32, 33, 5, 1, 0, 0, 33, 34, 5, 12, 0, 0, 34, 35, 5, 7, 0, 0, 35, 36, 3, 4, 2, 0, 36, 37, 5, 3, 0, 0, 37, 39, 1, 0, 0, 0, 38, 6, 1, 0, 0, 0, 38, 7, 1, 0, 0, 0, 38, 8, 1, 0, 0, 0, 38, 14, 1, 0, 0, 0, 38, 20, 1, 0, 0, 0, 38, 26, 1, 0, 0, 0, 38, 32, 1, 0, 0, 0, 39, 1, 1, 0, 0, 0, 40, 41, 5, 12, 0, 0, 41, 42, 5, 1, 0, 0, 42, 47, 3, 0, 0, 0, 43, 44, 5, 8, 0, 0, 44, 46, 3, 0, 0, 0, 45, 43, 1, 0, 0, 0, 46, 49, 1, 0, 0, 0, 47, 45, 1, 0, 0, 0, 47, 48, 1, 0, 0, 0, 48, 50, 1, 0, 0, 0, 49, 47, 1, 0, 0, 0, 50, 51, 5, 3, 0, 0, 51, 54, 1, 0, 0, 0, 52, 54, 3, 0, 0, 0, 53, 40, 1, 0, 0, 0, 53, 52, 1, 0, 0, 0, 54, 3, 1, 0, 0, 0, 55, 56, 5, 9, 0, 0, 56, 57, 5, 12, 0, 0, 57, 58, 5, 10, 0, 0, 58, 59, 3, 2, 1, 0, 59, 60, 5, 11, 0, 0, 60, 61, 3, 4, 2, 0, 61, 64, 1, 0, 0, 0, 62, 64, 3, 2, 1, 0, 63, 55, 1, 0, 0, 0, 63, 62, 1, 0, 0, 0, 64, 5, 1, 0, 0, 0, 4, 38, 47, 53, 63] \ No newline at end of file diff --git a/gen/ANF.tokens b/gen/ANF.tokens index 9a3d551..77df291 100644 --- a/gen/ANF.tokens +++ b/gen/ANF.tokens @@ -12,13 +12,13 @@ T__10=11 IDENT=12 NUMBER=13 WS=14 -'+'=1 -'-'=2 -'*'=3 -'/'=4 -'('=5 -':'=6 -')'=7 +'('=1 +'+'=2 +')'=3 +'-'=4 +'*'=5 +'/'=6 +':'=7 ','=8 'let'=9 '='=10 diff --git a/gen/ANFLexer.interp b/gen/ANFLexer.interp index 9be78fb..77b6f16 100644 --- a/gen/ANFLexer.interp +++ b/gen/ANFLexer.interp @@ -1,12 +1,12 @@ token literal names: null +'(' '+' +')' '-' '*' '/' -'(' ':' -')' ',' 'let' '=' @@ -58,4 +58,4 @@ 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 +[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, 40, 0, 0, 34, 2, 1, 0, 0, 0, 35, 36, 5, 43, 0, 0, 36, 4, 1, 0, 0, 0, 37, 38, 5, 41, 0, 0, 38, 6, 1, 0, 0, 0, 39, 40, 5, 45, 0, 0, 40, 8, 1, 0, 0, 0, 41, 42, 5, 42, 0, 0, 42, 10, 1, 0, 0, 0, 43, 44, 5, 47, 0, 0, 44, 12, 1, 0, 0, 0, 45, 46, 5, 58, 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 index 5200e78..548e11e 100644 --- a/gen/ANFLexer.py +++ b/gen/ANFLexer.py @@ -24,10 +24,10 @@ def serializedATN(): 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, + 1,0,0,0,31,76,1,0,0,0,33,34,5,40,0,0,34,2,1,0,0,0,35,36,5,43,0,0, + 36,4,1,0,0,0,37,38,5,41,0,0,38,6,1,0,0,0,39,40,5,45,0,0,40,8,1,0, + 0,0,41,42,5,42,0,0,42,10,1,0,0,0,43,44,5,47,0,0,44,12,1,0,0,0,45, + 46,5,58,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, @@ -65,7 +65,7 @@ class ANFLexer(Lexer): modeNames = [ "DEFAULT_MODE" ] literalNames = [ "", - "'+'", "'-'", "'*'", "'/'", "'('", "':'", "')'", "','", "'let'", + "'('", "'+'", "')'", "'-'", "'*'", "'/'", "':'", "','", "'let'", "'='", "'in'" ] symbolicNames = [ "", diff --git a/gen/ANFLexer.tokens b/gen/ANFLexer.tokens index 9a3d551..77df291 100644 --- a/gen/ANFLexer.tokens +++ b/gen/ANFLexer.tokens @@ -12,13 +12,13 @@ T__10=11 IDENT=12 NUMBER=13 WS=14 -'+'=1 -'-'=2 -'*'=3 -'/'=4 -'('=5 -':'=6 -')'=7 +'('=1 +'+'=2 +')'=3 +'-'=4 +'*'=5 +'/'=6 +':'=7 ','=8 'let'=9 '='=10 diff --git a/gen/ANFListener.py b/gen/ANFListener.py index cbceaab..81322bb 100644 --- a/gen/ANFListener.py +++ b/gen/ANFListener.py @@ -8,30 +8,102 @@ else: # 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): + # Enter a parse tree produced by ANFParser#var. + def enterVar(self, ctx:ANFParser.VarContext): pass - # Exit a parse tree produced by ANFParser#aexp. - def exitAexp(self, ctx:ANFParser.AexpContext): + # Exit a parse tree produced by ANFParser#var. + def exitVar(self, ctx:ANFParser.VarContext): pass - # Enter a parse tree produced by ANFParser#funcall. - def enterFuncall(self, ctx:ANFParser.FuncallContext): + # Enter a parse tree produced by ANFParser#num. + def enterNum(self, ctx:ANFParser.NumContext): pass - # Exit a parse tree produced by ANFParser#funcall. - def exitFuncall(self, ctx:ANFParser.FuncallContext): + # Exit a parse tree produced by ANFParser#num. + def exitNum(self, ctx:ANFParser.NumContext): pass - # Enter a parse tree produced by ANFParser#cexp. - def enterCexp(self, ctx:ANFParser.CexpContext): + # Enter a parse tree produced by ANFParser#add. + def enterAdd(self, ctx:ANFParser.AddContext): pass - # Exit a parse tree produced by ANFParser#cexp. - def exitCexp(self, ctx:ANFParser.CexpContext): + # Exit a parse tree produced by ANFParser#add. + def exitAdd(self, ctx:ANFParser.AddContext): + pass + + + # Enter a parse tree produced by ANFParser#sub. + def enterSub(self, ctx:ANFParser.SubContext): + pass + + # Exit a parse tree produced by ANFParser#sub. + def exitSub(self, ctx:ANFParser.SubContext): + pass + + + # Enter a parse tree produced by ANFParser#mul. + def enterMul(self, ctx:ANFParser.MulContext): + pass + + # Exit a parse tree produced by ANFParser#mul. + def exitMul(self, ctx:ANFParser.MulContext): + pass + + + # Enter a parse tree produced by ANFParser#div. + def enterDiv(self, ctx:ANFParser.DivContext): + pass + + # Exit a parse tree produced by ANFParser#div. + def exitDiv(self, ctx:ANFParser.DivContext): + pass + + + # Enter a parse tree produced by ANFParser#lam. + def enterLam(self, ctx:ANFParser.LamContext): + pass + + # Exit a parse tree produced by ANFParser#lam. + def exitLam(self, ctx:ANFParser.LamContext): + pass + + + # Enter a parse tree produced by ANFParser#call. + def enterCall(self, ctx:ANFParser.CallContext): + pass + + # Exit a parse tree produced by ANFParser#call. + def exitCall(self, ctx:ANFParser.CallContext): + pass + + + # Enter a parse tree produced by ANFParser#atom. + def enterAtom(self, ctx:ANFParser.AtomContext): + pass + + # Exit a parse tree produced by ANFParser#atom. + def exitAtom(self, ctx:ANFParser.AtomContext): + pass + + + # Enter a parse tree produced by ANFParser#let. + def enterLet(self, ctx:ANFParser.LetContext): + pass + + # Exit a parse tree produced by ANFParser#let. + def exitLet(self, ctx:ANFParser.LetContext): + pass + + + # Enter a parse tree produced by ANFParser#fc. + def enterFc(self, ctx:ANFParser.FcContext): + pass + + # Exit a parse tree produced by ANFParser#fc. + def exitFc(self, ctx:ANFParser.FcContext): pass diff --git a/gen/ANFParser.py b/gen/ANFParser.py index 834bbb8..830af0c 100644 --- a/gen/ANFParser.py +++ b/gen/ANFParser.py @@ -10,25 +10,27 @@ else: 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 + 4,1,14,66,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,1,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,1,0,1, + 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,3,0,39,8,0,1,1,1,1,1,1,1,1,1,1, + 5,1,46,8,1,10,1,12,1,49,9,1,1,1,1,1,1,1,3,1,54,8,1,1,2,1,2,1,2,1, + 2,1,2,1,2,1,2,1,2,3,2,64,8,2,1,2,0,0,3,0,2,4,0,0,71,0,38,1,0,0,0, + 2,53,1,0,0,0,4,63,1,0,0,0,6,39,5,12,0,0,7,39,5,13,0,0,8,9,5,1,0, + 0,9,10,3,0,0,0,10,11,5,2,0,0,11,12,3,0,0,0,12,13,5,3,0,0,13,39,1, + 0,0,0,14,15,5,1,0,0,15,16,3,0,0,0,16,17,5,4,0,0,17,18,3,0,0,0,18, + 19,5,3,0,0,19,39,1,0,0,0,20,21,5,1,0,0,21,22,3,0,0,0,22,23,5,5,0, + 0,23,24,3,0,0,0,24,25,5,3,0,0,25,39,1,0,0,0,26,27,5,1,0,0,27,28, + 3,0,0,0,28,29,5,6,0,0,29,30,3,0,0,0,30,31,5,3,0,0,31,39,1,0,0,0, + 32,33,5,1,0,0,33,34,5,12,0,0,34,35,5,7,0,0,35,36,3,4,2,0,36,37,5, + 3,0,0,37,39,1,0,0,0,38,6,1,0,0,0,38,7,1,0,0,0,38,8,1,0,0,0,38,14, + 1,0,0,0,38,20,1,0,0,0,38,26,1,0,0,0,38,32,1,0,0,0,39,1,1,0,0,0,40, + 41,5,12,0,0,41,42,5,1,0,0,42,47,3,0,0,0,43,44,5,8,0,0,44,46,3,0, + 0,0,45,43,1,0,0,0,46,49,1,0,0,0,47,45,1,0,0,0,47,48,1,0,0,0,48,50, + 1,0,0,0,49,47,1,0,0,0,50,51,5,3,0,0,51,54,1,0,0,0,52,54,3,0,0,0, + 53,40,1,0,0,0,53,52,1,0,0,0,54,3,1,0,0,0,55,56,5,9,0,0,56,57,5,12, + 0,0,57,58,5,10,0,0,58,59,3,2,1,0,59,60,5,11,0,0,60,61,3,4,2,0,61, + 64,1,0,0,0,62,64,3,2,1,0,63,55,1,0,0,0,63,62,1,0,0,0,64,5,1,0,0, + 0,4,38,47,53,63 ] class ANFParser ( Parser ): @@ -41,8 +43,8 @@ class ANFParser ( Parser ): sharedContextCache = PredictionContextCache() - literalNames = [ "", "'+'", "'-'", "'*'", "'/'", "'('", "':'", - "')'", "','", "'let'", "'='", "'in'" ] + literalNames = [ "", "'('", "'+'", "')'", "'-'", "'*'", "'/'", + "':'", "','", "'let'", "'='", "'in'" ] symbolicNames = [ "", "", "", "", "", "", "", "", @@ -87,15 +89,21 @@ class ANFParser ( Parser ): 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 getRuleIndex(self): + return ANFParser.RULE_aexp - def cexp(self): - return self.getTypedRuleContext(ANFParser.CexpContext,0) + + def copyFrom(self, ctx:ParserRuleContext): + super().copyFrom(ctx) + + + + class AddContext(AexpContext): + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.AexpContext + super().__init__(parser) + self.copyFrom(ctx) def aexp(self, i:int=None): if i is None: @@ -104,135 +112,285 @@ class ANFParser ( Parser ): return self.getTypedRuleContext(ANFParser.AexpContext,i) - def getRuleIndex(self): - return ANFParser.RULE_aexp + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterAdd" ): + listener.enterAdd(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitAdd" ): + listener.exitAdd(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitAdd" ): + return visitor.visitAdd(self) + else: + return visitor.visitChildren(self) + + + class DivContext(AexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.AexpContext + super().__init__(parser) + self.copyFrom(ctx) + + def aexp(self, i:int=None): + if i is None: + return self.getTypedRuleContexts(ANFParser.AexpContext) + else: + return self.getTypedRuleContext(ANFParser.AexpContext,i) + + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterDiv" ): + listener.enterDiv(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitDiv" ): + listener.exitDiv(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitDiv" ): + return visitor.visitDiv(self) + else: + return visitor.visitChildren(self) + + + class SubContext(AexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.AexpContext + super().__init__(parser) + self.copyFrom(ctx) + + def aexp(self, i:int=None): + if i is None: + return self.getTypedRuleContexts(ANFParser.AexpContext) + else: + return self.getTypedRuleContext(ANFParser.AexpContext,i) + + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterSub" ): + listener.enterSub(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitSub" ): + listener.exitSub(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitSub" ): + return visitor.visitSub(self) + else: + return visitor.visitChildren(self) + + + class MulContext(AexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.AexpContext + super().__init__(parser) + self.copyFrom(ctx) + + def aexp(self, i:int=None): + if i is None: + return self.getTypedRuleContexts(ANFParser.AexpContext) + else: + return self.getTypedRuleContext(ANFParser.AexpContext,i) + + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterMul" ): + listener.enterMul(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitMul" ): + listener.exitMul(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitMul" ): + return visitor.visitMul(self) + else: + return visitor.visitChildren(self) + + + class VarContext(AexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.AexpContext + super().__init__(parser) + self.copyFrom(ctx) + + def IDENT(self): + return self.getToken(ANFParser.IDENT, 0) + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterVar" ): + listener.enterVar(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitVar" ): + listener.exitVar(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitVar" ): + return visitor.visitVar(self) + else: + return visitor.visitChildren(self) + + + class NumContext(AexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.AexpContext + super().__init__(parser) + self.copyFrom(ctx) + + def NUMBER(self): + return self.getToken(ANFParser.NUMBER, 0) + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterNum" ): + listener.enterNum(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitNum" ): + listener.exitNum(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitNum" ): + return visitor.visitNum(self) + else: + return visitor.visitChildren(self) + + + class LamContext(AexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.AexpContext + super().__init__(parser) + self.copyFrom(ctx) + + def IDENT(self): + return self.getToken(ANFParser.IDENT, 0) + def cexp(self): + return self.getTypedRuleContext(ANFParser.CexpContext,0) + def enterRule(self, listener:ParseTreeListener): - if hasattr( listener, "enterAexp" ): - listener.enterAexp(self) + if hasattr( listener, "enterLam" ): + listener.enterLam(self) def exitRule(self, listener:ParseTreeListener): - if hasattr( listener, "exitAexp" ): - listener.exitAexp(self) + if hasattr( listener, "exitLam" ): + listener.exitLam(self) def accept(self, visitor:ParseTreeVisitor): - if hasattr( visitor, "visitAexp" ): - return visitor.visitAexp(self) + if hasattr( visitor, "visitLam" ): + return visitor.visitLam(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) + def aexp(self): + + localctx = ANFParser.AexpContext(self, self._ctx, self.state) + self.enterRule(localctx, 0, self.RULE_aexp) try: - self.enterOuterAlt(localctx, 1) - self.state = 15 + self.state = 38 self._errHandler.sync(self) - token = self._input.LA(1) - if token in [12]: - self.state = 7 + la_ = self._interp.adaptivePredict(self._input,0,self._ctx) + if la_ == 1: + localctx = ANFParser.VarContext(self, localctx) + self.enterOuterAlt(localctx, 1) + self.state = 6 self.match(ANFParser.IDENT) pass - elif token in [13]: - self.state = 8 + + elif la_ == 2: + localctx = ANFParser.NumContext(self, localctx) + self.enterOuterAlt(localctx, 2) + self.state = 7 self.match(ANFParser.NUMBER) pass - elif token in [5]: + + elif la_ == 3: + localctx = ANFParser.AddContext(self, localctx) + self.enterOuterAlt(localctx, 3) + self.state = 8 + self.match(ANFParser.T__0) self.state = 9 - self.match(ANFParser.T__4) + self.aexp() self.state = 10 - self.match(ANFParser.IDENT) + self.match(ANFParser.T__1) self.state = 11 - self.match(ANFParser.T__5) + self.aexp() self.state = 12 - self.cexp() - self.state = 13 - self.match(ANFParser.T__6) + self.match(ANFParser.T__2) 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 - - + elif la_ == 4: + localctx = ANFParser.SubContext(self, localctx) + self.enterOuterAlt(localctx, 4) + self.state = 14 + self.match(ANFParser.T__0) + self.state = 15 + self.aexp() + self.state = 16 + self.match(ANFParser.T__3) + self.state = 17 + self.aexp() + self.state = 18 + self.match(ANFParser.T__2) + pass + + elif la_ == 5: + localctx = ANFParser.MulContext(self, localctx) + self.enterOuterAlt(localctx, 5) + self.state = 20 + self.match(ANFParser.T__0) + self.state = 21 + self.aexp() + self.state = 22 + self.match(ANFParser.T__4) + self.state = 23 + self.aexp() + self.state = 24 + self.match(ANFParser.T__2) + pass + + elif la_ == 6: + localctx = ANFParser.DivContext(self, localctx) + self.enterOuterAlt(localctx, 6) + self.state = 26 + self.match(ANFParser.T__0) + self.state = 27 + self.aexp() + self.state = 28 + self.match(ANFParser.T__5) + self.state = 29 + self.aexp() + self.state = 30 + self.match(ANFParser.T__2) + pass + + elif la_ == 7: + localctx = ANFParser.LamContext(self, localctx) + self.enterOuterAlt(localctx, 7) + self.state = 32 + self.match(ANFParser.T__0) self.state = 33 - self._errHandler.sync(self) - _alt = self._interp.adaptivePredict(self._input,2,self._ctx) + self.match(ANFParser.IDENT) + self.state = 34 + self.match(ANFParser.T__6) + self.state = 35 + self.cexp() + self.state = 36 + self.match(ANFParser.T__2) + pass + except RecognitionException as re: localctx.exception = re self._errHandler.reportError(self, re) self._errHandler.recover(self, re) finally: - self.unrollRecursionContexts(_parentctx) + self.exitRule() return localctx @@ -243,9 +401,24 @@ class ANFParser ( Parser ): super().__init__(parent, invokingState) self.parser = parser + + def getRuleIndex(self): + return ANFParser.RULE_funcall + + + def copyFrom(self, ctx:ParserRuleContext): + super().copyFrom(ctx) + + + + class CallContext(FuncallContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.FuncallContext + super().__init__(parser) + self.copyFrom(ctx) + def IDENT(self): return self.getToken(ANFParser.IDENT, 0) - def aexp(self, i:int=None): if i is None: return self.getTypedRuleContexts(ANFParser.AexpContext) @@ -253,24 +426,45 @@ class ANFParser ( Parser ): 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) + if hasattr( listener, "enterCall" ): + listener.enterCall(self) def exitRule(self, listener:ParseTreeListener): - if hasattr( listener, "exitFuncall" ): - listener.exitFuncall(self) + if hasattr( listener, "exitCall" ): + listener.exitCall(self) def accept(self, visitor:ParseTreeVisitor): - if hasattr( visitor, "visitFuncall" ): - return visitor.visitFuncall(self) + if hasattr( visitor, "visitCall" ): + return visitor.visitCall(self) else: return visitor.visitChildren(self) + class AtomContext(FuncallContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.FuncallContext + super().__init__(parser) + self.copyFrom(ctx) + + def aexp(self): + return self.getTypedRuleContext(ANFParser.AexpContext,0) + + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterAtom" ): + listener.enterAtom(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitAtom" ): + listener.exitAtom(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitAtom" ): + return visitor.visitAtom(self) + else: + return visitor.visitChildren(self) + def funcall(self): @@ -279,37 +473,39 @@ class ANFParser ( Parser ): self.enterRule(localctx, 2, self.RULE_funcall) self._la = 0 # Token type try: - self.state = 47 + self.state = 53 self._errHandler.sync(self) - la_ = self._interp.adaptivePredict(self._input,4,self._ctx) + la_ = self._interp.adaptivePredict(self._input,2,self._ctx) if la_ == 1: + localctx = ANFParser.CallContext(self, localctx) self.enterOuterAlt(localctx, 1) - self.state = 34 + self.state = 40 self.match(ANFParser.IDENT) - self.state = 35 - self.match(ANFParser.T__4) - self.state = 36 - self.aexp(0) self.state = 41 + self.match(ANFParser.T__0) + self.state = 42 + self.aexp() + self.state = 47 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.match(ANFParser.T__7) + self.state = 44 + self.aexp() + self.state = 49 self._errHandler.sync(self) _la = self._input.LA(1) - self.state = 44 - self.match(ANFParser.T__6) + self.state = 50 + self.match(ANFParser.T__2) pass elif la_ == 2: + localctx = ANFParser.AtomContext(self, localctx) self.enterOuterAlt(localctx, 2) - self.state = 46 - self.aexp(0) + self.state = 52 + self.aexp() pass @@ -329,35 +525,70 @@ class ANFParser ( Parser ): super().__init__(parent, invokingState) self.parser = parser + + def getRuleIndex(self): + return ANFParser.RULE_cexp + + + def copyFrom(self, ctx:ParserRuleContext): + super().copyFrom(ctx) + + + + class LetContext(CexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.CexpContext + super().__init__(parser) + self.copyFrom(ctx) + 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) + if hasattr( listener, "enterLet" ): + listener.enterLet(self) def exitRule(self, listener:ParseTreeListener): - if hasattr( listener, "exitCexp" ): - listener.exitCexp(self) + if hasattr( listener, "exitLet" ): + listener.exitLet(self) def accept(self, visitor:ParseTreeVisitor): - if hasattr( visitor, "visitCexp" ): - return visitor.visitCexp(self) + if hasattr( visitor, "visitLet" ): + return visitor.visitLet(self) else: return visitor.visitChildren(self) + class FcContext(CexpContext): + + def __init__(self, parser, ctx:ParserRuleContext): # actually a ANFParser.CexpContext + super().__init__(parser) + self.copyFrom(ctx) + + def funcall(self): + return self.getTypedRuleContext(ANFParser.FuncallContext,0) + + + def enterRule(self, listener:ParseTreeListener): + if hasattr( listener, "enterFc" ): + listener.enterFc(self) + + def exitRule(self, listener:ParseTreeListener): + if hasattr( listener, "exitFc" ): + listener.exitFc(self) + + def accept(self, visitor:ParseTreeVisitor): + if hasattr( visitor, "visitFc" ): + return visitor.visitFc(self) + else: + return visitor.visitChildren(self) + def cexp(self): @@ -365,27 +596,29 @@ class ANFParser ( Parser ): localctx = ANFParser.CexpContext(self, self._ctx, self.state) self.enterRule(localctx, 4, self.RULE_cexp) try: - self.state = 57 + self.state = 63 self._errHandler.sync(self) token = self._input.LA(1) if token in [9]: + localctx = ANFParser.LetContext(self, localctx) self.enterOuterAlt(localctx, 1) - self.state = 49 + self.state = 55 self.match(ANFParser.T__8) - self.state = 50 + self.state = 56 self.match(ANFParser.IDENT) - self.state = 51 + self.state = 57 self.match(ANFParser.T__9) - self.state = 52 + self.state = 58 self.funcall() - self.state = 53 + self.state = 59 self.match(ANFParser.T__10) - self.state = 54 + self.state = 60 self.cexp() pass - elif token in [5, 12, 13]: + elif token in [1, 12, 13]: + localctx = ANFParser.FcContext(self, localctx) self.enterOuterAlt(localctx, 2) - self.state = 56 + self.state = 62 self.funcall() pass else: @@ -401,33 +634,5 @@ class ANFParser ( Parser ): - 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 index 982fa58..e1bf0b4 100644 --- a/gen/ANFVisitor.py +++ b/gen/ANFVisitor.py @@ -9,18 +9,58 @@ else: class ANFVisitor(ParseTreeVisitor): - # Visit a parse tree produced by ANFParser#aexp. - def visitAexp(self, ctx:ANFParser.AexpContext): + # Visit a parse tree produced by ANFParser#var. + def visitVar(self, ctx:ANFParser.VarContext): return self.visitChildren(ctx) - # Visit a parse tree produced by ANFParser#funcall. - def visitFuncall(self, ctx:ANFParser.FuncallContext): + # Visit a parse tree produced by ANFParser#num. + def visitNum(self, ctx:ANFParser.NumContext): return self.visitChildren(ctx) - # Visit a parse tree produced by ANFParser#cexp. - def visitCexp(self, ctx:ANFParser.CexpContext): + # Visit a parse tree produced by ANFParser#add. + def visitAdd(self, ctx:ANFParser.AddContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#sub. + def visitSub(self, ctx:ANFParser.SubContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#mul. + def visitMul(self, ctx:ANFParser.MulContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#div. + def visitDiv(self, ctx:ANFParser.DivContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#lam. + def visitLam(self, ctx:ANFParser.LamContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#call. + def visitCall(self, ctx:ANFParser.CallContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#atom. + def visitAtom(self, ctx:ANFParser.AtomContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#let. + def visitLet(self, ctx:ANFParser.LetContext): + return self.visitChildren(ctx) + + + # Visit a parse tree produced by ANFParser#fc. + def visitFc(self, ctx:ANFParser.FcContext): return self.visitChildren(ctx) diff --git a/grammar/ANF.g4 b/grammar/ANF.g4 index 95ab835..408439c 100644 --- a/grammar/ANF.g4 +++ b/grammar/ANF.g4 @@ -1,22 +1,22 @@ grammar ANF; aexp - : IDENT - | NUMBER - | aexp '+' aexp - | aexp '-' aexp - | aexp '*' aexp - | aexp '/' aexp - | '(' IDENT ':' cexp ')' + : IDENT #var + | NUMBER #num + | '(' aexp '+' aexp ')' #add + | '(' aexp '-' aexp ')' #sub + | '(' aexp '*' aexp ')' #mul + | '(' aexp '/' aexp ')' #div + | '(' IDENT ':' cexp ')' #lam ; funcall - : IDENT '(' aexp (',' aexp)* ')' - | aexp + : IDENT '(' aexp (',' aexp)* ')' #call + | aexp #atom ; cexp - : 'let' IDENT '=' funcall 'in' cexp - | funcall + : 'let' IDENT '=' funcall 'in' cexp #let + | funcall #fc ; IDENT: Letter (Letter | Digit)*; -- cgit 1.4.1-2-gfad0