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);
 }
 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;
 }
 public void Init( Grammar g )
 {
     this.grammar = g;
     this.generator = grammar.CodeGenerator;
     this.templates = generator.Templates;
 }
Exemple #4
0
        /** Create NFA, DFA and generate code for grammar.
         *  Create NFA for any delegates first.  Once all NFA are created,
         *  it's ok to create DFA, which must check for left-recursion.  That check
         *  is done by walking the full NFA, which therefore must be complete.
         *  After all NFA, comes DFA conversion for root grammar then code gen for
         *  root grammar.  DFA and code gen for delegates comes next.
         */
        protected virtual void GenerateRecognizer( Grammar grammar )
        {
            string language = (string)grammar.GetOption( "language" );
            if ( language != null )
            {
                CodeGenerator generator = new CodeGenerator( this, grammar, language );
                grammar.CodeGenerator = generator;
                generator.Debug = Debug;
                generator.Profile = Profile;
                generator.Trace = Trace;

                // generate NFA early in case of crash later (for debugging)
                if ( Generate_NFA_dot )
                {
                    GenerateNFAs( grammar );
                }

                // GENERATE CODE
                generator.GenRecognizer();

                if ( Generate_DFA_dot )
                {
                    GenerateDFAs( grammar );
                }

                IList<Grammar> delegates = grammar.GetDirectDelegates();
                for ( int i = 0; delegates != null && i < delegates.Count; i++ )
                {
                    Grammar @delegate = (Grammar)delegates[i];
                    if ( @delegate != grammar )
                    {
                        // already processing this one
                        GenerateRecognizer( @delegate );
                    }
                }
            }
        }
 public ActionTranslator( CodeGenerator generator,
                              string ruleName,
                              IToken actionToken,
                              int outerAltNum )
     : this(new ANTLRStringStream( actionToken.Text ))
 {
     this.generator = generator;
     grammar = generator.grammar;
     this.enclosingRule = grammar.GetRule( ruleName );
     this.actionToken = actionToken;
     this.outerAltNum = outerAltNum;
 }
 public ACyclicDFACodeGenerator( CodeGenerator parent )
 {
     this._parentGenerator = parent;
 }
Exemple #7
0
 // e.g., ".h"
 protected internal virtual void GenRecognizerHeaderFile( AntlrTool tool,
                                        CodeGenerator generator,
                                        Grammar grammar,
                                        StringTemplate headerFileST,
                                        string extName )
 {
     // no header file by default
 }
Exemple #8
0
        protected internal virtual void PerformGrammarAnalysis( CodeGenerator generator,
                                              Grammar grammar )
        {
            // Build NFAs from the grammar AST
            grammar.BuildNFA();

            // Create the DFA predictors for each decision
            grammar.CreateLookaheadDFAs();
        }
Exemple #9
0
 protected internal virtual void GenRecognizerFile( AntlrTool tool,
                                  CodeGenerator generator,
                                  Grammar grammar,
                                  StringTemplate outputFileST )
 {
     string fileName =
         generator.GetRecognizerFileName( grammar.name, grammar.type );
     generator.Write( outputFileST, fileName );
 }
Exemple #10
0
 /** Target must be able to override the labels used for token types */
 public virtual string GetTokenTypeAsTargetLabel( CodeGenerator generator, int ttype )
 {
     string name = generator.grammar.GetTokenDisplayName( ttype );
     // If name is a literal, return the token type instead
     if ( name[0] == '\'' )
     {
         return ttype.ToString(); //String.valueOf( ttype );
     }
     return name;
 }
Exemple #11
0
        /** Convert from an ANTLR string literal found in a grammar file to
         *  an equivalent string literal in the target language.  For Java, this
         *  is the translation 'a\n"' -> "a\n\"".  Expect single quotes
         *  around the incoming literal.  Just flip the quotes and replace
         *  double quotes with \"
         *
         *  Note that we have decided to allow poeple to use '\"' without
         *  penalty, so we must build the target string in a loop as Utils.replae
         *  cannot handle both \" and " without a lot of messing around.
         *
         */
        public virtual string GetTargetStringLiteralFromANTLRStringLiteral(
            CodeGenerator generator,
            string literal )
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder @is = new StringBuilder( literal );

            // Opening quote
            //
            sb.Append( '"' );

            for ( int i = 1; i < @is.Length - 1; i++ )
            {

                if ( @is[i] == '\\' )
                {

                    // Anything escaped is what it is! We assume that
                    // people know how to escape characters correctly. However
                    // we catch anything that does not need an escape in Java (which
                    // is what the default implementation is dealing with and remove
                    // the escape. The C target does this for instance.
                    //
                    switch ( @is[i + 1] )
                    {

                    // Pass through any escapes that Java also needs
                    //
                    case '"':
                    case 'n':
                    case 'r':
                    case 't':
                    case 'b':
                    case 'f':
                    case '\\':
                    case 'u':    // Assume unnnn

                        sb.Append( '\\' );    // Pass the escape through
                        break;

                    default:

                        // Remove the escape by virtue of not adding it here
                        // Thus \' becomes ' and so on
                        //
                        break;
                    }

                    // Go past the \ character
                    //
                    i++;

                }
                else
                {

                    // Chracters that don't need \ in ANTLR 'strings' but do in Java
                    //
                    if ( @is[i] == '"' )
                    {

                        // We need to escape " in Java
                        //
                        sb.Append( '\\' );
                    }
                }

                // Add in the next character, which may have been escaped
                //
                sb.Append( @is[i] );
            }

            // Append closing " and return
            //
            sb.Append( '"' );

            return sb.ToString();
        }
Exemple #12
0
        /** Convert from an ANTLR char literal found in a grammar file to
         *  an equivalent char literal in the target language.  For most
         *  languages, this means leaving 'x' as 'x'.  Actually, we need
         *  to escape '\u000A' so that it doesn't get converted to \n by
         *  the compiler.  Convert the literal to the char value and then
         *  to an appropriate target char literal.
         *
         *  Expect single quotes around the incoming literal.
         */
        public virtual string GetTargetCharLiteralFromANTLRCharLiteral(
            CodeGenerator generator,
            string literal )
        {
            StringBuilder buf = new StringBuilder();
            buf.Append( '\'' );
            int c = Grammar.GetCharValueFromGrammarCharLiteral( literal );
            if ( c < Label.MIN_CHAR_VALUE )
            {
                return "'\u0000'";
            }
            if ( c < targetCharValueEscape.Length &&
                 targetCharValueEscape[c] != null )
            {
                buf.Append( targetCharValueEscape[c] );
            }
            else if ( c <= 0x7f && !char.IsControl( (char)c ) )
            {
                // normal char
                buf.Append( (char)c );
            }
            else
            {
                // must be something unprintable...use \\uXXXX
                // turn on the bit above max "\\uFFFF" value so that we pad with zeros
                // then only take last 4 digits
                string hex = c.ToString( "X4" );
                buf.Append( "\\u" );
                buf.Append( hex );
            }

            buf.Append( '\'' );
            return buf.ToString();
        }
Exemple #13
0
 /** Some targets only support ASCII or 8-bit chars/strings.  For example,
  *  C++ will probably want to return 0xFF here.
  */
 public virtual int GetMaxCharValue( CodeGenerator generator )
 {
     return Label.MAX_CHAR_VALUE;
 }