public void TestMismatchedSetError()
        {
            Grammar pg = new Grammar(
                "parser grammar p;\n" +
                "prog : WHILE ID LCURLY (assign)* RCURLY;\n" +
                "assign : ID ASSIGN expr SEMI ;\n" +
                "expr : INT | FLOAT | ID ;\n");
            Grammar g = new Grammar();
            g.ImportTokenVocabulary(pg);
            g.FileName = "<string>";
            g.SetGrammarContent(
                "lexer grammar t;\n" +
                "WHILE : 'while';\n" +
                "LCURLY : '{';\n" +
                "RCURLY : '}';\n" +
                "ASSIGN : '=';\n" +
                "SEMI : ';';\n" +
                "ID : ('a'..'z')+ ;\n" +
                "INT : (DIGIT)+ ;\n" +
                "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n" +
                "fragment DIGIT : '0'..'9';\n" +
                "WS : (' ')+ ;\n");
            ICharStream input = new ANTLRStringStream("while x { i=; y=3.42; z=y; }");
            Interpreter lexEngine = new Interpreter(g, input);

            FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);
            tokens.SetTokenTypeChannel(g.GetTokenType("WS"), 99);
            //System.out.println("tokens="+tokens.toString());
            Interpreter parseEngine = new Interpreter(pg, tokens);
            ParseTree t = parseEngine.Parse("prog");
            string result = t.ToStringTree();
            string expecting =
                "(<grammar p> (prog while x { (assign i = (expr MismatchedSetException(10!={5,6,7})))))";
            Assert.AreEqual(expecting, result);
        }
 public NFAConversionThread( Grammar grammar,
                            Barrier barrier,
                            int i,
                            int j )
 {
     this.grammar = grammar;
     this.barrier = barrier;
     this.i = i;
     this.j = j;
 }
示例#3
0
 public void TestCharOptional()
 {
     Grammar g = new Grammar(
             "grammar P;\n" +
             "a : 'a'?;" );
     string expecting =
         "(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT 'a' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "a" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#4
0
 public void TestA()
 {
     Grammar g = new Grammar(
             "parser grammar P;\n" +
             "a : A;" );
     string expecting =
         "(rule a ARG RET scope (BLOCK (ALT A <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "a" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#5
0
 public ActionTranslator( CodeGenerator generator,
                              string ruleName,
                              GrammarAST actionAST )
     : this(new ANTLRStringStream( actionAST.Token.Text ))
 {
     this.generator = generator;
     this.grammar = generator.grammar;
     this.enclosingRule = grammar.GetLocallyDefinedRule( ruleName );
     this.actionToken = actionAST.Token;
     this.outerAltNum = actionAST.outerAltNum;
 }
示例#6
0
 public void TestActionInStarLoop()
 {
     Grammar g = new Grammar(
             "grammar Expr;\n" +
             "options { backtrack=true; }\n" +
             "a : ({blort} 'x')* ;\n" );  // bug: the synpred had nothing in it
     string expecting =
         "(rule synpred1_Expr ARG RET scope (BLOCK (ALT blort 'x' <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "synpred1_Expr" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#7
0
 public void TestFiniteCommonLeftPrefixes()
 {
     Grammar g = new Grammar(
             "lexer grammar t;\n" +
             "A : 'a' 'b' | 'a' 'c' | 'd' 'e' ;" );
     g.BuildNFA();
     g.CreateLookaheadDFAs( false );
     DFA dfa = g.GetLookaheadDFA( 1 );
     checkPrediction( dfa, "ab", 1 );
     checkPrediction( dfa, "ac", 2 );
     checkPrediction( dfa, "de", 3 );
     checkPrediction( dfa, "q", NFA.INVALID_ALT_NUMBER );
 }
示例#8
0
 public void TestA()
 {
     Grammar g = new Grammar(
         "parser grammar P;\n" +
         "a : A;" );
     string expecting =
         ".s0->.s1\n" +
         ".s1->.s2\n" +
         ".s2-A->.s3\n" +
         ".s3->:s4\n" +
         ":s4-EOF->.s5\n";
     checkRule( g, "a", expecting );
 }
示例#9
0
 public void TestA()
 {
     Grammar g = new Grammar(
         "parser grammar P;\n" +
         "a : A;" );
     string expecting =
         ".s0->.s1" + NewLine +
         ".s1->.s2" + NewLine +
         ".s2-A->.s3" + NewLine +
         ".s3->:s4" + NewLine +
         ":s4-EOF->.s5" + NewLine;
     checkRule( g, "a", expecting );
 }
示例#10
0
 public void TestSets()
 {
     Grammar g = new Grammar(
             "lexer grammar t;\n" +
             "A : {;}'a'..'z' | ';' | '0'..'9' ;" );
     g.BuildNFA();
     g.CreateLookaheadDFAs( false );
     DFA dfa = g.GetLookaheadDFA( 1 );
     checkPrediction( dfa, "a", 1 );
     checkPrediction( dfa, "q", 1 );
     checkPrediction( dfa, "z", 1 );
     checkPrediction( dfa, ";", 2 );
     checkPrediction( dfa, "9", 3 );
 }
示例#11
0
        protected override void PerformGrammarAnalysis(CodeGenerator generator, Grammar grammar)
        {
            base.PerformGrammarAnalysis(generator, grammar);

            foreach (Rule rule in grammar.Rules)
                rule.ThrowsSpec.Add("RecognitionException");

            IEnumerable<Rule> delegatedRules = grammar.GetDelegatedRules();
            if (delegatedRules != null)
            {
                foreach (Rule rule in delegatedRules)
                    rule.ThrowsSpec.Add("RecognitionException");
            }
        }
 public void Test2InsertMiddleIndex()
 {
     Grammar g = new Grammar(
         "lexer grammar t;\n" +
         "A : 'a';\n" +
         "B : 'b';\n" +
         "C : 'c';\n" );
     ICharStream input = new ANTLRStringStream( "abc" );
     Interpreter lexEngine = new Interpreter( g, input );
     TokenRewriteStream tokens = new TokenRewriteStream( lexEngine );
     tokens.Fill();
     tokens.InsertBefore( 1, "x" );
     tokens.InsertBefore( 1, "y" );
     string result = tokens.ToString();
     string expecting = "ayxbc";
     Assert.AreEqual( expecting, result );
 }
        public void TestCombine3Inserts() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar t;\n" +
                "A : 'a';\n" +
                "B : 'b';\n" +
                "C : 'c';\n");
            ICharStream        input     = new ANTLRStringStream("abc");
            Interpreter        lexEngine = new Interpreter(g, input);
            TokenRewriteStream tokens    = new TokenRewriteStream(lexEngine);

            tokens.Fill();
            tokens.InsertBefore(1, "x");
            tokens.InsertBefore(0, "y");
            tokens.InsertBefore(1, "z");
            string result    = tokens.ToString();
            string expecting = "yazxbc";

            assertEquals(expecting, result);
        }
示例#14
0
        public void TestNakedAstar() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : A*;");
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s2->.s3\n" +
                ".s2->.s9\n" +
                ".s3->.s4\n" +
                ".s4-A->.s5\n" +
                ".s5->.s3\n" +
                ".s5->.s6\n" +
                ".s6->:s7\n" +
                ".s9->.s6\n" +
                ":s7-EOF->.s8\n";

            checkRule(g, "a", expecting);
        }
