0

私はこの antlr 3.5 の初心者です。左再帰が ant;r 4.0 では受け入れられ、3.5 では受け入れられないことを理解しました。文法に対してあいまいなエラー警告が表示されます。この文法を使用してメールを確認しているだけです。誰かがこの文法を修正できますか

        grammar HelloWorld;

        options
        {
          // antlr will generate java lexer and parser
          language = Java;
          // generated parser should create abstract syntax tree
          output = AST;
          backtrack = true;
        }

        //as the generated lexer will reside in com.nuwaza.aqua.antlr
        //package, we have to add package declaration on top of it
        @lexer::header {
          package com.nuwaza.aqua.antlr;
        }

        //as the generated parser will reside in org.meri.antlr_step_by_step.parsers 
        //package, we have to add package declaration on top of it
        @parser::header {
          package com.nuwaza.aqua.antlr;
        }



        // ***************** parser rules:
        //our grammar accepts only salutation followed by an end symbol
        expression : EmailId At Domain Dot Web EOF;


        // ***************** lexer rules:
        //the grammar must contain at least one lexer rule
        EmailId: (Domain)+;   
        At : '@';
        Domain:(Identifier)+;
        Dot: DotOperator;
        Web:(Identifier)+|(DotOperator)+|(Identifier)+;
        /*Space
          :
          (
            ' '
            | '\t'
            | '\r'
            | '\n'
            | '\u000C'
          )

           {
            skip();
           }
          ;*/
        Identifier
          :
          (
            'a'..'z'
            | 'A'..'Z'
            | '_'
          )
          (
            'a'..'z'
            | 'A'..'Z'
            | '_'
            | Digit
          )*
          ; 
          fragment
        Digit
          :
          '0'..'9'
          ;

          fragment DotOperator:'.';
4

2 に答える 2

0

2 つの異なる文法ファイルがあり、異なる抽象化のために組み合わせた文法を使用しようとしています。私のコードは次のとおりです HelloWorldParser.g パーサー文法 HelloWorldParser;

                options
                {
                  // antlr will generate java lexer and parser
                  language = Java;
                  // generated parser should create abstract syntax tree
                  output = AST;
                }




                //as the generated parser will reside in org.meri.antlr_step_by_step.parsers 
                //package, we have to add package declaration on top of it



                // ***************** parser rules:
                //our grammar accepts only salutation followed by an end symbol
                expression1
                :
                Hello World EOF;

および HelloWorldLexer.g

        lexer grammar HelloWorldLexer;



        //as the generated lexer will reside in com.nuwaza.aqua.antlr
        //package, we have to add package declaration on top of it

        //as the generated parser will reside in org.meri.antlr_step_by_step.parsers 
        //package, we have to add package declaration on top of it




        // ***************** lexer rules:

        Hello: 'Hello';
        World: 'World';

私の結合された文法は Test.g です

                grammar Test;



                options
                {
                  // antlr will generate java lexer and parser
                  language = Java;
                  // generated parser should create abstract syntax tree
                  output = AST;
                }

                import HelloWorldLexer, HelloWorldParser;


                @lexer::header {
                  package com.nuwaza.aqua.antlr;
                }
                @parser::header {
                  package com.nuwaza.aqua.antlr;
                }


                // ***************** parser rules:
                //our grammar accepts only salutation followed by an end symbol
                expression:expression1;

私の LexerParserGenerator は次のとおりです。

パッケージ com.nuwaza.aqua.antlr.generator;

            import org.antlr.Tool;


            public class LexerParserGenerator {



                private static final String OUTPUT_DIRECTORY_KEY = "-o";

                public static void main(String[] args) {

                        //provide the grammar ( .g file) residing path
                        String grammarPath = "./src/main/resources/grammar/Test.g";
                        //Specify the path with which grammar has to be generated.
                        String outputPath = "./src/main/java/com/nuwaza/aqua/antlr/";
                        Tool tool = new Tool(new String[] { grammarPath, OUTPUT_DIRECTORY_KEY,
                                outputPath });
                        tool.process();

                }

            }
于 2014-10-16T09:12:18.603 に答える