当前位置:网站首页>Write a jison parser from scratch (1/10):jison, not JSON
Write a jison parser from scratch (1/10):jison, not JSON
2022-07-04 09:27:00 【Hu Zhenghui】
Write one from scratch Jison Parser (1/10):Jison, No Json
List of articles
The title is correct ,Jison, No Json
JSON(JavaScript Object Notation) Is derived from JavaScript Lightweight data exchange format , It is convenient for human beings to read and write , It is also easy to machine parse and generate .JSON be based on Standard ECMA-262 3rd Edition - December 1999 A subset of . So the name contains JavaScript, however JSON Use a completely language independent text format , But it also uses something like C The habits of the language family ( Include C, C++, C#, Java, JavaScript, Perl, Python etc. ). These characteristics make JSON Become the ideal data exchange language .
and Jison yes JavaScript Parser generator (parser generator).
What is a parser (parser)?
Parser generator (parser generator)? It sounds a bit awkward , Understand why Jison It is called parser generator (parser generator), Or from the JSON Speaking of . This is a JSON Example .
{
"object" : {
"number" : 3.1415926,
"string" : "This is a string in an object."
},
"array" : [
"First value of array",
"Second value of array",
"Third value of array"
]
}
stay JavaScript Output directly JSON.
console.log({
"object" : {
"number" : 3.1415926,
"string" : "This is a string in an object."
},
"array" : [
"First value of array",
"Second value of array",
"Third value of array"
]
});
If it's a string , Then you can only output strings .
console.log(`{ "object" : { "number" : 3.1415926, "string" : "This is a string in an object." }, "array" : [ "First value of array", "Second value of array", "Third value of array" ] }`);
You can use JSON.parse
The function parses a string into JSON.
console.log(JSON.parse(`{ "object" : { "number" : 3.1415926, "string" : "This is a string in an object." }, "array" : [ "First value of array", "Second value of array", "Third value of array" ] }`));
the JSON.parse
Functions are parsers (parser), This is what the function name means .
Then the parser (parser) And parser generator (parser generator) What are the differences and connections ?
JSON.parse
Function can only be used to parse in the form JSON String , If the string format is not JSON, Even slightly changed , For example, in JSON Add comments like writing a program .
console.log(JSON.parse(`{ //this is a comment line "object" : { "number" : 3.1415926, "string" : "This is a string in an object." }, "array" : [ "First value of array", "Second value of array", "Third value of array" ] }`));
You're going to report a mistake
SyntaxError: Unexpected token / in JSON at position 4
This is because JSON.parse
The function does not recognize the new //this is a comment line
, You need to use JSON5.
const JSON5 = require('json5')
console.log(JSON5.parse(`{ //this is a comment line "object" : { "number" : 3.1415926, "string" : "This is a string in an object." }, "array" : [ "First value of array", "Second value of array", "Third value of array" ] }`));
What we use here JSON5.parse
function , Namely JSON5 The parser provided (parser), however JSON5.parse
The function can only recognize that it conforms to JSON5 Canonical string . Similar to that Hjson、HOCON, All are JSON Variant parser (parser), Each parser can only parse the syntax that conforms to its own format specification .
Mentioned earlier JSON(JavaScript Object Notation) Is a lightweight data exchange format , It is convenient for human beings to read and write , It is also easy to machine parse and generate . Similar human readable ( Non binary ) Data exchange format RDF(Resource Description Framework)、XML(eXtensible Markup Language)、Atom( be based on XML)、YAML( yes YAML Ain’t Markup Language Recursive abbreviation of )、EDN(Extensible Data Notation)、Property list、TOML(Tom’s Obvious, Minimal Language)、Rebol(Relative Expression Based Object Language)、Gellish(Generic Engineering Language).
These formats have corresponding parsers (parser), Similar to the previous JSON Examples of adding comments in , They also have their own shortcomings and limitations . For example, use these mature formats mentioned above , In almost all common programming languages, ready-made parsers can be used directly (parser), Sometimes there are multiple parsers (parser) Realization , But while enjoying the convenience , Also accept all its shortcomings , for example YAML The third edition of YAML 1.2 Specification manual PDF Version has 84 page ! And common YAML Usually there are only dozens of lines , In order to write dozens of lines correctly YAML And read 84 Page manual is not worth the loss . However, when using program processing YAML when , If you don't use mature packages , It's self parsing , Mistakes are inevitable , After all 84 There are too many details described in the page manual . Or like JSON Various variants of parser , Mature formats need to be improved , When the original format does not support this improvement , You also need to write your own parser (parser), At this time, we also need to fully understand the original specifications . further , If you want to customize the format , You also need to write a parser specifically (parser).
What is a parser generator (parser generator)?
I mentioned several common parsers that need to be written by myself (parser) Scenarios and challenges , So is there any way to develop a parser quickly and well (parser) Well ? There is also a class of mature products , Specifically for generating parsers (parser), Such products are parser generators (parser generator).
边栏推荐
- Opencv environment construction (I)
- Reading notes on how to connect the network - tcp/ip connection (II)
- What is uid? What is auth? What is a verifier?
- How should PMP learning ideas be realized?
- China electronic grade sulfur trioxide Market Forecast and investment strategy report (2022 Edition)
- Dede plug-in (multi-function integration)
- Investment analysis and future production and marketing demand forecast report of China's paper industry Ⓥ 2022 ~ 2028
- 2022-2028 global probiotics industry research and trend analysis report
- 《网络是怎么样连接的》读书笔记 - 集线器、路由器和路由器(三)
- Simulate EF dbcontext with MOQ - mocking EF dbcontext with MOQ
猜你喜欢
At the age of 30, I changed to Hongmeng with a high salary because I did these three things
Solve the problem of "Chinese garbled MySQL fields"
C语言-入门-基础-语法-[运算符,类型转换](六)
How to ensure the uniqueness of ID in distributed environment
2022-2028 global tensile strain sensor industry research and trend analysis report
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
C语言-入门-基础-语法-数据类型(四)
Latex download installation record
2022-2028 global intelligent interactive tablet industry research and trend analysis report
保姆级JDEC增删改查练习
随机推荐
C语言-入门-基础-语法-[标识符,关键字,分号,空格,注释,输入和输出](三)
什么是uid?什么是Auth?什么是验证器?
Ultimate bug finding method - two points
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
[untitled] forwarding least square method
保姆级JDEC增删改查练习
The 14th five year plan and investment risk analysis report of China's hydrogen fluoride industry 2022 ~ 2028
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
What is uid? What is auth? What is a verifier?
The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
Mantis creates users without password options
"How to connect the network" reading notes - Web server request and response (4)
Multilingual Wikipedia website source code development part II
Global and Chinese market of bipolar generators 2022-2028: Research Report on technology, participants, trends, market size and share
C語言-入門-基礎-語法-[運算符,類型轉換](六)
About the for range traversal operation in channel in golang
Solution to null JSON after serialization in golang
Basic discipline formula and unit conversion
GoLand environment variable configuration
A subclass must use the super keyword to call the methods of its parent class