Merge branch 'master' into stack-graphs-integration

This commit is contained in:
Terence Parr 2012-07-21 16:27:36 -07:00
commit 93945d47f5
4 changed files with 51 additions and 33 deletions

View File

@ -1,8 +1,34 @@
grammar T;
s : stat ;
stat: expr[0] NEWLINE
| ID '=' expr[0] NEWLINE
| NEWLINE
;
s : ID b ;
b : INT ;
expr[int _p]
: ( INT
| ID
| '(' expr[0] ')'
)
( {5 >= $_p}? ('*'|'/') expr[6]
| {4 >= $_p}? ('+'|'-') expr[5]
)*
;
ID : [a-zA-Z]+ ;
INT : [0-9]+ ;
WS : [ \t\n\r]+ -> skip ;
/*
expr: expr ('*'|'/') expr # MulDiv
| expr ('+'|'-') expr # AddSub
| INT # int
| ID # id
| '(' expr ')' # parens
;
*/
MUL : '*' ; // assigns token name to '*' used above in grammar
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
ID : [a-zA-Z]+ ; // match identifiers
INT : [0-9]+ ; // match integers
NEWLINE:'\r'? '\n' ; // return newlines to parser (is end-statement signal)
WS : [ \t]+ -> skip ; // toss out whitespace

View File

@ -1,6 +1,7 @@
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.UnbufferedCharStream;
@ -23,8 +24,9 @@ public class TestT {
CommonTokenStream tokens = new CommonTokenStream(lex);
TParser parser = new TParser(tokens);
ParserRuleContext tree = parser.s();
tree.save(parser, "/tmp/t.ps");
parser.addErrorListener(new DiagnosticErrorListener());
ParserRuleContext tree = parser.s();
// tree.save(parser, "/tmp/t.ps");
}
}

View File

@ -297,31 +297,19 @@ public class TestLeftRecursion extends BaseTest {
@Test
public void testAmbigLR() throws Exception {
String grammar =
"// START: g\n" +
"grammar Expr;\n" +
"// END: g\n" +
"\n" +
"// START:stat\n" +
"prog: stat ;\n" +
"\n" +
"stat: expr NEWLINE # printExpr\n" +
" | ID '=' expr NEWLINE # assign\n" +
" | NEWLINE # blank\n" +
" ;\n" +
"// END:stat\n" +
"\n" +
"// START:expr\n" +
"expr: expr ('*'|'/') expr # MulDiv\n" +
" | expr ('+'|'-') expr # AddSub\n" +
" | INT # int\n" +
" | ID # id\n" +
" | '(' expr ')' # parens\n" +
" ;\n" +
"// END:expr\n" +
"\n" +
"// show marginal cost of adding a clear/wipe command for memory\n" +
"\n" +
"// START:tokens\n" +
"MUL : '*' ; // assigns token name to '*' used above in grammar\n" +
"DIV : '/' ;\n" +
"ADD : '+' ;\n" +
@ -329,8 +317,7 @@ public class TestLeftRecursion extends BaseTest {
"ID : [a-zA-Z]+ ; // match identifiers\n" +
"INT : [0-9]+ ; // match integers\n" +
"NEWLINE:'\\r'? '\\n' ; // return newlines to parser (is end-statement signal)\n" +
"WS : [ \\t]+ -> skip ; // toss out whitespace\n" +
"// END:tokens\n";
"WS : [ \\t]+ -> skip ; // toss out whitespace\n";
String result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "1\n", true);
assertNull(stderrDuringParse);

View File

@ -164,32 +164,33 @@ public class TestParserExec extends BaseTest {
/**
* This test is meant to detect regressions of bug antlr/antlr4#41.
* https://github.com/antlr/antlr4/issues/41
* Related to https://github.com/antlr/antlr4/issues/41. EOF is
* not viable after "if x" since EOF not viable after stat.
*/
@Test
public void testOptional() throws Exception {
String grammar =
"grammar T;\n" +
"s : a | 'x';\n" +
"a : 'a' s ('b' s)?;\n"
"stat : ifstat | 'x';\n" +
"ifstat : 'if' stat ('else' stat)?;\n" +
"WS : [ \\n\\t]+ -> skip ;"
;
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", false);
String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "x", false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "axbx", false);
found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x else x", false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "ax", false);
found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x", false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
assertEquals("line 1:4 no viable alternative at input '<EOF>'\n", this.stderrDuringParse);
found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "aaxbx", false);
found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if if x else x", false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
assertEquals("line 1:14 no viable alternative at input '<EOF>'\n", this.stderrDuringParse);
}
/**
@ -200,6 +201,7 @@ public class TestParserExec extends BaseTest {
public void testIfIfElse() throws Exception {
String grammar =
"grammar T;\n" +
"s : stmt EOF ;\n" +
"stmt : ifStmt | ID;\n" +
"ifStmt : 'if' ID stmt ('else' stmt | {_input.LA(1) != ELSE}?);\n" +
"ELSE : 'else';\n" +
@ -207,11 +209,12 @@ public class TestParserExec extends BaseTest {
"WS : (' ' | '\\t')+ -> skip;\n"
;
String found = execParser("T.g4", grammar, "TParser", "TLexer", "stmt",
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s",
"if x if x a else b", true);
String expecting = "";
assertEquals(expecting, found);
assertNull(this.stderrDuringParse);
assertEquals("line 1:12 reportAttemptingFullContext d=1, input='else'\n",
this.stderrDuringParse);
}
}