public LabelElementPair(Grammar g, GrammarAST label, GrammarAST element, int labelOp) { this.label = label; this.element = element; // compute general case for label type if (element.GetFirstDescendantWithType(tokenTypeForTokens) != null) { if (labelOp == ANTLRParser.ASSIGN) type = LabelType.TOKEN_LABEL; else type = LabelType.TOKEN_LIST_LABEL; } else if (element.GetFirstDescendantWithType(ANTLRParser.RULE_REF) != null) { if (labelOp == ANTLRParser.ASSIGN) type = LabelType.RULE_LABEL; else type = LabelType.RULE_LIST_LABEL; } // now reset if lexer and string if (g.IsLexer()) { if (element.GetFirstDescendantWithType(ANTLRParser.STRING_LITERAL) != null) { if (labelOp == ANTLRParser.ASSIGN) type = LabelType.LEXER_STRING_LABEL; } } }
public virtual void GenerateATNs(Grammar g) { DOTGenerator dotGenerator = new DOTGenerator(g); IList<Grammar> grammars = new List<Grammar>(); grammars.Add(g); IList<Grammar> imported = g.GetAllImportedGrammars(); if (imported != null) { foreach (Grammar importedGrammar in imported) grammars.Add(importedGrammar); } foreach (Grammar ig in grammars) { foreach (Rule r in ig.rules.Values) { try { string dot = dotGenerator.GetDOT(g.atn.ruleToStartState[r.index], g.IsLexer()); if (dot != null) { WriteDOTFile(g, r, dot); } } catch (IOException ioe) { errMgr.ToolError(ErrorType.CANNOT_WRITE_FILE, ioe); } } } }
public virtual void CheckForModeConflicts(Grammar g) { if (g.IsLexer()) { LexerGrammar lexerGrammar = (LexerGrammar)g; foreach (string modeName in lexerGrammar.modes.Keys) { if (!modeName.Equals("DEFAULT_MODE") && reservedNames.Contains(modeName)) { Rule rule = lexerGrammar.modes[modeName].First(); g.tool.errMgr.GrammarError(ErrorType.MODE_CONFLICTS_WITH_COMMON_CONSTANTS, g.fileName, ((CommonTree)rule.ast.Parent).Token, modeName); } if (g.GetTokenType(modeName) != TokenConstants.InvalidType) { Rule rule = lexerGrammar.modes[modeName].First(); g.tool.errMgr.GrammarError(ErrorType.MODE_CONFLICTS_WITH_TOKEN, g.fileName, ((CommonTree)rule.ast.Parent).Token, modeName); } } } }
public virtual void ProcessNonCombinedGrammar(Grammar g, bool gencode) { if (g.ast == null || g.ast.hasErrors) return; if (internalOption_PrintGrammarTree) System.Console.WriteLine(g.ast.ToStringTree()); bool ruleFail = CheckForRuleIssues(g); if (ruleFail) return; int prevErrors = errMgr.GetNumErrors(); // MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT) SemanticPipeline sem = new SemanticPipeline(g); sem.Process(); if (errMgr.GetNumErrors() > prevErrors) return; // BUILD ATN FROM AST ATNFactory factory; if (g.IsLexer()) factory = new LexerATNFactory((LexerGrammar)g); else factory = new ParserATNFactory(g); g.atn = factory.CreateATN(); if (generate_ATN_dot) GenerateATNs(g); // PERFORM GRAMMAR ANALYSIS ON ATN: BUILD DECISION DFAs AnalysisPipeline anal = new AnalysisPipeline(g); anal.Process(); //if ( generate_DFA_dot ) generateDFAs(g); if (g.tool.GetNumErrors() > prevErrors) return; // GENERATE CODE if (gencode) { CodeGenPipeline gen = new CodeGenPipeline(g); gen.Process(); } }