示例#15
0
        public void TestNakedAstar() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : A*;");
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2->.s3" + NewLine +
                ".s2->.s9" + NewLine +
                ".s3->.s4" + NewLine +
                ".s4-A->.s5" + NewLine +
                ".s5->.s3" + NewLine +
                ".s5->.s6" + NewLine +
                ".s6->:s7" + NewLine +
                ".s9->.s6" + NewLine +
                ":s7-EOF->.s8" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#16
0
        /** Rewrite alt to have a synpred as first element;
         *  (xxx)=>xxx
         *  but only if they didn't specify one manually.
         */
        protected virtual void PrefixWithSynPred(GrammarAST alt)
        {
            // if they want backtracking and it's not a lexer rule in combined grammar
            string autoBacktrack = (string)Grammar.GetBlockOption(currentBlockAST, "backtrack");

            if (autoBacktrack == null)
            {
                autoBacktrack = (string)Grammar.GetOption("backtrack");
            }
            if (autoBacktrack != null && autoBacktrack.Equals("true") &&
                !(GrammarType == GrammarType.Combined &&
                  Rule.GetRuleType(currentRuleName) == RuleType.Lexer) &&
                alt.GetChild(0).Type != SYN_SEMPRED)
            {
                // duplicate alt and make a synpred block around that dup'd alt
                GrammarAST synpredBlockAST = CreateBlockFromDupAlt(alt);

                // Create a BACKTRACK_SEMPRED node as if user had typed this in
                // Effectively we replace (xxx)=>xxx with {synpredxxx}? xxx
                GrammarAST synpredAST = CreateSynSemPredFromBlock(synpredBlockAST,
                                                                  BACKTRACK_SEMPRED);

                // insert BACKTRACK_SEMPRED as first element of alt
                //synpredAST.getLastSibling().setNextSibling( alt.getFirstChild() );
                //synpredAST.addChild( alt.getFirstChild() );
                //alt.setFirstChild( synpredAST );
                GrammarAST[] children = alt.GetChildrenAsArray();
                adaptor.SetChild(alt, 0, synpredAST);
                for (int i = 0; i < children.Length; i++)
                {
                    if (i < children.Length - 1)
                    {
                        adaptor.SetChild(alt, i + 1, children[i]);
                    }
                    else
                    {
                        adaptor.AddChild(alt, children[i]);
                    }
                }
            }
        }
