如何C / C ++的解析器的工作? [英] How do C/C++ parsers work?

查看:184
本文介绍了如何C / C ++的解析器的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了很多时间研究PHP解析器如何工作的:

I've spent much time researching how PHP's parser works:

其转换PHP code,最终C $ C $角

it translates PHP code to finally c code.

但如何公司的C code转换为可执行文件?

But how's c code translated to executables?

BTW,如何判断是否语言A可以从数学方面以某种方式转化为B语言?

BTW, how to judge whether language A can be converted to language B somehow from mathematical aspect?

推荐答案

这是一个真正伟大的,真正深刻的问题上有很多计算机科学的部分绘制。

This is a really great and really deep question that draws on a lot of parts of computer science.

最后,一台电脑上的所有程序发出指令,机器code中的处理器上执行。没有一个机器code和每个处理器有它自己的一组可以被执行的指令。这些通常是像装载值到内存或低级操作两个值相加在一起。在理论上,每一个程序可被写入机器code,但这是罕见的情况。机code本质上是一系列的零和一的是被处理器特定的方式去$ C $的CD,这将是几乎不可能直接构建任何复杂的系统这样的。

Ultimately, all programs on a computer execute by issuing instructions to the processor in machine code. There is no one "machine code," and every processor has its own set of instructions that can be executed. These are usually low-level operations like "load a value into memory" or "add two values together." In theory, every program can be written in machine code, but this is rarely the case. Machine code is essentially series of zeros and ones that are decoded in particular ways by the processor, and it would be all but impossible to build any complex system directly this way.

一步上述机器code是的汇编语言的,通常有一到一对一的映射与机器code一个非常低级的宏观式的语言。例如,你可能有这样的添加即做加法,分作减法,或呼吁函数调用的命令。最终,code是使用转换组装机器code时的汇编程序的,程序转换成机器code。这是不可能建立在装配大复杂的系统,但它是非常困难的。

One step above machine code is assembly language, a very low-level macro-esque language that usually has a one-to-one mapping with the machine code. For example, you might have commands like "add" that do addition, "sub" for subtraction, or "call" for function calls. Ultimately, the code is converted to machine code using an assembler, a program that translates the assembly to machine code. It's possible to build big complex systems in assembly, but it's very difficult.

许多编程语言,如C和C ++是的编译的,这意味着一个特殊的程序叫的编译的转换源$ C ​​$ C分解成汇编语言,它可以然后可以直接转化成机械code。通过这种方式,您可以编程code,在高水平的作品 - 它可以有变量,函数,对象,模板,异常,等等 - 但可以直接在机器硬件运行。其他编程语言的间preTED 的,这意味着一个特殊的程序叫做的间preTER 的解析源$ C ​​$ C,建立了一些IN-它存储器重新presentation,然后将其转化为组件或者间接地(通过使用程序,用于控制执行什么指令),或直接(通过在需要的基础上产生组件)。

Many programming languages like C and C++ are compiled, which means that a special program called the compiler translates the source code down into assembly language, which can then be converted directly to machine code. In this way, you can program code that works at a high level - it can have variables, functions, objects, templates, exceptions, etc. - but which can operate directly on machine hardware. Other programming languages are interpreted, which means that a special program called an interpreter parses the source code, builds up some in-memory representation of it, and then translates it to assembly either indirectly (by using the program to control what instructions to execute) or directly (by generating assembly on an as-needed basis).

如何从一种语言转换成另一种理论已被广泛研究。有很多的挑战,从你怎么连看程序的源$ C ​​$ c和理解你在看什么? 什么是这个程序转换成其他语言的最有效方法是什么?前者涉及的词法解析语义分析的;后者涉及的优化的和的 code代

The theory of how to convert from one language into another has been extensively studied. There are many challenges, ranging from "how do you even look at the source code of the program and understand what you're looking at?" to "what is the most efficient way to convert this program into some other language?" The former involves lexing, parsing, and semantic analysis; the latter involves optimization and code generation.

典型地,在任何语言编写的程序,可以在另一种语言转换成等价的方案,虽然可以有效率的显着损失。某些编程语言具有访问底层硬件,因此不能在不具有访问该硬件语言编写的特殊功能,但是这种情况很少见。是否程序可以用另一种语言重写一个典型的措施是要求两种语言是否图灵完备的,一个数学术语,指编程语言是否EX pressive足够的EN $ C $ç特定类别的功能。

Typically, a program in any language can be converted into an equivalent program in another language, though there can be a notable loss in efficiency. Some programming languages have special functions that access underlying hardware and thus can't be written in languages that don't have access to that hardware, but this is rarely the case. One typical measure of whether a program can be rewritten in another language is to ask whether the two languages are Turing-complete, a mathematical term indicating whether the programming language is expressive enough to encode particular classes of functions.

希望这有助于!

这篇关于如何C / C ++的解析器的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