zoukankan      html  css  js  c++  java
  • Understanding ANTLR Grammar Files

    Are you confused by all the different sections of an Antlr grammar file, wondering what each one does ? Well lets take another look at them, this time using Antlr Studio.

    Believe it or not but Antlr grammar files are built to be as close to your java source files as possible. What, you don't see any similarities between your java files and antlr grammars? Well, let me show you...

    Grammar


     

    Everything you put in the header section of the grammar file is placed right on top of all the java files generated by Antlr. Think of this at the absolute top of your java files, you generally use this section to put the package definition. You can even place some imports that are common in all the generated files.

    The action section below that is unique for every grammar you specify in the file. This is placed right before the actual class specification. For example here we want to import the ArrayList and MyClass for the CalcParser only.

    Then we have the grammar specification, which even looks like a class declaration.

    Below that is the options section , where you can specify the options for the grammar. You can press Ctrl+Space in Antlr Studio to see which options are available.

    The tokens section is used to specify 'imaginary' tokens which are not declared in the lexer. These are generally used in TreeParsers to specify 'imaginary nodes.

    Another action section. This one puts its stuff 'inside' the class definition. You generally use it to define some custom methods for your parser.

    RULES

    A rule definition inside an antlr grammar corresponds to a method definition in the generated java file.

    As you can see , here again we can do everything with a rule , that can be done with a function.We can specify arguments for the rule ,as shown above (int a), even a return value and the exceptions thrown by the rule.

    The options section allows us to specify some rule specific options.

    We can specify custom exception handlers in the exception section.

    Java and all Java-based trademarks and logos are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States, other countries, or both.

  • 相关阅读:
    更新自己知识的网站
    CRM IFD 部署在同一台服务器上遇到的错误
    Dynamics CRM 修改Excel 最大导出记录限制及 最大上传文件限制
    CRM 插件奇怪的报错
    单实例,当MongoDB单表数据文件太大导致写入速度变慢
    MongoDB分片实战
    SpringCloud使用feign时的复杂参数传递(转)
    99%的人都理解错了HTTP中GET与POST的区别(转)
    MySQL 数据导入 Unknown MySQL server host 'localhost'
    会员积分体系设计思路(转)
  • 原文地址:https://www.cnblogs.com/daichangya/p/12959168.html
Copyright © 2011-2022 走看看