示例#17
0
        public void TestABorCD() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : A B | C D;");
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s1->.s8" + NewLine +
                ".s10-D->.s11" + NewLine +
                ".s11->.s5" + NewLine +
                ".s2-A->.s3" + NewLine +
                ".s3-B->.s4" + NewLine +
                ".s4->.s5" + NewLine +
                ".s5->:s6" + NewLine +
                ".s8->.s9" + NewLine +
                ".s9-C->.s10" + NewLine +
                ":s6-EOF->.s7" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#18
0
        public void TestbA() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : b A ;\n" +
                "b : B ;");
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s2->.s3\n" +
                ".s3->.s4\n" +
                ".s4->.s5\n" +
                ".s5-B->.s6\n" +
                ".s6->:s7\n" +
                ".s8-A->.s9\n" +
                ".s9->:s10\n" +
                ":s10-EOF->.s11\n" +
                ":s7->.s8\n";

            checkRule(g, "a", expecting);
        }
示例#19
0
        public void TestPredicatedAorB() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : {p1}? A | {p2}? B ;");
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s1->.s8" + NewLine +
                ".s10-B->.s11" + NewLine +
                ".s11->.s5" + NewLine +
                ".s2-{p1}?->.s3" + NewLine +
                ".s3-A->.s4" + NewLine +
                ".s4->.s5" + NewLine +
                ".s5->:s6" + NewLine +
                ".s8->.s9" + NewLine +
                ".s9-{p2}?->.s10" + NewLine +
                ":s6-EOF->.s7" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#20
0
        public void TestbA() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : b A ;\n" +
                "b : B ;");
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2->.s3" + NewLine +
                ".s3->.s4" + NewLine +
                ".s4->.s5" + NewLine +
                ".s5-B->.s6" + NewLine +
                ".s6->:s7" + NewLine +
                ".s8-A->.s9" + NewLine +
                ".s9->:s10" + NewLine +
                ":s10-EOF->.s11" + NewLine +
                ":s7->.s8" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#21
0
        public void TestLabeledNotSet() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "tokens { A; B; C; }\n" +
                "a : t=~A ;\n");
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2-B..C->.s3" + NewLine +
                ".s3->:s4" + NewLine +
                ":s4-EOF->.s5" + NewLine;

            checkRule(g, "a", expecting);

            string expectingGrammarStr =
                "1:8: parser grammar P;\n" +
                "a : t=~ A ;";

            Assert.AreEqual(expectingGrammarStr, g.ToString());
        }
示例#22
0
        public void TestAutoBacktracking_PlusBlock1Alt() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {backtrack=true;}\n" +
                "a : ('a')+ ;"
                );
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s2->.s3\n" +
                ".s3->.s4\n" +
                ".s4-{synpred1_t}?->.s5\n" +
                ".s5-'a'->.s6\n" +
                ".s6->.s3\n" +
                ".s6->.s7\n" +
                ".s7->:s8\n" +
                ":s8-EOF->.s9\n";

            checkRule(g, "a", expecting);
        }
示例#23
0
        public void TestRewriteRuleAndRewriteModeIgnoreActionsPredicates() /*throws Exception*/ {
            ErrorQueue equeue = new ErrorQueue();
            ErrorManager.SetErrorListener( equeue );
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a: {action} {action2} x=A -> {ick}\n" +
                " | {pred1}? y+=B -> {ick}\n" +
                " | C {action} -> {ick}\n" +
                " | {pred2}?=> z+=D -> {ick}\n" +
                " | (E)=> ^(F G) -> {ick}\n" +
                " ;\n"
            );
            AntlrTool antlr = newTool();
            antlr.SetOutputDirectory( null ); // write to /dev/null
            CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
示例#24
0
        public void TestABorCD() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : A B | C D;");
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s1->.s8\n" +
                ".s10-D->.s11\n" +
                ".s11->.s5\n" +
                ".s2-A->.s3\n" +
                ".s3-B->.s4\n" +
                ".s4->.s5\n" +
                ".s5->:s6\n" +
                ".s8->.s9\n" +
                ".s9-C->.s10\n" +
                ":s6-EOF->.s7\n";

            checkRule(g, "a", expecting);
        }
