Don't let users be misled by “ Professional term ” Frighten !
Tips and feedback designed by heart are the responsibility of software designers “ Professional conscience ”.
1 What are the mistakes ?
2 Beautify the error prompt
3 Error recovery strategy
1 What are the mistakes ?
stay DSL In the language development cycle , First you need to design grammar , Generating translators from grammar , Then input language samples to test the effect of the translator , And adjust the translation strategies according to the existing translation problems . There may be two types of mistakes : Grammatical Mistakes and Language sample error .
stay Parr Pull some mistakes out of the street , And praise ANTLR In the description of excellent performance in error prompt and recovery , You can observe some error prompts and recovery strategies and conventions :
(1) Error recovery is the process of recovering from grammatical errors in language samples , Often by modifying the input symbol or eating some symbols until the parser reaches a certain rule state of recognition ;
(2) Cascading error messages should be avoided as much as possible , That is, only one error message is given for each syntax error .
2 Beautify the error prompt
open ANTLR Of -dfa Options , You can view the generated DFA, Understand the decision and status information of the recognizer .
To achieve beautification of the error prompt , Cover... In grammar BaseRecognizer Of getErrorMesage() and getTokenErrorDisplay() Method .( It is required to cover displayRecognitionError() instead of getErrorMesage(), In fact, if you look at the generated code, the former will call the latter ).
Some built-in exception classes
abnormal explain
RecognitionException Identify anomalies .ANTLR The generated identifier throws the base class of the exception . Record the input stream information when the error occurs : The symbol the recognizer is currently seeing ( character 、Token Or tree nodes ) The index of , Pointer to the wrong symbol 、 Current row 、 Position in the industry .
MismatchedTokenException Token Mismatch exception . Indicates that the specific symbol expected by the parser is not currently found . Record additional Token Type information .
MismatchedTreeNodeException Tree node mismatch exception . Similar to the previous exception , Point out the specific token Type node not found .
NoViableAltException No viable option exception . The recognizer reaches the decision point , But looking forward, the symbol doesn't match all options . Record looking forward DFA Middle decision number and status number , Also record the piece of grammar that made the decision .
EarlyExitException Premature exit exception . distinguish (...)+ EBNF Sub rule , Only one less match , But the rule doesn't match any symbols . Record DFA Middle decision number .
FailedPredicateException Failed predicate exception . The semantic predicate evaluates to false. Record the rule name and predicate text .
MismatchedRangeException Range mismatch exception . The scope is as follows [a..z], No symbols in the range are matched . The smallest and largest elements in the record range .
MismatchedSetException Collection mismatch exception . Assemble as {'a','b'}, Does not match any symbols in the set . Record all the symbols in the set .
MismatchedNostSetException Non set mismatch exception . Non set refers to ~ The quasi... Of the set represented by the operator . Similar to the previous exception .
ANTLR The recognizer does not use string information to create exception objects , Track and record only the field information necessary to generate errors .
Base recognizer class BaseRecognizer Multiple error reporting methods in can generate local error messages , Don't expect from the exception class above getMessage() Method returns any exception information .
In addition to exception information , You can use custom actions in grammar to provide error information .
An important error message tip is to indicate the parsing rules used when an error occurs ( Stack ), The general method is in grammar @members Define a stack of error messages in , In the rules that make the most sense to users @init/@after In the action, the stack in and stack out operations are performed respectively , At the same time @memebers Cover in action getErrorMessage() Method , It uses the peek() Get the prompt information of the current scene .
Error zero tolerance : Exit immediately when you encounter the first error
Need to be in grammar @members Cover in action mismatch()、recoverFromMismatchedSet() Method and override the default @rulecatch.
Cover @rulecatch Actions affect all the rules , Fine grained rule level can be used catch Action to perform a manual recovery operation .
The mistakes in morphology and tree grammar are similar to those in general grammar , The only difference is that it deals with characters and tree nodes .
3 Error recovery strategy
Standing on the shoulders of giants
ANTLR The error recovery mechanism of Nikalaus Wirth Of "Algorithms + Data Structures = Programs"( Gossip : It's like he won the Turing prize with that )、Rodney Topor Of " Error recovery record in recursive descent parser " And some of the Josef Grosch stay CoCo Some ideas of parser generator .
The essential idea is , When the recognizer encounters mismatched symbol errors , Try single symbols as much as possible first Insert and Delete Whether it can be recovered , If not , Again Swallow some symbols until you look ahead and the symbols belong to a resynchronization set (resynchronization set) Exit the rule after .
A resynchronization set is a set of input symbols , These symbols can legally appear in the current rule and the current rule call chain .

ANTLR3 Full reference to the guide book notes [08] More articles about

  1. ANTLR3 Full reference to the guide book notes [01]

    quote Terence Parr. The Definitive ANTLR Reference, Building Domain Specific Languages(antlr3 version). ...

  2. ANTLR3 Full reference to the guide book notes [06]

    Preface During this period of time, I'm as busy as a dog in the company , But I'm busy with less technical work . At last AST IR and tree grammar After a while , Plan to finish the reading notes of this part tomorrow .   Content 1 Internal representation AST structure 2 Tree grammar   ...

  3. ANTLR3 Full reference to the guide book notes [02]

    Preface What is a programming language ? use wiki Description above , Programming language is a kind of artificial design language , It's used to interact with machines through instructions : Programming language is the mark of programming program , A program is a description of a calculation or algorithm . Detailed introduction and background information reference : Programmin ...

  4. ANTLR3 Full reference to the guide book notes [07]

    Preface Real programmers are lazy , I'm so lazy that I don't even write one extra line of code . If you can hand over the oil stained work at the bottom to others , Play the role of a member of a think tank , Life will be more comfortable than you think . Strictly follow the instructions for a long time ...

  5. ANTLR3 Full reference to the guide book notes [05]

    Preface Only generation gives true/false It's not very useful to use our recognizer , It's natural to execute a piece of code when a structure is encountered in the identification process . The idea of storing the information in the structure . ANTLR Provides a super hybrid of embedding attributes and actions in grammar “ Grammar ”, Can live ...

  6. ANTLR3 Full reference to the guide book notes [04]

    Preface What's the way to learn about frameworks or third-party libraries (1) A little browsing manual or tutoral, Focus only on the features required by the program , And then improve the understanding of its detailed content and characteristics ? (2) Spend a lot of time studying the details , Consider the program implementation ? It's a chicken ...

  7. ANTLR3 Full reference to the guide book notes [03]

    Preface Wenzhongdi 4 There's a lot of content in this chapter , It's a little boring , But don't hold on , The previous work is in vain . Reconfirm the overall goal : protege4 Editor Class Definition Syntax parsing and error tips in : Java Virtual machine specification ...

  8. HTTP Authority guide book notes

    HTTP Authoritative guide notes There are two realms of reading , The first realm is to read the book thinly , The other is thick reading . This article is HTTP The authoritative guide to reading notes , It's the first level of reading , Read thick books thin . The article is right HTTP This paper gives a detailed overview of some key concepts of , Read through ...

  9. css Authority guide book notes

    Today, I'm on my cell phone , It's a long time ago css Notes from the authoritative guide , So I moved to my blog . 1. Attribute selector class be known as one Of p Elements p[class][name] contain class and name Attribute p Elements p[cla ...

Random recommendation

  1. iOS Push certificate to pem file

    iOS Push certificate to .pem file . Push certificate to pem file openssl x509 -in apns_miaobozhibo.cer -inform der -out apns_miaobozhibo.p ...

  2. objective-c static Summary of the use of variables

    stay java in , We often use singleton mode , These design patterns are in ios It is also commonly used in development , I've been thinking about using it recently ios Singleton mode is used in development stay objective-c in , Need to be in .m There is a definition in the file static Variables to represent the global ...

  3. Topic 1 : Print out all " Narcissistic number ", So-called " Narcissistic number " A three digit number , The sum of its cubes is equal to the number itself . for example :153 It's a " Narcissistic number ", because 153=1 The third power of +5 The third power of +3 The third power of .

  4. r-cnn Study ( Two )

    faster r-cnn 1. problem stay fast r-cnn in ,proposals It has become the bottleneck of speed improvement . In this paper , Use deep networks to calculate proposals, Compared with the computation of detection network ,proposals ...

  5. Web Lightweight message queuing in applications

    Web Why message queues are needed in applications ? The main reason is due to the high concurrency environment , Due to late synchronization processing , Requests tend to jam , for instance , a large number of insert,update And so on at the same time mysql, Leads directly to an infinite number of row lock table locks , what ...

  6. (C#)Windows Shell Programming Series 1 - Basics , Browse a folder

    original text (C#)Windows Shell Programming Series 1 - Basics , Browse a folder ( This series of articles by lemon's (lc_mtt) original , Reprint please indicate the source , thank you -) Windows Shell Programming , namely Windows ...

  7. springmvc Go back to the page , Only in iframe Middle jump , Not the whole page , resolvent .

    Problem description : In my homepage, I used iframe, Among them in iframe There is one of them. button Click on the submit , And then in Controller When you go back to the page in , Is in iframe Open in , Not the whole page . At first I wanted to use aja ...

  8. VisualSFM+PMVS Generate dense point clouds

    Use the camera to take pictures of a scene from different angles , Use VisualSFM Can get a sparse point cloud , If you want to get a dense point cloud , Can be in VisualSFM Add PMVS Applications for ,PMVS It will be run as a plug-in to plug a sparse point cloud into a dense one . ...

  9. selenium in webdriver distinguish class A solution to the problem of spaces in multiple values of a property

    Beginning to learn automated testing , It seems that nine times out of ten, everyone is practicing with Baidu website , Thanks to Baidu . The main page is the search box and submit button : Input box element attributes :<input id=&qu ...

  10. keepalived to LVS What did it bring

    LVS+Keepalived 1>Keepalived brief introduction  Keepalived yes Linux The next lightweight high availability solution , High availability (High Avalilability,HA), Actually, there are two different kinds of ...