ArticleDirectory
- Migration and statute
- Two important generic
- Shiftreduceparser <yystype, yyltype>
- Parser
Preface
In the previous article, we used our own simpleBytesAndParserInstead of the parser and parser in managedmc, we finally get a language service that only implements simple syntax highlighting. Before continuing the discussion, let's take a look atCode. I have read the Babel code with patience. I found that Babel not only provides interfaces for the underlying mplex and mppg, but also implements some ide-oriented interfaces. Understanding the former helps to implement the core functions of the language service, and understanding the latter helps to express the functions of the Language Service in the IDE. In this article, I plan to analyze the underlying interfaces.
Mppg-oriented interfaces
Let's take a look at the class diagram in Babel. Some classes in the following two namespaces will be discussed in this article.Babel. parserThe following class is the class in parser. CS obtained by mppg compiling parser. Y,Babel. parsergeneratorThe following class is the helper class in Babel, that is, the Babel mppg-oriented interface.
Migration and statute
Add a concept before continuing. AnalysisProgramDuring the parsing process, shift and reduce will be performed to move the nodes into the Statute. The so-called "move in" means that the parser can find a terminator or non-terminator in a rule sequence that meets the current sequence.ExpectedA terminator or non-Terminator that accepts this symbol and merges it into the stack. The so-called protocol means that the parsing program completes a complete sequence after multiple moves, and return a non-terminator action. For detailed definitions of the Migration and statute, see Lex and YACC. In the following description, I refer to the terminator or non-terminator in a valid sequenceMark.
Two important generic
You may notice that the classes listed above contain two generics:YystypeAndYyltype. YystypeSemantictypeIndicates the semantic meaning type of a tag; yyltype indicatesLocationtypeIndicates the type of location meaning indicated by a tag.
Yyltype
First, explain the meaning of yyltype: when parsing program conventions, it needs to execute the default "Merge (merge)" action. Merging refers to merging the location information of several tags into one location. By default, the starting position of the first tag is used as the starting position of the non-Terminator, the ending position of the last mark is used as the ending position of the non-Terminator. Such a merge is meaningful, because in our syntax definition, there must be mutual nesting. With the merge, each tag can have its own single location information. The parser exposes an imerge <yyltype> interface, the defined method Merge (), which is called by the parser during the protocol and performs the default merge action. In parser. Y, merge () can be called in the Protocol behavior to change the default merge behavior. Babel defines this interface and implements a lexlocation. This class is almost enough for use, because in the code editor, a piece of text can be uniquely determined by a pair of start positions and end locations:
Yystype
Yystype indicates the semantic type of the tag. This type can be the text of the tag, or it can be a more complex type. When parsing the program specification, the value type of the non-terminator defined by the specification uses the value type of the first tag by default. We can define this type by defining the % Union keyword in parser. Y. We will detail how to define % Union in future articles. The lexvalue generated in parser. CS is yystype.
Shiftreduceparser <yystype, yyltype>
This class is defined in Babel and implements the process of moving forward and protocol.AlgorithmAnd auxiliary functions. Therefore, this class is very important and is the key to parser. CS and other modules collaboration. Without it, parser. CS cannot even be compiled. Instances of rule, state, parserstack <t>, ascanner <yystype, and yyltype are required in the specific implementation of the algorithm. Here, rule, State, and parserstack <t> are used to implement the stack algorithm. ascanner <yystype, yyltype> is the interface that must be implemented by the scanner for this parsing program. We can think that parser. CS is actually a configuration table, and shiftreduceparser is a program running according to this table. In addition, shiftreduceparser defines a doaction abstract method. parser. CS overrides this method and implements this method according to the behavior code of the Protocol.
Parser
The last parser class generated by parser. CS is inherited from shiftreduceparser <yystype, yyltype>. Parser is composed of many "configurations". addstate and rules are defined in shiftreduceparser <yystype, yyltype>. In addition, parser is a partial classification, which is set by the % partial keyword. For this design, Babel can extend some method interfaces for the parser class to the presentation layer in other files.
Mplex-oriented interfaces
In fact, only the following two interfaces need to be implemented by lexer:
Public interface icolorscan
{
Void setsource (string source, int offset );
Int getnext (ref int state, out int start, out int end );
}
Public abstract class ascanner <yystype, yyltype>
Where yystype: struct
Where yyltype: imerge <yyltype>
Here, icolorscan is the interface for coloring, and ascanner is the interface for scanner. Mplex automatically implements these two interfaces. In fact, for the second interface, mplex is not directly implemented, but indirectly implemented scanbase in Parser:
Public sealed class labels: scanbase, icolorscan
Public abstract class scanbase: ascanner <lexvalue, lexlocation>
Summary
Finally, use a picture to describe the main content in this article.