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

What is the language knowledge of ​ JavaScript?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail what the language knowledge about JavaScript is, and 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.

Languages are classified by grammar

First of all, let's talk about the taxonomy of general-used languages. when we usually speak, we speak Chinese. when we study abroad or travel, we all need to speak English. I don't know if you have this kind of experience, when we are abroad, because English is not very good, we will put the key words together, and then the grammar is also wrong, but foreigners also understand. For example, "long time no see", we will say "long time no see", and then foreigners still think it is very useful, so they also add to the language.

A characteristic of this kind of language is that its grammar does not have a strict definition, so we call it "informal language". The typical representative is what we usually say.

In computers, most languages are "formal languages"-formal languages have a formal definition of their characteristics, and they are very rigorous.

Then it is also classified in the formal language, and one of them is the Chomsky pedigree.

Chomsky pedigree: a taxonomic pedigree in computer science that describes the expressive ability of formal grammars, proposed by Norm Chomsky in 1956. It consists of four levels.

Informal language

Chinese, English

Formal language (Chomsky pedigree)

Type 0: unlimited grammar-as long as the language is clearly defined

Type 1: context-sensitive grammar-the same combination of words and sentences that are related to the above and the following

Type 2: context-free grammar-the same expression has the same meaning wherever it is put.

Type 3: regular grammar-- A grammar that can be described by regular expressions

In the Chomsky pedigree, 0123 is an inclusion relation, that is, a context-sensitive grammar, which must also belong to type 0 -, but not vice versa.

What is production? (BNF)

Production: in a computer, a series of grammatical rules (Backus-Naur Form,BNF) statements obtained by the Tiger compiler after lexical analysis (Lexical Analysis) and syntax analysis (Syntax Analysis) of the source program.

Backus Normal Form Paradigm: Bakos Paradigm (English: Bakos, abbreviated as BNF) is a language used to express context-free grammars, which describe a class of formal languages. It is a symbol set first introduced by John Backus and Peter Naur to describe the grammar of computer language.

