Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Javacc

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the case analysis of Javacc. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

PARSER_BEGIN (Simple1)

Public class Simple1 {

Public static void main (String args []) throws ParseException {

Simple1 parser = new Simple1 (System.in)

Parser.Input ()

}

}

PARSER_END (Simple1)

Void Input ():

{}

{

MatchedBraces () ("|") *

}

Void MatchedBraces ():

{}

{

"{" [MatchedBraces ()] "}"

}

After setting up the bin directory for javacc, type javacc Simple1.jj at the command prompt and javacc will generate the following java source code files for you

Simple1.java

Simple1TokenManager.java

Simple1Constants.java

SimpleCharStream.java

Token.java

TokenMgrError.java

Where Simple1 is the object of your parser, and its constructor argument is the input stream to be parsed, and here is System.in. Class Simple1 is defined between the tags PARSER_BEGIN (Simple1) and PARSER_END (Simple1). But it must be clear that the name in PARSER_BEGIN and PARSER_END must be the name of the lexical analyzer (in this case, Simple1).

The following definition of PARSER_END is the definition of grammatical non-terminating symbols.

The grammar of Simple1 is basically:

Input-> MatchedBraces ("|") *

MatchedBraces-> "{" MatchedBraces "}"

From its definition, we can see that each non-final symbol is for a process. Such as the process of Input

Void Input ():

{}

{

MatchedBraces () ("|") *

}

Remember to add a colon ":" after the definition of void Input, followed by the definition of two blocks {}.

The code in the first {} is the code that defines the data and initializes the data. The second part of {} is the production that actually defines Input.

Each production is connected with a "|" symbol.

Note: the production here does not require a strict BNF normal form grammar. Its grammar can be either BNF or mixed with the definition method in the regular expression. For example, Input-> MatchedBraces ("|") * above

In ("|") * is a regular expression that represents 0 to infinitely repeated tokens of or. Instead, it is a javacc system-defined token (TOKEN) that indicates the end of the file.

Except for, whether it is a system-defined TOKEN or a custom TOKEN, the TOKEN in it is expressed in the same way.

Each non-terminating symbol (Input and MatchedBraces) forms a member function of Class Simple1 in the Simple1.java generated by javacc. When you call Simple1's Input externally, the parser starts parsing.

This is the end of the case analysis of Javacc. I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report