Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

6.4 文法

对于如何理解表达式的含义,已经有标准的解决方法了:首先读入符号,将它们组合为单词。因此,如果键入

程序应该产生如下单词列表:

一个单词就是一个字符序列,用来表示一个基本单元,例如数字或者运算符。

在产生单词之后,我们的程序必须保证对整个表达式正确解析。例如,我们知道表达式45+11.5/7的计算顺序是45+(11.5/7)而不是(45+11.5)/7,但如何告诉程序这些有用的规则呢(例如除法比加法优先级高)?标准的方法就是设计一个文法(grammar)来定义表达式的语法,然后在程序中实现这些文法规则。例如:

这是一个简单的规则集合,最后一条规则读作“一个Number是一个浮点常量”,倒数第二条规则表明“一个Primary是一个Number,或者是'('后接一个Expression再接一个')'”。针对Expression与Term的规则类似,都是依赖其后的规则来定义。

如6.3.2节,我们从C++定义中借用了如下几类单词:

·浮点常量:与C++定义相同,如3.14、0.274e2和42等。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252