Terminator: the character that finally appears in the code (https://zh.wikipedia.org/wiki/ character and non-end character)

Use the name enclosed in angle brackets () to represent the grammatical structure name

Grammatical structures are divided into basic structures and compound structures that need to be defined by other grammatical structures.

Infrastructure called Terminator

Compound structure is called non-Terminator

Quotation marks and the characters in the middle represent the Terminator

Can have parentheses

* means to repeat many times

| | indicates "or" |

+ means at least once

Case study:

Let's use BNF to describe the four operations that we are familiar with.

The four are as follows: 1 + 2 * 3

The summary character in this:

Number

+, -, *, /

Non-terminal character

MultiplicativeExpression

AdditiveExpression

The four operations we learned when we were young are addition, subtraction, multiplication and division, which actually have a priority relationship. We can understand it as a continuous addition of 1+2x3, which can be split into a 1 and 2x3. So 2x3 is its substructure, and then 2 and 3 are the Number in this structure, and then the operator * is in the middle.

So when we use BNF to describe this remote calculation, we will first define an addition expression in the format:

A list of multiplication expressions or

Addition expression + multiplication expression or

Addition expression-multiplication expression

Because BNF is recursive, you can use your own expressions when defining expressions.

Then multiplication is similar, except that the expression of multiplication in addition is replaced by Number:

Number or

Multiplication expression * Number or

Multiplication expression / Number

Finally, let's take a look at how it is written in code:

:: = | "*" | "/" |:: = | "+" | "-" |

An in-depth understanding of generation

Here we try to gain an in-depth understanding of the Chomsky pedigree mentioned earlier through production.

Terminator: the zh.wikipedia.org/wiki/ character that finally appears in the code.

Type 0: unlimited grammar

Production:?

Multiple non-terminators can be generated in unrestricted grammars

So it can be written freely in unlimited grammar.

Type 1: context-sensitive grammar

Production:?

There are certain restrictions on the writing produced.

Can it be on the left and right? Write multiple non-terminators in the

But what can be changed can only be the front and the back, and it is related.

And there must be a fixed part in the middle.

So the one in front? Is that the above, the back? It's the following.

Type 2: context-free grammar

Production:: =?

The one on the left must be a non-Terminator.

On the right? It can be written freely, it can be a bunch of Terminators or a mixture of Terminators and non-Terminators.

Type 3: regular grammar

Production: =

Regular grammars are required.

If it is defined recursively by regular grammar, then it does not allow you to define An on the tail.

If the symbol on the left, then the right must appear at the beginning of the production

According to this rule, all regular grammars can be represented by regular expressions.

So is JavaScript a context-sensitive grammar, a context-free grammar, or a regular independent grammar?

JavaScript is generally a context-free grammar, and most of the expressions are regular grammars, but there are two special cases:

A new * * operator is added to the expression of 1.JavaScript, and * * represents the multiplier.

The multiplier operator is actually combined to the right, for example, 2 / 2 and the result is 2.

This is because 1 * * 2 is calculated first, the 2 power of 1 is 1, and then the 1 power of 2 is 2, so the final result is 2 instead of 4.

So because it's right associative, it's not a regular grammar.

If these judgments of if are added, it will be even less regular grammar.

two。 For example, get.

If we're writing get a {return 1}, then get is something like a keyword.

But if we add: after get, then get itself is the property name.

So if we understand it strictly according to the Chomsky pedigree, then JavaScript belongs to context-sensitive grammar. In the implementation of the JavaScript engine, it can be understood that the structure of mass programming is for context-free grammar. once you encounter a context-sensitive place like get, then you will do some special cases with the code separately. So generally speaking, JavaScript will not be classified as context-sensitive grammar to deal with.

Other production type

In addition to the Chomsky pedigree that can be defined by BNF, there are actually many different production types. For example, the later emergence of EBNF, ABNF, are aimed at BNF on the basis of grammatical expansion. So generally speaking, in the standard of every language, there will be a custom production way of writing.

For example, it is also true in JavaScript:

AdditiveExpression: MultiplicativeExpression AdditiveExpression + MultiplicativeExpression AdditiveExpression-MultiplicativeExpression

It begins with indentation, which is equivalent to the non-Terminator on the left side of the production, which is followed by a colon, followed by indentation of two spaces. Then in the JavaScript standard, its non-terminators, plus and minus signs, are represented by bold black fonts. So there are many kinds of production on the Internet, and you can't read all the languages by learning only one BNF. Although they all have different standards and ways of writing, the meaning they express is roughly the same. So we need to understand the ideas and principles behind production, so we can ignore the differences in expressions.

The classification of modern language

A special case of modern language

In C++, * may express a multiplication sign or pointer, depending on whether the identifier before the asterisk is declared as a type

In VB, < may be the less than sign or the beginning of the direct quantity of XML, depending on whether the direct quantity of XML is acceptable in the current location.

In Python, the tab characters and spaces at the beginning of a line are processed into virtual terminators indent or dedent according to certain rules according to the first white space of the previous line.

In JavaScript, / may be a division sign, or it may be the beginning of a regular expression, and its processing is similar to that of VB. Special handling is also required in string templates, and there are rules for automatic insertion of semicolons.

Classification of language

Formal language-purpose

Data description language-sometimes we need to store pure data, and there is no way to program it.

JSON, HTML, XAML, SQL, CSS

programing language

C, Clearing, Java, Perl, Python, Ruby, Perl, PHP, Go, Perl, Lisp, T-SQL, Clojure, Haskell, JavaScript, CoffeeScriptx

Formal language-- expression

Declarative language

JSON, HTML, XAML, SQL, CSS, Lisp, Clojure, Haskell

Imperative language

C, Python, Ruby, Perl, JavaScript, Java

The nature of programming languages

Turing completeness

Imperative-Turing machine

Goto

If and while

Declarative-lambda

Recursion

Turing completeness: in computability theory, if a series of rules for manipulating data (such as instruction sets, programming languages, cellular automata) can be used to simulate single-band Turing machines, then it is Turing complete. The word comes from Alan Turing, a mathematician who introduced the concept of Turing machine. Although Turing opportunities are physically limited by storage capacity, Turing completeness usually refers to "a general physical machine or programming language with unlimited storage capacity".

Turing machine machine, also known as deterministic Turing machine, is a kind of mathematical logic machine that abstracts human computing behavior, which is put forward by British mathematician Alan Turing in 1936. Its more abstract meaning is a computing model, which can be regarded as the ultimate powerful logic machine equivalent to any finite logic mathematical process.

Dynamic and static

Dynamic:

Run on the user's device / online server

Timing: when the product is actually used

Terminology: Runtime (runtime)

Static:

Run on the programmer's device

Timing: during product development

Term: Compiletime (compile time)

JavaScript, the language of interpretation execution, does not have Compiletime. We will now also use Webpack to build our code, but there is actually no Compiletime. So, today's correspondence between Runtime and Compiletime is no longer accurate, but we will still be willing to follow the habit of Compiletime, because JavaScript is also a time of "Compiletime", so we will also use the word Compiletime to talk about some of the features in JavaScript.

Type system

Dynamic type system-when types can be found on the user's machine

JavaScript is a dynamic type system.

Static type system-- only when programmers can find types when writing code

When C++ finally compiles the code to the target machine, all type information is lost

Semi-dynamic and semi-static type systems-- languages such as Java provide reflection mechanisms

The main type checking and type operations at compile time have been disposed of at compile time

But if you want to get type information at run time, you can still get it through reflection.

Strong typing and weak typing-- describing the form of type conversion in a programming language

Strongly typed: no implicit conversion (type conversion does not occur by default)

Weakly typed: there is implicit conversion (JavaScript is a typical weakly typed language. By default, Number is converted to String type and then added to give you a String type, as well as String and Boolean double operations, which first convert Boolean to Number and then do the same comparison with String)

Compound type

Structural body

Function signature (including parameter type and return value type)

Subtypes-the typical language is C++ (there are some default behaviors when doing type conversions)

Paradigm

Covariant and inverter: https://jkchao.github.io/typescript-book-chinese/tips/covarianceAndContravariance.html

Covariant example: Array can be used wherever the norm array Array can be used.

Inverter example: wherever you can use Function, you can use Function

The Design method of General imperative programming language

Generally speaking, our imperative language may have some subtle structural inconsistencies, but it is generally divided into five levels.

Atomic level (Atom)-the smallest unit of a language

Keyword (Identifier)

Direct amount of characters / numbers (Literal)

Variable name (Variables)

Expression (Expression)-atomic structures are formed by operator connections and auxiliary symbols

Atomic unit (Atom)

Operator (Operator)-addition, subtraction, multiplication and division, splicer, etc.

Grammar character (Punctuator)

Statement (Statement)-expressions with specific identifiers, keywords, symbols form a certain structure

Expression (Expression)

Keyword (Keyword)

Grammar character (Punctuator)

Structure-helps us organize, block, and divide into different reuse structures

Function (Function)

Class (Class)

Process (Process)-PASCAL language will have the concept of Process

Namespace (Namespace)-there will be the concept of Namespace in C++ / PHP

Program (Program)-manage language modules and installation

Program (Program)-the code actually executed

Module (Module)-- A module ready to be reused

Package (Package)

Library (Library)

We will have a relatively fixed structure for each level of explanation. At each level we use grammar as a clue, but in fact, in addition to grammar, we focus on semantics and progressive tense.

The so-called "semantics" is what it looks like in practice when the user uses it. What front-end engineers are most concerned about is what kind of syntax we write and what it looks like when it runs on the user's computer. This is how we become.

When the intermediate join syntax runs, it is the semantics of the language. We express a certain semantics through a certain syntax, and finally change the state of the runtime.

What about the language knowledge of JavaScript is shared here, I hope the above content can be of some help to you, can 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