简体   繁体   English

决策可以使用多种选择匹配输入,例如“ {'A'..'Z','_','a'..'z'}”:1、3

[英]Decision can match input such as “{'A'..'Z', '_', 'a'..'z'}” using multiple alternatives: 1, 3

I am a startbie for this antlr 3.5. 我是这个antlr 3.5的初学者。 I understood that left recursion is accepted in ant;r 4.0 and not in 3.5, I am getting ambigious error warning for my grammar . 我知道在ant; r 4.0中接受左递归,而在3.5中则不接受,我的语法收到错误的错误警告。 I am just verifying my email using this grammar, can some one fix this grammar 我只是使用这种语法验证我的电子邮件,有人可以修复此语法吗

        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:'.';

I assume that your problem is in your rule: Identifier . 我认为您的问题出在您的规则中: Identifier If I were you, I would do something like: 如果我是你,我会做类似的事情:

Identifier : ID (ID |Digit)*; 

fragment ID : ('a'..'z' | 'A'..'Z' | '_');

I hope this would help you. 希望对您有帮助。 ;) ;)

I am having two different grammar file and i am trying to use combined grammar for different abstraction. 我有两个不同的语法文件,并且我尝试将组合语法用于不同的抽象。 My code is as follows HelloWorldParser.g parser grammar HelloWorldParser; 我的代码如下: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;

and HelloWorldLexer.g 和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';

My combined grammar is Test.g 我的组合语法是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;

My LexerParserGenerator is : 我的LexerParserGenerator是:

package com.nuwaza.aqua.antlr.generator; 包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();

                }

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM