TestFullContextParsing for Java is working

This commit is contained in:
Sam Harwell 2015-01-26 19:53:09 -06:00
parent 4fa9ed5a6b
commit 99dcf93206
27 changed files with 396 additions and 390 deletions

View File

@ -0,0 +1,30 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "abc"
Rule() ::= "s"
Output() ::= <<
Decision 0:
s0-ID->:s1^=>1<\n>
>>
Errors() ::= <<
line 1:0 reportAttemptingFullContext d=0 (s), input='abc'<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
s @after {<DumpDFA()>}
: ID | ID {} ;
ID : 'a'..'z'+;
WS : (' '|'\t'|'\n')+ -> skip ;
>>

View File

@ -0,0 +1,39 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "a@"
Rule() ::= "prog"
Output() ::= <<
alt 1<\n>
>>
Errors() ::= <<
line 1:2 reportAttemptingFullContext d=0 (prog), input='a@'
line 1:2 reportAmbiguity d=0 (prog): ambigAlts={1, 2}, input='a@'
line 1:2 reportAttemptingFullContext d=1 (expr), input='a@'
line 1:2 reportContextSensitivity d=1 (expr), input='a@'<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
prog
@init {<LL_EXACT_AMBIG_DETECTION()>}
: expr expr {<writeln("\"alt 1\"")>}
| expr
;
expr: '@'
| ID '@'
| ID
;
ID : [a-z]+ ;
WS : [ \r\n\t]+ -> skip ;
>>

View File

@ -1,3 +1,16 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Rule() ::= "s"
grammar(grammarName) ::= <<
grammar <grammarName>;
s @after {<DumpDFA()>}
: '$' a | '@' b ;
@ -7,3 +20,4 @@ e : INT | ;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\t'|'\n')+ -> skip ;
>>

View File

@ -0,0 +1,39 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "$ 34 abc @ 34 abc"
Rule() ::= "s"
Output() ::= <<
Decision 2:
s0-INT->s1
s1-ID->:s2^=>1<\n>
>>
Errors() ::= <<
line 1:5 reportAttemptingFullContext d=2 (e), input='34abc'
line 1:2 reportContextSensitivity d=2 (e), input='34'
line 1:14 reportAttemptingFullContext d=2 (e), input='34abc'
line 1:14 reportContextSensitivity d=2 (e), input='34abc'<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
s @after {<DumpDFA()>}
: ('$' a | '@' b)+ ;
a : e ID ;
b : e INT ID ;
e : INT | ;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\t'|'\n')+ -> skip ;
>>

View File

@ -0,0 +1,14 @@
import "CtxSensitiveDFA.stg"
Input() ::= "$ 34 abc"
Output() ::= <<
Decision 1:
s0-INT->s1
s1-ID->:s2^=>1<\n>
>>
Errors() ::= <<
line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'
line 1:2 reportContextSensitivity d=1 (e), input='34'<\n>
>>

View File

@ -0,0 +1,14 @@
import "CtxSensitiveDFA.stg"
Input() ::= "@ 34 abc"
Output() ::= <<
Decision 1:
s0-INT->s1
s1-ID->:s2^=>1<\n>
>>
Errors() ::= <<
line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'
line 1:5 reportContextSensitivity d=1 (e), input='34abc'<\n>
>>

View File

@ -1,3 +1,16 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Rule() ::= "s"
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {<LL_EXACT_AMBIG_DETECTION()>}
@ -11,3 +24,5 @@ s
;
ID : [a-zA-Z]+ ;
WS : [ \r\n\t]+ -> skip ;
>>

View File

@ -0,0 +1,12 @@
import "ExprAmbiguity.stg"
Input() ::= "a+b"
Output() ::= <<
(expr a + (expr b))<\n>
>>
Errors() ::= <<
line 1:1 reportAttemptingFullContext d=1 (expr), input='+'
line 1:2 reportContextSensitivity d=1 (expr), input='+b'<\n>
>>

View File

@ -0,0 +1,14 @@
import "ExprAmbiguity.stg"
Input() ::= "a+b*c"
Output() ::= <<
(expr a + (expr b * (expr c)))<\n>
>>
Errors() ::= <<
line 1:1 reportAttemptingFullContext d=1 (expr), input='+'
line 1:2 reportContextSensitivity d=1 (expr), input='+b'
line 1:3 reportAttemptingFullContext d=1 (expr), input='*'
line 1:5 reportAmbiguity d=1 (expr): ambigAlts={1, 2}, input='*c'<\n>
>>

View File

@ -1,3 +1,16 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Rule() ::= "s"
grammar(grammarName) ::= <<
grammar <grammarName>;
s
@init {<LL_EXACT_AMBIG_DETECTION()>}
@ -8,3 +21,4 @@ stat: 'if' ID 'then' stat ('else' ID)?
;
ID : 'a'..'z'+ ;
WS : (' '|'\t'|'\n')+ -> skip ;
>>

View File

@ -0,0 +1,10 @@
import "FullContextIF_THEN_ELSEParse.stg"
Input() ::= "{ if x then return }"
Output() ::= <<
Decision 1:
s0-'}'->:s1=>2<\n>
>>
Errors() ::= ""

View File

@ -0,0 +1,13 @@
import "FullContextIF_THEN_ELSEParse.stg"
Input() ::= "{ if x then return else foo }"
Output() ::= <<
Decision 1:
s0-'else'->:s1^=>1<\n>
>>
Errors() ::= <<
line 1:19 reportAttemptingFullContext d=1 (stat), input='else'
line 1:19 reportContextSensitivity d=1 (stat), input='else'<\n>
>>

View File

@ -0,0 +1,14 @@
import "FullContextIF_THEN_ELSEParse.stg"
Input() ::= "{ if x then if y then return else foo }"
Output() ::= <<
Decision 1:
s0-'}'->:s2=>2
s0-'else'->:s1^=>1<\n>
>>
Errors() ::= <<
line 1:29 reportAttemptingFullContext d=1 (stat), input='else'
line 1:38 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'<\n>
>>

View File

@ -0,0 +1,19 @@
import "FullContextIF_THEN_ELSEParse.stg"
// should not be ambiguous because the second 'else bar' clearly
// indicates that the first else should match to the innermost if.
// LL_EXACT_AMBIG_DETECTION makes us keep going to resolve
Input() ::= "{ if x then if y then return else foo else bar }"
Output() ::= <<
Decision 1:
s0-'else'->:s1^=>1<\n>
>>
Errors() ::= <<
line 1:29 reportAttemptingFullContext d=1 (stat), input='else'
line 1:38 reportContextSensitivity d=1 (stat), input='elsefooelse'
line 1:38 reportAttemptingFullContext d=1 (stat), input='else'
line 1:38 reportContextSensitivity d=1 (stat), input='else'<\n>
>>

View File

@ -0,0 +1,19 @@
import "FullContextIF_THEN_ELSEParse.stg"
Input() ::= <<
{ if x then return else foo
if x then if y then return else foo }
>>
Output() ::= <<
Decision 1:
s0-'}'->:s2=>2
s0-'else'->:s1^=>1<\n>
>>
Errors() ::= <<
line 1:19 reportAttemptingFullContext d=1 (stat), input='else'
line 1:19 reportContextSensitivity d=1 (stat), input='else'
line 2:27 reportAttemptingFullContext d=1 (stat), input='else'
line 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'<\n>
>>

View File

@ -0,0 +1,19 @@
import "FullContextIF_THEN_ELSEParse.stg"
Input() ::= <<
{ if x then return else foo
if x then if y then return else foo }
>>
Output() ::= <<
Decision 1:
s0-'}'->:s2=>2
s0-'else'->:s1^=>1<\n>
>>
Errors() ::= <<
line 1:19 reportAttemptingFullContext d=1 (stat), input='else'
line 1:19 reportContextSensitivity d=1 (stat), input='else'
line 2:27 reportAttemptingFullContext d=1 (stat), input='else'
line 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'<\n>
>>

View File

@ -0,0 +1,20 @@
//TestFolders ::= [
//]
TestTemplates ::= [
"AmbigYieldsCtxSensitiveDFA": [],
"CtxSensitiveDFA_1": [],
"CtxSensitiveDFA_2": [],
"CtxSensitiveDFATwoDiffInput": [],
"SLLSeesEOFInLLGrammar": [],
"FullContextIF_THEN_ELSEParse_1": [],
"FullContextIF_THEN_ELSEParse_2": [],
"FullContextIF_THEN_ELSEParse_3": [],
"FullContextIF_THEN_ELSEParse_4": [],
"FullContextIF_THEN_ELSEParse_5": [],
"FullContextIF_THEN_ELSEParse_6": [],
"LoopsSimulateTailRecursion": [],
"AmbiguityNoLoop": [],
"ExprAmbiguity_1": [],
"ExprAmbiguity_2": []
]

View File

@ -0,0 +1,40 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "a(i)\<-x"
Rule() ::= "prog"
Output() ::= <<
pass: a(i)\<-x<\n>
>>
Errors() ::= <<
line 1:3 reportAttemptingFullContext d=3 (expr_primary), input='a(i)'
line 1:7 reportAmbiguity d=3 (expr_primary): ambigAlts={2, 3}, input='a(i)\<-x'<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
prog
@init {<LL_EXACT_AMBIG_DETECTION()>}
: expr_or_assign*;
expr_or_assign
: expr '++' {<writeln("\"fail.\"")>}
| expr {<writeln("\"pass: \"+$expr.text")>}
;
expr: expr_primary ('\<-' ID)?;
expr_primary
: '(' ID ')'
| ID '(' ID ')'
| ID
;
ID : [a-z]+ ;
>>

View File

@ -0,0 +1,36 @@
TestType() ::= "Parser"
Options ::= [
"Debug": true
]
Grammar ::= [
"T": {<grammar("T")>}
]
Input() ::= "34 abc"
Rule() ::= "s"
Output() ::= <<
Decision 0:
s0-INT->s1
s1-ID->:s2^=>1<\n>
>>
Errors() ::= <<
line 1:3 reportAttemptingFullContext d=0 (e), input='34abc'
line 1:0 reportContextSensitivity d=0 (e), input='34'<\n>
>>
grammar(grammarName) ::= <<
grammar <grammarName>;
s @after {<DumpDFA()>}
: a;
a : e ID ;
b : e INT ID ;
e : INT | ;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\t'|'\n')+ -> skip ;
>>

View File

