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; grammar T;
s : stat ;
stat: expr[0] NEWLINE
| ID '=' expr[0] NEWLINE
| NEWLINE
;
s : ID b ; expr[int _p]
b : INT ; : ( INT
| ID
| '(' expr[0] ')'
)
( {5 >= $_p}? ('*'|'/') expr[6]
| {4 >= $_p}? ('+'|'-') expr[5]
)*
;
ID : [a-zA-Z]+ ; /*
INT : [0-9]+ ; expr: expr ('*'|'/') expr # MulDiv
WS : [ \t\n\r]+ -> skip ; | 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.CharStream;
import org.antlr.v4.runtime.CommonTokenFactory; import org.antlr.v4.runtime.CommonTokenFactory;
import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.UnbufferedCharStream; import org.antlr.v4.runtime.UnbufferedCharStream;
@ -23,8 +24,9 @@ public class TestT {
CommonTokenStream tokens = new CommonTokenStream(lex); CommonTokenStream tokens = new CommonTokenStream(lex);
TParser parser = new TParser(tokens); TParser parser = new TParser(tokens);
ParserRuleContext tree = parser.s(); parser.addErrorListener(new DiagnosticErrorListener());
tree.save(parser, "/tmp/t.ps");
ParserRuleContext tree = parser.s();
// tree.save(parser, "/tmp/t.ps");
} }
} }

View File

@ -297,31 +297,19 @@ public class TestLeftRecursion extends BaseTest {
@Test @Test
public void testAmbigLR() throws Exception { public void testAmbigLR() throws Exception {
String grammar = String grammar =
"// START: g\n" +
"grammar Expr;\n" + "grammar Expr;\n" +
"// END: g\n" +
"\n" +
"// START:stat\n" +
"prog: stat ;\n" + "prog: stat ;\n" +
"\n" +
"stat: expr NEWLINE # printExpr\n" + "stat: expr NEWLINE # printExpr\n" +
" | ID '=' expr NEWLINE # assign\n" + " | ID '=' expr NEWLINE # assign\n" +
" | NEWLINE # blank\n" + " | NEWLINE # blank\n" +
" ;\n" + " ;\n" +
"// END:stat\n" +
"\n" +
"// START:expr\n" +
"expr: expr ('*'|'/') expr # MulDiv\n" + "expr: expr ('*'|'/') expr # MulDiv\n" +
" | expr ('+'|'-') expr # AddSub\n" + " | expr ('+'|'-') expr # AddSub\n" +
" | INT # int\n" + " | INT # int\n" +
" | ID # id\n" + " | ID # id\n" +
" | '(' expr ')' # parens\n" + " | '(' expr ')' # parens\n" +
" ;\n" + " ;\n" +
"// END:expr\n" +
"\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" + "MUL : '*' ; // assigns token name to '*' used above in grammar\n" +
"DIV : '/' ;\n" + "DIV : '/' ;\n" +
"ADD : '+' ;\n" + "ADD : '+' ;\n" +
@ -329,8 +317,7 @@ public class TestLeftRecursion extends BaseTest {
"ID : [a-zA-Z]+ ; // match identifiers\n" + "ID : [a-zA-Z]+ ; // match identifiers\n" +
"INT : [0-9]+ ; // match integers\n" + "INT : [0-9]+ ; // match integers\n" +
"NEWLINE:'\\r'? '\\n' ; // return newlines to parser (is end-statement signal)\n" + "NEWLINE:'\\r'? '\\n' ; // return newlines to parser (is end-statement signal)\n" +
"WS : [ \\t]+ -> skip ; // toss out whitespace\n" + "WS : [ \\t]+ -> skip ; // toss out whitespace\n";
"// END:tokens\n";
String result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "1\n", true); String result = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "prog", "1\n", true);
assertNull(stderrDuringParse); 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. * Related to https://github.com/antlr/antlr4/issues/41. EOF is
* https://github.com/antlr/antlr4/issues/41 * not viable after "if x" since EOF not viable after stat.
*/ */
@Test @Test
public void testOptional() throws Exception { public void testOptional() throws Exception {
String grammar = String grammar =
"grammar T;\n" + "grammar T;\n" +
"s : a | 'x';\n" + "stat : ifstat | 'x';\n" +
"a : 'a' s ('b' s)?;\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); assertEquals("", found);
assertNull(this.stderrDuringParse); 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); assertEquals("", found);
assertNull(this.stderrDuringParse); 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); 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); 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 { public void testIfIfElse() throws Exception {
String grammar = String grammar =
"grammar T;\n" + "grammar T;\n" +
"s : stmt EOF ;\n" +
"stmt : ifStmt | ID;\n" + "stmt : ifStmt | ID;\n" +
"ifStmt : 'if' ID stmt ('else' stmt | {_input.LA(1) != ELSE}?);\n" + "ifStmt : 'if' ID stmt ('else' stmt | {_input.LA(1) != ELSE}?);\n" +
"ELSE : 'else';\n" + "ELSE : 'else';\n" +
@ -207,11 +209,12 @@ public class TestParserExec extends BaseTest {
"WS : (' ' | '\\t')+ -> skip;\n" "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); "if x if x a else b", true);
String expecting = ""; String expecting = "";
assertEquals(expecting, found); assertEquals(expecting, found);
assertNull(this.stderrDuringParse); assertEquals("line 1:12 reportAttemptingFullContext d=1, input='else'\n",
this.stderrDuringParse);
} }
} }