ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

编译器简介

2021-11-27 16:33:19  阅读:209  来源: 互联网

标签:language 简介 parser source 编译器 program Bison compiler


[binaryterms稻糠亩] A compiler transforms a program from its source language to a target language. The meaning of the program does not change. 'transform' is more fancy and mathy than 'translate'. For example, from
for (int i = 1, j = 2; j >= 0; j = j - 1) i = i + 1
to
( 0, 0, 0, 0, 0, 0, 0, 1 ), # store a 1
( 0, 0, 1, 0, 0, 0, 1, 0 ), # store b 2
( 0, 1, 0, 0, 0, 0, 0, 1 ), # add a 1
( 0, 1, 1, 1, 1, 1, 1, 1 ), # add b -1
( 1, 0, 0, 0, 0, 0, 1, 0 ), # jlz 2
( 1, 1, 0, 1, 1, 1, 0, 1 ), # jmp -3
( 1, 1, 0, 0, 0, 0, 0, 0 ), # jmp 0
Those 0s and 1s are binary bits. 0000000100100010010000010111111100000101101110111000000 is called machine code; machines are too stupid to speak a language.

A compiler is used to transform a program in a high-level language to a program in a low-level language. Java, C, C++, C#, Perl, and Python are high-level languages.

The compiler operates in phases. At each phase, the compiler decomposes the source program in a process to produce a target program.

 The lexical analysis is the initial phase in the analysis of the source program. The source program is made up of a stream of characters, and it is the input of the lexer, which outputs a sequence of tokens. Consider a small assignment statement that calculates the interest (simple interest) on the principal amount.

Interest = Principal * Rate * Time

This assignment statement above can be broken into the following tokens: Identifier, Assignment_Symbol, Identifier, Operator, Identifier, Operator, Identifier.

The syntax analysis phase is also referred to as hierarchical analysis or parsing. 我们分析句子成分,也研究字的构成。The tokens are grouped into grammatical phrases, and finally the parse tree.

 How to write a parser? GNU Bison is a parser generator. Bison reads a specification of a context-free language, and generates a parser (either in C, C++, or Java). You can find some examples by searching "bison calculator example". Bison is not a toy; the following list is of some projects that are known to use Bison:

* MySQL and PostgresSQL
* CMake
* Bash shell uses a yacc grammar for parsing the command input.
* GCC started out using Bison, but switched to a hand-written recursive-descent parser for C++ in 2004 (version 3.4), and for C and Objective-C in 2006 (version 4.1)
* The Go programming language (GC) used Bison, but switched to a hand-written scanner and parser in version 1.5.
* Perl 5 uses a Bison-generated parser starting in 5.10.
* The PHP programming language (Zend Parser).
* Bison's own grammar parser is generated by Bison.

标签:language,简介,parser,source,编译器,program,Bison,compiler
来源: https://www.cnblogs.com/funwithwords/p/15612266.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有