@ -1,5 +1,6 @@
TestFolders ::= [
"CompositeLexers": [],
"FullContextParsing": [],
"LeftRecursion": [],
"LexerErrors": [],
"LexerExec": [],

View File

@ -151,123 +151,9 @@ public class Generator {
private Collection<JUnitTestFile> buildTests() throws Exception {
List<JUnitTestFile> list = new ArrayList<JUnitTestFile>();
list.add(buildCompositeParsers());
list.add(buildFullContextParsing());
return list;
}
private JUnitTestFile buildFullContextParsing() throws Exception {
JUnitTestFile file = new JUnitTestFile("FullContextParsing");
JUnitTestMethod tm = file.addParserTest(input, "AmbigYieldsCtxSensitiveDFA", "T", "s", "abc",
"Decision 0:\n" +
"s0-ID->:s1^=>1\n",
"line 1:0 reportAttemptingFullContext d=0 (s), input='abc'\n");
tm.debug = true;
tm = file.addParserTestsWithErrors(input, "CtxSensitiveDFA", "T", "s",
"$ 34 abc",
"Decision 1:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n",
"line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\n" +
"line 1:2 reportContextSensitivity d=1 (e), input='34'\n",
"@ 34 abc",
"Decision 1:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n",
"line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\n" +
"line 1:5 reportContextSensitivity d=1 (e), input='34abc'\n");
tm.debug = true;
tm = file.addParserTest(input, "CtxSensitiveDFATwoDiffInput", "T", "s",
"$ 34 abc @ 34 abc",
"Decision 2:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n",
"line 1:5 reportAttemptingFullContext d=2 (e), input='34abc'\n" +
"line 1:2 reportContextSensitivity d=2 (e), input='34'\n" +
"line 1:14 reportAttemptingFullContext d=2 (e), input='34abc'\n" +
"line 1:14 reportContextSensitivity d=2 (e), input='34abc'\n");
tm.debug = true;
tm = file.addParserTest(input, "SLLSeesEOFInLLGrammar", "T", "s",
"34 abc",
"Decision 0:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n",
"line 1:3 reportAttemptingFullContext d=0 (e), input='34abc'\n" +
"line 1:0 reportContextSensitivity d=0 (e), input='34'\n");
tm.debug = true;
tm = file.addParserTestsWithErrors(input, "FullContextIF_THEN_ELSEParse", "T", "s",
"{ if x then return }",
"Decision 1:\n" +
"s0-'}'->:s1=>2\n",
null,
"{ if x then return else foo }",
"Decision 1:\n" +
"s0-'else'->:s1^=>1\n",
"line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:19 reportContextSensitivity d=1 (stat), input='else'\n",
"{ if x then if y then return else foo }",
"Decision 1:\n" +
"s0-'}'->:s2=>2\n" +
"s0-'else'->:s1^=>1\n",
"line 1:29 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:38 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n",
// should not be ambiguous because the second 'else bar' clearly
// indicates that the first else should match to the innermost if.
// LL_EXACT_AMBIG_DETECTION makes us keep going to resolve
"{ if x then if y then return else foo else bar }",
"Decision 1:\n" +
"s0-'else'->:s1^=>1\n",
"line 1:29 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:38 reportContextSensitivity d=1 (stat), input='elsefooelse'\n" +
"line 1:38 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:38 reportContextSensitivity d=1 (stat), input='else'\n",
"{ if x then return else foo\n" +
"if x then if y then return else foo }",
"Decision 1:\n" +
"s0-'}'->:s2=>2\n" +
"s0-'else'->:s1^=>1\n",
"line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:19 reportContextSensitivity d=1 (stat), input='else'\n" +
"line 2:27 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n",
"{ if x then return else foo\n" +
"if x then if y then return else foo }",
"Decision 1:\n" +
"s0-'}'->:s2=>2\n" +
"s0-'else'->:s1^=>1\n",
"line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:19 reportContextSensitivity d=1 (stat), input='else'\n" +
"line 2:27 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n");
tm.debug = true;
tm = file.addParserTest(input, "LoopsSimulateTailRecursion", "T", "prog",
"a(i)<-x",
"pass: a(i)<-x\n",
"line 1:3 reportAttemptingFullContext d=3 (expr_primary), input='a(i)'\n" +
"line 1:7 reportAmbiguity d=3 (expr_primary): ambigAlts={2, 3}, input='a(i)<-x'\n");
tm.debug = true;
tm = file.addParserTest(input, "AmbiguityNoLoop", "T", "prog",
"a@",
"alt 1\n",
"line 1:2 reportAttemptingFullContext d=0 (prog), input='a@'\n" +
"line 1:2 reportAmbiguity d=0 (prog): ambigAlts={1, 2}, input='a@'\n" +
"line 1:2 reportAttemptingFullContext d=1 (expr), input='a@'\n" +
"line 1:2 reportContextSensitivity d=1 (expr), input='a@'\n");
tm.debug = true;
tm = file.addParserTestsWithErrors(input, "ExprAmbiguity", "T", "s",
"a+b",
"(expr a + (expr b))\n",
"line 1:1 reportAttemptingFullContext d=1 (expr), input='+'\n" +
"line 1:2 reportContextSensitivity d=1 (expr), input='+b'\n",
"a+b*c",
"(expr a + (expr b * (expr c)))\n",
"line 1:1 reportAttemptingFullContext d=1 (expr), input='+'\n" +
"line 1:2 reportContextSensitivity d=1 (expr), input='+b'\n" +
"line 1:3 reportAttemptingFullContext d=1 (expr), input='*'\n" +
"line 1:5 reportAmbiguity d=1 (expr): ambigAlts={1, 2}, input='*c'\n");
tm.debug = true;
return file;
}
private JUnitTestFile buildCompositeParsers() throws Exception {
JUnitTestFile file = new JUnitTestFile("CompositeParsers");
file.importErrorQueue = true;

View File

@ -1,5 +0,0 @@
grammar <grammarName>;
s @after {<DumpDFA()>}
: ID | ID {} ;
ID : 'a'..'z'+;
WS : (' '|'\t'|'\n')+ -> skip ;

View File

@ -1,12 +0,0 @@
grammar <grammarName>;
prog
@init {<LL_EXACT_AMBIG_DETECTION()>}
: expr expr {<writeln("\"alt 1\"")>}
| expr
;
expr: '@'
| ID '@'
| ID
;
ID : [a-z]+ ;
WS : [ \r\n\t]+ -> skip ;

View File

@ -1,9 +0,0 @@
grammar <grammarName>;
s @after {<DumpDFA()>}
: ('$' a | '@' b)+ ;
a : e ID ;
b : e INT ID ;
e : INT | ;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\t'|'\n')+ -> skip ;

View File

@ -1,15 +0,0 @@
grammar <grammarName>;
prog
@init {<LL_EXACT_AMBIG_DETECTION()>}
: expr_or_assign*;
expr_or_assign
: expr '++' {<writeln("\"fail.\"")>}
| expr {<writeln("\"pass: \"+$expr.text")>}
;
expr: expr_primary ('\<-' ID)?;
expr_primary
: '(' ID ')'
| ID '(' ID ')'
| ID
;
ID : [a-z]+ ;

View File

@ -1,9 +0,0 @@
grammar <grammarName>;
s @after {<DumpDFA()>}
: a;
a : e ID ;
b : e INT ID ;
e : INT | ;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\t'|'\n')+ -> skip ;

View File

@ -1,226 +0,0 @@
package org.antlr.v4.test.rt.java;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestFullContextParsing extends BaseTest {
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testAmbigYieldsCtxSensitiveDFA() throws Exception {
String grammar = "grammar T;\n" +
"s @after {this.dumpDFA();}\n" +
" : ID | ID {} ;\n" +
"ID : 'a'..'z'+;\n" +
"WS : (' '|'\\t'|'\\n')+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "abc", true);
assertEquals("Decision 0:\ns0-ID->:s1^=>1\n", found);
assertEquals("line 1:0 reportAttemptingFullContext d=0 (s), input='abc'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
String testCtxSensitiveDFA(String input) throws Exception {
String grammar = "grammar T;\n" +
"s @after {this.dumpDFA();}\n" +
" : '$' a | '@' b ;\n" +
"a : e ID ;\n" +
"b : e INT ID ;\n" +
"e : INT | ;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+ ;\n" +
"WS : (' '|'\\t'|'\\n')+ -> skip ;";
return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testCtxSensitiveDFA_1() throws Exception {
String found = testCtxSensitiveDFA("$ 34 abc");
assertEquals("Decision 1:\ns0-INT->s1\ns1-ID->:s2^=>1\n", found);
assertEquals("line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\nline 1:2 reportContextSensitivity d=1 (e), input='34'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testCtxSensitiveDFA_2() throws Exception {
String found = testCtxSensitiveDFA("@ 34 abc");
assertEquals("Decision 1:\ns0-INT->s1\ns1-ID->:s2^=>1\n", found);
assertEquals("line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\nline 1:5 reportContextSensitivity d=1 (e), input='34abc'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testCtxSensitiveDFATwoDiffInput() throws Exception {
String grammar = "grammar T;\n" +
"s @after {this.dumpDFA();}\n" +
" : ('$' a | '@' b)+ ;\n" +
"a : e ID ;\n" +
"b : e INT ID ;\n" +
"e : INT | ;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+ ;\n" +
"WS : (' '|'\\t'|'\\n')+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "$ 34 abc @ 34 abc", true);
assertEquals("Decision 2:\ns0-INT->s1\ns1-ID->:s2^=>1\n", found);
assertEquals("line 1:5 reportAttemptingFullContext d=2 (e), input='34abc'\nline 1:2 reportContextSensitivity d=2 (e), input='34'\nline 1:14 reportAttemptingFullContext d=2 (e), input='34abc'\nline 1:14 reportContextSensitivity d=2 (e), input='34abc'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testSLLSeesEOFInLLGrammar() throws Exception {
String grammar = "grammar T;\n" +
"s @after {this.dumpDFA();}\n" +
" : a;\n" +
"a : e ID ;\n" +
"b : e INT ID ;\n" +
"e : INT | ;\n" +
"ID : 'a'..'z'+ ;\n" +
"INT : '0'..'9'+ ;\n" +
"WS : (' '|'\\t'|'\\n')+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "34 abc", true);
assertEquals("Decision 0:\ns0-INT->s1\ns1-ID->:s2^=>1\n", found);
assertEquals("line 1:3 reportAttemptingFullContext d=0 (e), input='34abc'\nline 1:0 reportContextSensitivity d=0 (e), input='34'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
String testFullContextIF_THEN_ELSEParse(String input) throws Exception {
String grammar = "grammar T;\n" +
"s \n" +
"@init {_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);}\n" +
"@after {this.dumpDFA();}\n" +
" : '{' stat* '}' ;\n" +
"stat: 'if' ID 'then' stat ('else' ID)?\n" +
" | 'return'\n" +
" ;\n" +
"ID : 'a'..'z'+ ;\n" +
"WS : (' '|'\\t'|'\\n')+ -> skip ;";
return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testFullContextIF_THEN_ELSEParse_1() throws Exception {
String found = testFullContextIF_THEN_ELSEParse("{ if x then return }");
assertEquals("Decision 1:\ns0-'}'->:s1=>2\n", found);
assertNull(this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testFullContextIF_THEN_ELSEParse_2() throws Exception {
String found = testFullContextIF_THEN_ELSEParse("{ if x then return else foo }");
assertEquals("Decision 1:\ns0-'else'->:s1^=>1\n", found);
assertEquals("line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\nline 1:19 reportContextSensitivity d=1 (stat), input='else'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testFullContextIF_THEN_ELSEParse_3() throws Exception {
String found = testFullContextIF_THEN_ELSEParse("{ if x then if y then return else foo }");
assertEquals("Decision 1:\ns0-'}'->:s2=>2\ns0-'else'->:s1^=>1\n", found);
assertEquals("line 1:29 reportAttemptingFullContext d=1 (stat), input='else'\nline 1:38 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testFullContextIF_THEN_ELSEParse_4() throws Exception {
String found = testFullContextIF_THEN_ELSEParse("{ if x then if y then return else foo else bar }");
assertEquals("Decision 1:\ns0-'else'->:s1^=>1\n", found);
assertEquals("line 1:29 reportAttemptingFullContext d=1 (stat), input='else'\nline 1:38 reportContextSensitivity d=1 (stat), input='elsefooelse'\nline 1:38 reportAttemptingFullContext d=1 (stat), input='else'\nline 1:38 reportContextSensitivity d=1 (stat), input='else'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testFullContextIF_THEN_ELSEParse_5() throws Exception {
String found = testFullContextIF_THEN_ELSEParse("{ if x then return else foo\nif x then if y then return else foo }");
assertEquals("Decision 1:\ns0-'}'->:s2=>2\ns0-'else'->:s1^=>1\n", found);
assertEquals("line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\nline 1:19 reportContextSensitivity d=1 (stat), input='else'\nline 2:27 reportAttemptingFullContext d=1 (stat), input='else'\nline 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testFullContextIF_THEN_ELSEParse_6() throws Exception {
String found = testFullContextIF_THEN_ELSEParse("{ if x then return else foo\nif x then if y then return else foo }");
assertEquals("Decision 1:\ns0-'}'->:s2=>2\ns0-'else'->:s1^=>1\n", found);
assertEquals("line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\nline 1:19 reportContextSensitivity d=1 (stat), input='else'\nline 2:27 reportAttemptingFullContext d=1 (stat), input='else'\nline 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testLoopsSimulateTailRecursion() throws Exception {
String grammar = "grammar T;\n" +
"prog\n" +
"@init {_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);}\n" +
" : expr_or_assign*;\n" +
"expr_or_assign\n" +
" : expr '++' {System.out.println(\"fail.\");}\n" +
" | expr {System.out.println(\"pass: \"+$expr.text);}\n" +
" ;\n" +
"expr: expr_primary ('<-' ID)?;\n" +
"expr_primary\n" +
" : '(' ID ')'\n" +
" | ID '(' ID ')'\n" +
" | ID\n" +
" ;\n" +
"ID : [a-z]+ ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "prog", "a(i)<-x", true);
assertEquals("pass: a(i)<-x\n", found);
assertEquals("line 1:3 reportAttemptingFullContext d=3 (expr_primary), input='a(i)'\nline 1:7 reportAmbiguity d=3 (expr_primary): ambigAlts={2, 3}, input='a(i)<-x'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testAmbiguityNoLoop() throws Exception {
String grammar = "grammar T;\n" +
"prog\n" +
"@init {_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);}\n" +
" : expr expr {System.out.println(\"alt 1\");}\n" +
" | expr\n" +
" ;\n" +
"expr: '@'\n" +
" | ID '@'\n" +
" | ID\n" +
" ;\n" +
"ID : [a-z]+ ;\n" +
"WS : [ \\r\\n\\t]+ -> skip ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "prog", "a@", true);
assertEquals("alt 1\n", found);
assertEquals("line 1:2 reportAttemptingFullContext d=0 (prog), input='a@'\nline 1:2 reportAmbiguity d=0 (prog): ambigAlts={1, 2}, input='a@'\nline 1:2 reportAttemptingFullContext d=1 (expr), input='a@'\nline 1:2 reportContextSensitivity d=1 (expr), input='a@'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
String testExprAmbiguity(String input) throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {_interp.setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);}\n" +
": expr[0] {System.out.println($expr.ctx.toStringTree(this));};\n" +
" expr[int _p]\n" +
" : ID \n" +
" ( \n" +
" {5 >= $_p}? '*' expr[6]\n" +
" | {4 >= $_p}? '+' expr[5]\n" +
" )*\n" +
" ;\n" +
"ID : [a-zA-Z]+ ;\n" +
"WS : [ \\r\\n\\t]+ -> skip ;";
return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testExprAmbiguity_1() throws Exception {
String found = testExprAmbiguity("a+b");
assertEquals("(expr a + (expr b))\n", found);
assertEquals("line 1:1 reportAttemptingFullContext d=1 (expr), input='+'\nline 1:2 reportContextSensitivity d=1 (expr), input='+b'\n", this.stderrDuringParse);
}
/* this file and method are generated, any edit will be overwritten by the next generation */
@Test
public void testExprAmbiguity_2() throws Exception {
String found = testExprAmbiguity("a+b*c");
assertEquals("(expr a + (expr b * (expr c)))\n", found);
assertEquals("line 1:1 reportAttemptingFullContext d=1 (expr), input='+'\nline 1:2 reportContextSensitivity d=1 (expr), input='+b'\nline 1:3 reportAttemptingFullContext d=1 (expr), input='*'\nline 1:5 reportAmbiguity d=1 (expr): ambigAlts={1, 2}, input='*c'\n", this.stderrDuringParse);
}
}