示例#25
0
        public void TestAutoBacktracking_OptionalBlock1Alt() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {backtrack=true;}\n" +
                "a : ('a')?;"
                );
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2->.s3" + NewLine +
                ".s2->.s9" + NewLine +
                ".s3-{synpred1_t}?->.s4" + NewLine +
                ".s4-'a'->.s5" + NewLine +
                ".s5->.s6" + NewLine +
                ".s6->:s7" + NewLine +
                ".s9->.s6" + NewLine +
                ":s7-EOF->.s8" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#26
0
        public void TestLabeledNotSet() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "tokens { A; B; C; }\n" +
                "a : t=~A ;\n");
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s2-B..C->.s3\n" +
                ".s3->:s4\n" +
                ":s4-EOF->.s5\n";

            checkRule(g, "a", expecting);

            string expectingGrammarStr =
                "1:8: parser grammar P;\n" +
                "a : t=~ A ;";

            assertEquals(expectingGrammarStr, g.ToString());
        }
示例#27
0
        public void TestPredicatedAorB() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "a : {p1}? A | {p2}? B ;");
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s1->.s8\n" +
                ".s10-B->.s11\n" +
                ".s11->.s5\n" +
                ".s2-{p1}?->.s3\n" +
                ".s3-A->.s4\n" +
                ".s4->.s5\n" +
                ".s5->:s6\n" +
                ".s8->.s9\n" +
                ".s9-{p2}?->.s10\n" +
                ":s6-EOF->.s7\n";

            checkRule(g, "a", expecting);
        }
示例#28
0
        public void TestLabeledNotBlockSet() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar P;\n" +
                "A : t=~('3'|'b') ;\n");
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2-{'\\u0000'..'2', '4'..'a', 'c'..'\\uFFFF'}->.s3" + NewLine +
                ".s3->:s4" + NewLine +
                ":s4-<EOT>->.s5" + NewLine;

            checkRule(g, "A", expecting);

            string expectingGrammarStr =
                "1:7: lexer grammar P;\n" +
                "A : t=~ ( '3' | 'b' ) ;\n" +
                "Tokens : A ;";

            Assert.AreEqual(expectingGrammarStr, g.ToString());
        }
示例#29
0
        public void TestLabeledNotCharSet() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar P;\n" +
                "A : t=~'3' ;\n");
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s2-{'\\u0000'..'2', '4'..'\\uFFFF'}->.s3\n" +
                ".s3->:s4\n" +
                ":s4-<EOT>->.s5\n";

            checkRule(g, "A", expecting);

            string expectingGrammarStr =
                "1:7: lexer grammar P;\n" +
                "A : t=~ '3' ;\n" +
                "Tokens : A ;";

            assertEquals(expectingGrammarStr, g.ToString());
        }
示例#30
0
        /** Given IList&lt;Label&gt;, return a String with a useful representation
         *  of the associated input string.  One could show something different
         *  for lexers and parsers, for example.
         */
        public virtual String GetInputSequenceDisplay(IList <Label> labels)
        {
            Grammar       g   = dfa.nfa.grammar;
            StringBuilder buf = new StringBuilder();

            foreach (Label label in labels)
            {
                buf.Append(label.ToString(g));
                if (/*it.hasNext() &&*/ g.type != GrammarType.Lexer)
                {
                    buf.Append(' ');
                }
            }

            // remove the final appended space
            if (g.type != GrammarType.Lexer)
            {
                buf.Length = buf.Length - 1;
            }

            return(buf.ToString());
        }
示例#31
0
        public void TestRewriteRuleAndRewriteModeNotSimple() /*throws Exception*/
        {
            ErrorQueue equeue = new ErrorQueue();

            ErrorManager.SetErrorListener(equeue);
            Grammar g = new Grammar(
                "tree grammar TP;\n" +
                "options {ASTLabelType=CommonTree; output=template; rewrite=true;}\n" +
                "a  : ID+ -> {ick}\n" +
                "   | INT INT -> {ick}\n" +
                "   ;\n"
                );
            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer();

            Assert.AreEqual(0, equeue.warnings.Count, "unexpected errors: " + equeue);
        }
示例#32
0
        public Set <Rule> GetImportedRulesSensitiveToOverriddenRulesDueToLOOK()
        {
            Set <String> diffFIRSTs = getOverriddenRulesWithDifferentFIRST();
            Set <Rule>   rules      = new HashSet();

            for (Iterator it = diffFIRSTs.iterator(); it.hasNext();)
            {
                String r = (String)it.next();
                for (int i = 0; i < delegates.size(); i++)
                {
                    Grammar    g       = delegates.get(i);
                    Set <Rule> callers = g.ruleSensitivity.get(r);
                    // somebody invokes rule whose FIRST changed in subgrammar?
                    if (callers != null)
                    {
                        rules.addAll(callers);
                        //[email protected](g.name+" rules "+callers+" sensitive to "+r+"; dup 'em");
                    }
                }
            }
            return(rules);
        }
