Introduction to functions not directly used during Python running

Source: Internet
Author: User

We all know that when running Python, you do not need to directly use the relevant content of the Grammar file for relevant syntax analysis. If you are interested in the actual operations of running Python, you can browse our articles to get a better understanding of them. The data structure in graminit. c/graminit. h is used for syntax analysis.

As mentioned above, there is a Grammar directory under the Python source code Directory, which has only one file, Grammar, which defines all the Python syntax with BNF syntax. Take the if statement for example:

 
 
  1. if_stmt: 'if' test ':' suite 
    ('elif' test ':' suite)* ['else' ':' suite] 

The preceding statement can be understood as follows: The if statement is followed by up to 0 elif statements followed by the if keyword + logical expression + ':' + statement block (suite) and ends with an else statement. If_stmt on the far left indicates that this sentence defines if_stmt non-terminator), and ':' indicates the specific content corresponding to if_stmt on the right.

1. The content in ''quotation marks is the actual string, and 'if' indicates the if characters.

2. General identifiers represent non-terminologies, that is, the left side of an equation. if_stmt, test, and suite are all non-terminologies and can be extended to the sequence on the right side of the equation.

3. () brackets are atomic operators, which are enclosed in brackets and viewed as a single expression.

4. * Indicates 0 or more. For example, in if_stmt ('elif 'test': 'suite) * indicates that one if statement can contain 0 or more elif clauses.

5. + indicates one or more

However, this document is not just used as a reference. In fact, when running Python, you also need to indirectly use the content of the Grammar file for syntax analysis.

Python PGEN

The Makefile. pre. in and Parser/grammar. mak both have the following code:

 
 
  1. ###################################################
    #######################  
  2. # Grammar  
  3. GRAMMAR_H= $(srcdir)/Include/graminit.h  
  4. GRAMMAR_C= $(srcdir)/Python/graminit.c  
  5. GRAMMAR_INPUT= $(srcdir)/Grammar/Grammar  
  6. ###############################################
    ###########################  
  7. # Parser  
  8. PGEN= Parser/pgen$(EXE)  
  9. POBJS= \  
  10. Parser/acceler.o \  
  11. Parser/grammar1.o \  
  12. Parser/listnode.o \  
  13. Parser/node.o \  
  14. Parser/parser.o \  
  15. Parser/parsetok.o \  
  16. Parser/bitset.o \  
  17. Parser/metagrammar.o \  
  18. Parser/firstsets.o \  
  19. Parser/grammar.o \  
  20. Parser/pgen.o  
  21. PARSER_OBJS= $(POBJS) Parser/myreadline.o Parser/tokenizer.o  
  22. PGOBJS= \  
  23. Objects/obmalloc.o \  
  24. Python/mysnprintf.o \  
  25. Parser/tokenizer_pgen.o \  
  26. Parser/printgrammar.o \  
  27. Parser/pgenmain.o  
  28. PGENOBJS= $(PGENMAIN) $(POBJS) $(PGOBJS)  
  29. ###################################################
    #########################  
  30. # Special rules for object files  
  31. $(GRAMMAR_H) $(GRAMMAR_C): $(PGEN) $(GRAMMAR_INPUT)  
  32. -$(PGEN) $(GRAMMAR_INPUT) $(GRAMMAR_H) $(GRAMMAR_C)  
  33. $(PGEN): $(PGENOBJS)  
  34. $(CC) $(OPT) $(LDFLAGS) $(PGENOBJS) $(LIBS) -o $(PGEN)  

This code is used to generate pgen and then call pgen to generate graminit. h/graminit. c using Grammar as the input. PGEN is a tool for generating syntax analysis data in Python. It analyzes Grammar and generates corresponding graminit. c/graminit. h. Then, the data structure in graminit. c/graminit. h is used for syntax analysis during Python running. The specific implementation of PGEN is not covered in this article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.