本文整理汇总了C#中Antlr3.Tool.Grammar类的典型用法代码示例。如果您正苦于以下问题:C# Grammar类的具体用法?C# Grammar怎么用?C# Grammar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Grammar类属于Antlr3.Tool命名空间,在下文中一共展示了Grammar类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GrammarSemanticsMessage
public GrammarSemanticsMessage( int msgID,
Grammar g,
IToken offendingToken,
object arg )
: this(msgID, g, offendingToken, arg, null)
{
}
示例2: GrammarSyntaxMessage
public GrammarSyntaxMessage( int msgID,
Grammar grammar,
IToken offendingToken,
RecognitionException exception )
: this(msgID, grammar, offendingToken, null, exception)
{
}
示例3: TestMessageStringificationIsConsistent
public void TestMessageStringificationIsConsistent()
{
string action = "$other.tree = null;";
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar a;\n" +
"options { output = AST;}" +
"otherrule\n" +
" : 'y' ;" +
"rule\n" +
" : other=otherrule {" + action + "}\n" +
" ;" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
ActionTranslator translator = new ActionTranslator( generator,
"rule",
new CommonToken( ANTLRParser.ACTION, action ), 1 );
string rawTranslation =
translator.Translate();
int expectedMsgID = ErrorManager.MSG_WRITE_TO_READONLY_ATTR;
object expectedArg = "other";
object expectedArg2 = "tree";
GrammarSemanticsMessage expectedMessage =
new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg, expectedArg2 );
string expectedMessageString = expectedMessage.ToString();
Assert.AreEqual( expectedMessageString, expectedMessage.ToString() );
}
示例4: TreeToNFAConverter
public TreeToNFAConverter( Grammar g, NFA nfa, NFAFactory factory, ITreeNodeStream input )
: this(input)
{
this.grammar = g;
this.nfa = nfa;
this.factory = factory;
}
示例5: TestCompleteBuffer
public void TestCompleteBuffer()
{
Grammar g = new Grammar(
"lexer grammar t;\n" +
"ID : 'a'..'z'+;\n" +
"INT : '0'..'9'+;\n" +
"SEMI : ';';\n" +
"ASSIGN : '=';\n" +
"PLUS : '+';\n" +
"MULT : '*';\n" +
"WS : ' '+;\n");
// Tokens: 012345678901234567
// Input: x = 3 * 0 + 2 * 0;
ICharStream input = new ANTLRStringStream("x = 3 * 0 + 2 * 0;");
Interpreter lexEngine = new Interpreter(g, input);
BufferedTokenStream tokens = new BufferedTokenStream(lexEngine);
int i = 1;
IToken t = tokens.LT(i);
while (t.Type != CharStreamConstants.EndOfFile)
{
i++;
t = tokens.LT(i);
}
tokens.LT(i++); // push it past end
tokens.LT(i++);
string result = tokens.ToString();
string expecting = "x = 3 * 0 + 2 * 0;";
Assert.AreEqual(expecting, result);
}
示例6: TestCannotHaveSpaceAfterDot
public void TestCannotHaveSpaceAfterDot()
{
string action = "%x. y = z;";
//String expecting = null;
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
Grammar g = new Grammar(
"grammar t;\n" +
"options {\n" +
" output=template;\n" +
"}\n" +
"\n" +
"a : ID {" + action + "}\n" +
" ;\n" +
"\n" +
"ID : 'a';\n" );
AntlrTool antlr = newTool();
CodeGenerator generator = new CodeGenerator( antlr, g, "Java" );
g.CodeGenerator = generator;
generator.GenRecognizer(); // forces load of templates
int expectedMsgID = ErrorManager.MSG_INVALID_TEMPLATE_ACTION;
object expectedArg = "%x.";
GrammarSemanticsMessage expectedMessage =
new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
checkError( equeue, expectedMessage );
}
示例7: DgmlGenerator
public DgmlGenerator(Grammar grammar)
{
if (grammar == null)
throw new ArgumentNullException("grammar");
_grammar = grammar;
}
示例8: ActionAnalysisLexer
public ActionAnalysisLexer(Grammar grammar, string ruleName, GrammarAST actionAST)
: this(new ANTLRStringStream(actionAST.Token.Text))
{
this.grammar = grammar;
this.enclosingRule = grammar.GetLocallyDefinedRule(ruleName);
this.actionToken = actionAST.Token;
this.outerAltNum = actionAST.outerAltNum;
}
示例9: TestA
public void TestA()
{
Grammar g = new Grammar(
"parser grammar t;\n" +
"a : A C | B;" );
string expecting =
".s0-A->:s1=>1" + NewLine +
".s0-B->:s2=>2" + NewLine;
checkDecision( g, 1, expecting, null, null, null, null, 0 );
}
示例10: LeftRecursiveRuleAnalyzer
public LeftRecursiveRuleAnalyzer(ITreeNodeStream input, Grammar g, string ruleName)
: base(input)
{
this.g = g;
this.ruleName = ruleName;
language = (string)g.GetOption("language");
generator = new CodeGenerator(g.Tool, g, language);
generator.LoadTemplates(language);
recRuleTemplates = LoadPrecRuleTemplates(g.Tool);
}
示例11: TestAB_or_AC
public void TestAB_or_AC()
{
Grammar g = new Grammar(
"parser grammar t;\n" +
"a : A B | A C;" );
string expecting =
".s0-A->.s1" + NewLine +
".s1-B->:s2=>1" + NewLine +
".s1-C->:s3=>2" + NewLine;
checkDecision( g, 1, expecting, null, null, null, null, 0 );
}
示例12: TestAndPredicates
public void TestAndPredicates()
{
Grammar g = new Grammar(
"parser grammar P;\n" +
"a : {p1}? {p1a}? A | {p2}? A ;" );
string expecting =
".s0-A->.s1" + NewLine +
".s1-{(p1&&p1a)}?->:s2=>1" + NewLine +
".s1-{p2}?->:s3=>2" + NewLine;
checkDecision( g, 1, expecting, null, null, null, null, null, 0, false );
}
示例13: TestAdjacentNotCharLoops
public void TestAdjacentNotCharLoops()
{
Grammar g = new Grammar(
"lexer grammar t;\n" +
"A : (~'r')+ ;\n" +
"B : (~'s')+ ;\n" );
string expecting =
".s0-'r'->:s3=>2" + NewLine +
".s0-'s'->:s2=>1" + NewLine +
".s0-{'\\u0000'..'q', 't'..'\\uFFFF'}->.s1" + NewLine +
".s1-'r'->:s3=>2" + NewLine +
".s1-<EOT>->:s2=>1" + NewLine +
".s1-{'\\u0000'..'q', 't'..'\\uFFFF'}->.s1" + NewLine;
checkDecision( g, 3, expecting, null );
}
示例14: TestBadGrammarOption
public void TestBadGrammarOption()
{
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue ); // unique listener per thread
AntlrTool antlr = newTool();
Grammar g = new Grammar( antlr,
"grammar t;\n" +
"options {foo=3; language=Java;}\n" +
"a : 'a';\n" );
object expectedArg = "foo";
int expectedMsgID = ErrorManager.MSG_ILLEGAL_OPTION;
GrammarSemanticsMessage expectedMessage =
new GrammarSemanticsMessage( expectedMsgID, g, null, expectedArg );
checkGrammarSemanticsError( equeue, expectedMessage );
}
示例15: Test3LevelImport
public void Test3LevelImport()
{
ErrorQueue equeue = new ErrorQueue();
ErrorManager.SetErrorListener( equeue );
string slave =
"parser grammar T;\n" +
"a : T ;\n";
mkdir( tmpdir );
writeFile( tmpdir, "T.g", slave );
string slave2 =
"parser grammar S;\n" + // A, B, C token type order
"import T;\n" +
"a : S ;\n";
mkdir( tmpdir );
writeFile( tmpdir, "S.g", slave2 );
string master =
"grammar M;\n" +
"import S;\n" +
"a : M ;\n";
writeFile( tmpdir, "M.g", master );
AntlrTool antlr = newTool( new string[] { "-lib", tmpdir } );
CompositeGrammar composite = new CompositeGrammar();
Grammar g = new Grammar( antlr, tmpdir + "/M.g", composite );
composite.SetDelegationRoot( g );
g.ParseAndBuildAST();
g.composite.AssignTokenTypes();
g.composite.DefineGrammarSymbols();
string expectedTokenIDToTypeMap = "[M=6, S=5, T=4]";
string expectedStringLiteralToTypeMap = "{}";
string expectedTypeToTokenList = "[T, S, M]";
assertEquals( expectedTokenIDToTypeMap,
realElements( g.composite.tokenIDToTypeMap ).ToElementString() );
assertEquals( expectedStringLiteralToTypeMap, g.composite.stringLiteralToTypeMap.ToElementString() );
assertEquals( expectedTypeToTokenList,
realElements( g.composite.typeToTokenList ).ToElementString() );
assertEquals( "unexpected errors: " + equeue, 0, equeue.errors.Count );
bool ok =
rawGenerateAndBuildRecognizer( "M.g", master, "MParser", null, false );
bool expecting = true; // should be ok
assertEquals( expecting, ok );
}