编译器如何处理数据类型 [英] how data types are processed by Compiler

查看:46
本文介绍了编译器如何处理数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解像 C++ 这样的编译器是如何处理数据类型的.当从键盘输入任何内容时,比如数字 123,我们在屏幕上看到相同的数字,但根据数据类型,它的存储方式不同.即使输入了相同的键盘敲击,转换数据类型背后的算法是什么?为什么数字的存储方式和显示方式不同?例如在上面的情况下,如果数字 123 是 int 类型,它将存储为 01111011(1 个字节),但是如果它是字符串类型,它将存储在 3 个字节中(每个数字一个字节).但在这两种情况下,我们都会在屏幕上看到相同的数字.计算机是否使用不同的字节来存储和显示数字?我的问题是,当我们输入任何数字时,它是否会通过 ASCII 表并查找该数字的字符代码,然后编译器进行某种处理以将该数字存储在适当的数据类型中并显示该数字?

感谢堆

解决方案

你真的很想得到一本关于编译器的书来正确回答这个问题,但特别是对于数字字符串,你会在编译的词法分析阶段这样做.

编译器扫描程序的文本并尝试形成标记,这些将是语法的基本构建块,因此字符串、{、数字等...

所以假设你得到了这样的东西

int i = 10

您的编译器扫描此行并尝试创建令牌.首先它扫描 i, n, t,然后当它到达空间时它停止,因为它将空间视为分隔符.它根据其内部列表检查值int"并生成适当的标记.

它继续遍历字符串并检测变量 i、等号和数字.

它告诉字符串10"中的数字的方式可能是引号,但这取决于语言.

这就是为什么你有 10L 或 10f 之类的东西来表明你正在创建一个 long 或 float 值.否则编译器无法分辨.

参见 http://en.wikipedia.org/wiki/Lexical_analysishttp://en.wikibooks.org/wiki/Compiler_construction

或者甚至 https://www.coursera.org/course/compilers >

I struggle to understand how data types are processeed by compiler like C++. When entering anything from keyboard let say number 123, we see that same number on screen but depending upon the data types it is stored differently. What is the algorithm that goes behind converting the data types even though that same keyboard strokes are entered? How come numbers are stored differently and displayed differently? for instance in above case if number 123 is an int type it will be stored as 01111011 (in 1 byte) however if it is string type it will be stored in 3 bytes (one byte for each number). But in both cases we see the same number on the screen. Does computer use different bytes to store and display numbers? My question is when we enter any number does it go through the ASCII table and look up the char code of that number and then the compiler does some sort of processing to store that number in appropriate data type and diplay that number?

Thanks heaps

解决方案

You will really want to get a book on compilers to answer this questions properly, but specifically for numbers strings you would do this during the lexical analysis phase of the compilation.

The compiler scans through the text of the program and attempts to form tokens, these would be basic building blocks of the syntax, so a string, {, number, etc...

So lets say you get something like this

int i = 10

You compiler scans through this line and attempts to create the tokens. First it scans i, n, t, then when it gets to space it stops, since it would treat space as a delimeter. It checks the value "int" against it's internal list and generates the appropriate token.

It continues through the string and detects the variable i, an equals sign, and then a number.

The way it tells the number from the string "10" is probably the quotation marks, but this would depend on the language.

this is why you have things like 10L or 10f to indicate that you are creating a long or float value. Otherwise the compiler could not tell.

See http://en.wikipedia.org/wiki/Lexical_analysis http://en.wikibooks.org/wiki/Compiler_construction

or perhaps even https://www.coursera.org/course/compilers

这篇关于编译器如何处理数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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