示例#33
0
        public void TestNewlineLiterals() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar T;\n" +
                "A : '\\n\\n' ;\n"  // ANTLR sees '\n\n'
                );
            string expecting = "match(\"\\n\\n\")";

            AntlrTool antlr = newTool();

            antlr.SetOutputDirectory(null);   // write to /dev/null
            CodeGenerator generator = new CodeGenerator(antlr, g, "Java");

            g.CodeGenerator = generator;
            generator.GenRecognizer(); // codegen phase sets some vars we need
            StringTemplate codeST = generator.RecognizerST;
            string         code   = codeST.Render();
            int            m      = code.IndexOf("match(\"");
            string         found  = code.Substring(m, expecting.Length);

            Assert.AreEqual(expecting, found);
        }
示例#34
0
        public void TestMismatchedTokenError()
        {
            Assert.Inconclusive("May be failing on just my port...");
            Grammar pg = new Grammar(
                "parser grammar p;\n" +
                "prog : WHILE ID LCURLY (assign)* RCURLY;\n" +
                "assign : ID ASSIGN expr SEMI ;\n" +
                "expr : INT | FLOAT | ID ;\n");
            Grammar g = new Grammar();

            g.FileName = Grammar.IGNORE_STRING_IN_GRAMMAR_FILE_NAME + "string";
            g.ImportTokenVocabulary(pg);
            g.SetGrammarContent(
                "lexer grammar t;\n" +
                "WHILE : 'while';\n" +
                "LCURLY : '{';\n" +
                "RCURLY : '}';\n" +
                "ASSIGN : '=';\n" +
                "SEMI : ';';\n" +
                "ID : ('a'..'z')+ ;\n" +
                "INT : (DIGIT)+ ;\n" +
                "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n" +
                "fragment DIGIT : '0'..'9';\n" +
                "WS : (' ')+ ;\n");
            ICharStream input     = new ANTLRStringStream("while x { i=1 y=3.42; z=y; }");
            Interpreter lexEngine = new Interpreter(g, input);

            FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);

            tokens.SetTokenTypeChannel(g.GetTokenType("WS"), 99);
            //System.out.println("tokens="+tokens.toString());
            Interpreter parseEngine = new Interpreter(pg, tokens);
            ParseTree   t           = parseEngine.Parse("prog");
            string      result      = t.ToStringTree();
            string      expecting   =
                "(<grammar p> (prog while x { (assign i = (expr 1) MismatchedTokenException(5!=9))))";

            assertEquals(expecting, result);
        }
示例#35
0
        public void TestAutoBacktracking_StarSetBlock_IgnoresPreds() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {backtrack=true;}\n" +
                "a : ('a'|'b')* ;"
                );
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2->.s3" + NewLine +
                ".s2->.s9" + NewLine +
                ".s3->.s4" + NewLine +
                ".s4-'a'..'b'->.s5" + NewLine +
                ".s5->.s3" + NewLine +
                ".s5->.s6" + NewLine +
                ".s6->:s7" + NewLine +
                ".s9->.s6" + NewLine +
                ":s7-EOF->.s8" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#36
0
        public void TestAutoBacktracking_ExistingPred() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {backtrack=true;}\n" +
                "a : ('a')=> 'a' | 'b';"
                );
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s1->.s8" + NewLine +
                ".s10->.s5" + NewLine +
                ".s2-{synpred1_t}?->.s3" + NewLine +
                ".s3-'a'->.s4" + NewLine +
                ".s4->.s5" + NewLine +
                ".s5->:s6" + NewLine +
                ".s8->.s9" + NewLine +
                ".s9-'b'->.s10" + NewLine +
                ":s6-EOF->.s7" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#37
