是否所有程序语言都按顺序读取? [英] Are all programming languages read in sequential order?

查看:57
本文介绍了是否所有程序语言都按顺序读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到直接的答案。使用多个搜索引擎会给我非常模糊的结果,或者只能回答一半的问题。



因此,为了详细说明,在编写程序并对其进行编译/解释时,计算机是按时间顺序依次读取每一行还是默认情况下跳过? / p>

示例:
使用C风格的源代码,因为这是我想到的第一件事。

  printf(我是程序的第一行!是的!); 
printf(我是第二个,呜!);
printf(我是第三个,是的!);

上面您可以看到三行代码,并且在运行时,它们将按顺序运行并按所述顺序输出到控制台。



我假设,除非被告知跳至源文件中的特定代码部分,否则它将按顺序读取每一行代码。那么,这是在计算机中解释程序的方式吗,是否所有的编程语言都以此方式进行编译/解释?程序是如何读取语言的?



我知道这是关于编程语言和计算机科学的基本问题,但我想澄清一下,没有



我的搜索词可能不正确,这也许就是为什么我什么都没找到的原因。谢谢!

解决方案

今天几乎所有广泛使用的编程语言实现都将 entire 源文件读入内存然后转换为一个或多个更高级别的表示形式,然后再编译或执行它们的任何



例如,如果您运行此Python程序:

 打印第一行 
打印打印<-此行有错误

然后解释器输出:

 文件 temp.py,第2行
打印输出<-该行出错
^
SyntaxError:语法无效

注意,在报告该错误之前,它不会打印第一行。在开始执行甚至第一行之前,它就在第二行中检测到语法错误。



在计算的早期,内存不足时,某些语言会进行解析和一次执行每一行。但是如今,这种风格非常罕见。


I can't seem to find a direct answer on this. Using several search engines gives me very vague results, or only answers half the question.

So, to elaborate, when you write a program and it's compiled/interpreted, does the computer read each line sequentially in chronological order or does it skip around by default?

Example: Using C-style source because it was the first thing that came to mind.

printf("I'm the first line of your program! Yay!");
printf("I'm the second, woo!");
printf("And I'm the third, yay!");

Above you can see there are three lines of code, and when run, they will be run in order and outputted to the console in said order.

I am assuming that, unless told to jump to a specific part of code within the source file, it will read each line of code in order. So is this how a program is interpreted in a computer, and do all programming languages get compiled/interpreted this way? Is how a program read language specific?

I know this is a basic question about programming languages and computer science, but I'd like some clarification and there aren't any concrete answers out there.

I probably worded the question incorrectly with my searches - that might be why I didn't find anything. Thanks!

解决方案

Almost all programming language implementations in wide use today read the entire source file into memory and then convert into one or more higher-level representations before they compile or execute any of it.

For example, if you run this Python program:

print "first line"
print print "<- error on this line"

Then the interpreter outputs:

  File "temp.py", line 2
    print print "<- error on this line"
              ^
SyntaxError: invalid syntax

Notice that it does not print "first line" before reporting that error. It detected the syntax error in the second line before beginning to execute even the first line.

Back in the early days of computing when memory was scarce, some languages did parse and execute each line one at a time. But these days, that style is quite rare.

这篇关于是否所有程序语言都按顺序读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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