本文整理汇总了C#中Antlr3.Tool.Grammar类的典型用法代码示例。如果您正苦于以下问题:C# Antlr3.Tool.Grammar类的具体用法?C# Antlr3.Tool.Grammar怎么用?C# Antlr3.Tool.Grammar使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Antlr3.Tool.Grammar类属于命名空间,在下文中一共展示了Antlr3.Tool.Grammar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestMismatchedSetError
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);
}
示例2: NFAConversionThread
public NFAConversionThread( Grammar grammar,
Barrier barrier,
int i,
int j )
{
this.grammar = grammar;
this.barrier = barrier;
this.i = i;
this.j = j;
}
示例3: TestCharOptional
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: TestA
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: ActionTranslator
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: TestActionInStarLoop
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: TestA
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 );
}
示例8: TestA
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 );
}
示例9: TestFiniteCommonLeftPrefixes
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 );
}
示例10: PerformGrammarAnalysis
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");
}
}
示例11: TestSets
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 );
}
示例12: Test2InsertMiddleIndex
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 );
}
示例13: TestRuleListLabelOfPositiveClosure
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 );
}
示例14: TestNakeRulePlusInLexer
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 );
}
示例15: TestNakedRuleOptional
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 );
}