0
        public void TestNoViableAltError()
        {
            Grammar pg = new Grammar(
                "parser grammar p;\n" +
                "prog : WHILE ID LCURLY (assign)* RCURLY;\n" +
                "assign : ID ASSIGN expr SEMI ;\n" +
                "expr : {;}INT | FLOAT | ID ;\n");
            Grammar g = new Grammar();

            g.ImportTokenVocabulary(pg);
            g.FileName = "<string>";
            g.SetGrammarContent(
                "lexer grammar t;\n" +
                "WHILE : 'while';\n" +
                "LCURLY : '{';\n" +
                "RCURLY : '}';\n" +
                "ASSIGN : '=';\n" +
                "SEMI : ';';\n" +
                "ID : ('a'..'z')+ ;\n" +
                "INT : (DIGIT)+ ;\n" +
                "FLOAT : (DIGIT)+ '.' (DIGIT)* ;\n" +
                "fragment DIGIT : '0'..'9';\n" +
                "WS : (' ')+ ;\n");
            ICharStream input     = new ANTLRStringStream("while x { i=; y=3.42; z=y; }");
            Interpreter lexEngine = new Interpreter(g, input);

            FilteringTokenStream tokens = new FilteringTokenStream(lexEngine);

            tokens.SetTokenTypeChannel(g.GetTokenType("WS"), 99);
            //System.out.println("tokens="+tokens.toString());
            Interpreter parseEngine = new Interpreter(pg, tokens);
            ParseTree   t           = parseEngine.Parse("prog");
            string      result      = t.ToStringTree();
            string      expecting   =
                "(<grammar p> (prog while x { (assign i = (expr NoViableAltException(10@[4:1: expr : ( INT | FLOAT | ID );])))))";

            Assert.AreEqual(expecting, result);
        }
示例#38
0
        protected override void GenRecognizerFile(AntlrTool tool, CodeGenerator generator, Grammar grammar, Template outputFileST)
        {
            if (!grammar.IsRoot)
            {
                Grammar rootGrammar = grammar.composite.RootGrammar;
                string  actionScope = grammar.GetDefaultActionScope(grammar.type);
                IDictionary <string, object> actions;
                object rootNamespace;
                if (rootGrammar.Actions.TryGetValue(actionScope, out actions) && actions.TryGetValue("namespace", out rootNamespace))
                {
                    if (!grammar.Actions.TryGetValue(actionScope, out actions))
                    {
                        actions = new Dictionary <string, object>();
                        grammar.Actions[actionScope] = actions;
                    }

                    actions["namespace"] = rootNamespace;
                }
            }

            generator.Templates.RegisterRenderer(typeof(string), new StringRenderer(generator, this));
            base.GenRecognizerFile(tool, generator, grammar, outputFileST);
        }
示例#39
0
        public void TestRangeOrRange() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar P;\n" +
                "A : ('a'..'c' 'h' | 'q' 'j'..'l') ;"
                );
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s10-'q'->.s11\n" +
                ".s11-'j'..'l'->.s12\n" +
                ".s12->.s6\n" +
                ".s2->.s3\n" +
                ".s2->.s9\n" +
                ".s3-'a'..'c'->.s4\n" +
                ".s4-'h'->.s5\n" +
                ".s5->.s6\n" +
                ".s6->:s7\n" +
                ".s9->.s10\n" +
                ":s7-<EOT>->.s8\n";

            checkRule(g, "A", expecting);
        }
示例#40
0
        public void TestRangeOrRange() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "lexer grammar P;\n" +
                "A : ('a'..'c' 'h' | 'q' 'j'..'l') ;"
                );
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s10-'q'->.s11" + NewLine +
                ".s11-'j'..'l'->.s12" + NewLine +
                ".s12->.s6" + NewLine +
                ".s2->.s3" + NewLine +
                ".s2->.s9" + NewLine +
                ".s3-'a'..'c'->.s4" + NewLine +
                ".s4-'h'->.s5" + NewLine +
                ".s5->.s6" + NewLine +
                ".s6->:s7" + NewLine +
                ".s9->.s10" + NewLine +
                ":s7-<EOT>->.s8" + NewLine;

            checkRule(g, "A", expecting);
        }
示例#41
0
        public void TestAutoBacktracking_RuleBlock() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {backtrack=true;}\n" +
                "a : 'a'{;}|'b';"
                );
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s1->.s9" + NewLine +
                ".s10-'b'->.s11" + NewLine +
                ".s11->.s6" + NewLine +
                ".s2-{synpred1_t}?->.s3" + NewLine +
                ".s3-'a'->.s4" + NewLine +
                ".s4-{}->.s5" + NewLine +
                ".s5->.s6" + NewLine +
                ".s6->:s7" + NewLine +
                ".s9->.s10" + NewLine +
                ":s7-EOF->.s8" + NewLine;

            checkRule(g, "a", expecting);
        }
