simplify test.

This commit is contained in:
Terence Parr 2012-07-21 16:27:00 -07:00
parent 7a4a269615
commit 9539572ee7
3 changed files with 36 additions and 21 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);