示例#42
0
        public void TestAutoBacktracking_RuleBlock() /*throws Exception*/
        {
            Grammar g = new Grammar(
                "grammar t;\n" +
                "options {backtrack=true;}\n" +
                "a : 'a'{;}|'b';"
                );
            string expecting =
                ".s0->.s1\n" +
                ".s1->.s2\n" +
                ".s1->.s9\n" +
                ".s10-'b'->.s11\n" +
                ".s11->.s6\n" +
                ".s2-{synpred1_t}?->.s3\n" +
                ".s3-'a'->.s4\n" +
                ".s4-{}->.s5\n" +
                ".s5->.s6\n" +
                ".s6->:s7\n" +
                ".s9->.s10\n" +
                ":s7-EOF->.s8\n";

            checkRule(g, "a", expecting);
        }
示例#43
0
 public void TestCharRangePlus()
 {
     Grammar g = new Grammar(
             "lexer grammar P;\n" +
             "ID : 'a'..'z'+;" );
     string expecting =
         "(rule ID ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (.. 'a' 'z') <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "ID" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#44
0
 public override string ToString( Grammar g )
 {
     return ToString();
 }
示例#45
0
        public void TestLabeledNotCharSet()
        {
            Grammar g = new Grammar(
                "lexer grammar P;\n" +
                "A : t=~'3' ;\n" );
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2-{'\\u0000'..'2', '4'..'\\uFFFF'}->.s3" + NewLine +
                ".s3->:s4" + NewLine +
                ":s4-<EOT>->.s5" + NewLine;
            checkRule( g, "A", expecting );

            string expectingGrammarStr =
                "1:7: lexer grammar P;\n" +
                "A : t=~ '3' ;\n" +
                "Tokens : A ;";
            Assert.AreEqual( expectingGrammarStr, g.ToString() );
        }
示例#46
0
 public void TestSetLabel()
 {
     Grammar g = new Grammar(
             "grammar P;\n" +
             "a : x=(A|B);\n" );
     string expecting =
         "(rule a ARG RET scope (BLOCK (ALT (= x (BLOCK (ALT A <end-of-alt>) (ALT B <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "a" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#47
0
 public void TestNakeRulePlusInLexer()
 {
     Grammar g = new Grammar(
             "lexer grammar P;\n" +
             "A : B+;\n" +
             "B : 'a';" );
     string expecting =
         "(rule A ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT B <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "A" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#48
0
 public void TestNakedAstar()
 {
     Grammar g = new Grammar(
         "parser grammar P;\n" +
         "a : A*;" );
     string expecting =
         ".s0->.s1" + NewLine +
         ".s1->.s2" + NewLine +
         ".s2->.s3" + NewLine +
         ".s2->.s9" + NewLine +
         ".s3->.s4" + NewLine +
         ".s4-A->.s5" + NewLine +
         ".s5->.s3" + NewLine +
         ".s5->.s6" + NewLine +
         ".s6->:s7" + NewLine +
         ".s9->.s6" + NewLine +
         ":s7-EOF->.s8" + NewLine;
     checkRule( g, "a", expecting );
 }
示例#49
0
文件: NFA.cs 项目: bszafko/antlrcs
 public NFA( Grammar g )
 {
     this.grammar = g;
 }
示例#50
0
 public void TestListLabelOfClosure2()
 {
     Grammar g = new Grammar(
             "grammar P;\n" +
             "a : x+='int'*;" );
     string expecting =
         "(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT (+= x 'int') <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "a" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#51
0
 public void TestNakedRuleOptional()
 {
     Grammar g = new Grammar(
             "parser grammar P;\n" +
             "a : b?;\n" +
             "b : B;" );
     string expecting =
         "(rule a ARG RET scope (BLOCK (ALT (? (BLOCK (ALT b <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "a" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#52
0
 public void TestNakedAoptional()
 {
     Grammar g = new Grammar(
         "parser grammar P;\n" +
         "a : A?;" );
     string expecting =
         ".s0->.s1" + NewLine +
         ".s1->.s2" + NewLine +
         ".s2->.s3" + NewLine +
         ".s2->.s8" + NewLine +
         ".s3-A->.s4" + NewLine +
         ".s4->.s5" + NewLine +
         ".s5->:s6" + NewLine +
         ".s8->.s5" + NewLine +
         ":s6-EOF->.s7" + NewLine;
     checkRule( g, "a", expecting );
 }
示例#53
0
 public void TestRuleListLabelOfPositiveClosure()
 {
     Grammar g = new Grammar(
             "grammar P;\n" +
             "options {output=AST;}\n" +
             "a : x+=b+;\n" +
             "b : ID;\n" );
     string expecting =
         "(rule a ARG RET scope (BLOCK (ALT (+ (BLOCK (ALT (+= x b) <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "a" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#54
0
 public void TestABorCD()
 {
     Grammar g = new Grammar(
         "parser grammar P;\n" +
         "a : A B | C D;" );
     string expecting =
         ".s0->.s1" + NewLine +
         ".s1->.s2" + NewLine +
         ".s1->.s8" + NewLine +
         ".s10-D->.s11" + NewLine +
         ".s11->.s5" + NewLine +
         ".s2-A->.s3" + NewLine +
         ".s3-B->.s4" + NewLine +
         ".s4->.s5" + NewLine +
         ".s5->:s6" + NewLine +
         ".s8->.s9" + NewLine +
         ".s9-C->.s10" + NewLine +
         ":s6-EOF->.s7" + NewLine;
     checkRule( g, "a", expecting );
 }
示例#55
0
 public void TestStringStar()
 {
     Grammar g = new Grammar(
             "grammar P;\n" +
             "a : 'while'*;" );
     string expecting =
         "(rule a ARG RET scope (BLOCK (ALT (* (BLOCK (ALT 'while' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "a" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#56
0
 public void TestNestedAstar()
 {
     Grammar g = new Grammar(
         "parser grammar P;\n" +
         "a : (A*)*;" );
     string expecting =
         ".s0->.s1" + NewLine +
         ".s1->.s2" + NewLine +
         ".s10->:s11" + NewLine +
         ".s13->.s8" + NewLine +
         ".s14->.s10" + NewLine +
         ".s2->.s14" + NewLine +
         ".s2->.s3" + NewLine +
         ".s3->.s4" + NewLine +
         ".s4->.s13" + NewLine +
         ".s4->.s5" + NewLine +
         ".s5->.s6" + NewLine +
         ".s6-A->.s7" + NewLine +
         ".s7->.s5" + NewLine +
         ".s7->.s8" + NewLine +
         ".s8->.s9" + NewLine +
         ".s9->.s10" + NewLine +
         ".s9->.s3" + NewLine +
         ":s11-EOF->.s12" + NewLine;
     checkRule( g, "a", expecting );
 }
示例#57
0
        public void TestLabeledNotSet()
        {
            Grammar g = new Grammar(
                "parser grammar P;\n" +
                "tokens { A; B; C; }\n" +
                "a : t=~A ;\n" );
            string expecting =
                ".s0->.s1" + NewLine +
                ".s1->.s2" + NewLine +
                ".s2-B..C->.s3" + NewLine +
                ".s3->:s4" + NewLine +
                ":s4-EOF->.s5" + NewLine;
            checkRule( g, "a", expecting );

            string expectingGrammarStr =
                "1:8: parser grammar P;\n" +
                "a : t=~ A ;";
            Assert.AreEqual( expectingGrammarStr, g.ToString() );
        }
示例#58
0
 public void Init( Grammar g )
 {
     this.grammar = g;
     this.generator = grammar.CodeGenerator;
     this.templates = generator.Templates;
 }
示例#59
0
 public void TestCharOptionalInLexer()
 {
     Grammar g = new Grammar(
             "lexer grammar P;\n" +
             "B : 'b'?;" );
     string expecting =
         "(rule B ARG RET scope (BLOCK (ALT (? (BLOCK (ALT 'b' <end-of-alt>) <end-of-block>)) <end-of-alt>) <end-of-block>) <end-of-rule>)";
     string found = g.GetRule( "B" ).tree.ToStringTree();
     assertEquals( expecting, found );
 }
示例#60
0
 public void TestMultiplePredicates()
 {
     Grammar g = new Grammar(
         "parser grammar P;\n" +
         "a : {p1}? {p1a}? A | {p2}? B | {p3} b;\n" +
         "b : {p4}? B ;" );
     string expecting =
         ".s0->.s1" + NewLine +
         ".s1->.s2" + NewLine +
         ".s1->.s9" + NewLine +
         ".s10-{p2}?->.s11" + NewLine +
         ".s11-B->.s12" + NewLine +
         ".s12->.s6" + NewLine +
         ".s13->.s14" + NewLine +
         ".s14-{}->.s15" + NewLine +
         ".s15->.s16" + NewLine +
         ".s16->.s17" + NewLine +
         ".s17->.s18" + NewLine +
         ".s18-{p4}?->.s19" + NewLine +
         ".s19-B->.s20" + NewLine +
         ".s2-{p1}?->.s3" + NewLine +
         ".s20->:s21" + NewLine +
         ".s22->.s6" + NewLine +
         ".s3-{p1a}?->.s4" + NewLine +
         ".s4-A->.s5" + NewLine +
         ".s5->.s6" + NewLine +
         ".s6->:s7" + NewLine +
         ".s9->.s10" + NewLine +
         ".s9->.s13" + NewLine +
         ":s21->.s22" + NewLine +
         ":s7-EOF->.s8" + NewLine;
     checkRule( g, "a", expecting );
 }