如何理解条件,赋值和常量表达式的语法?

时间:2017-08-16 03:58:00

标签: c

来自C11标准

  1.   

    6.5.15条件运算符

         

    语法

    conditional-expression:
        logical-OR-expression
        logical-OR-expression ? expression : conditional-expression
    

    逻辑OR表达式是条件表达式,因为它是 条件表达式的退化?

  2.   

    6.5.16分配运营商

         

    语法

    assignment-expression:
        conditional-expression
        unary-expression  assignment-operator  assignment-expression
    

    这是否意味着条件表达式是一个赋值 表达?为什么?

  3.   

    6.6常量表达式

         

    语法

    constant-expression:    
        conditional-expression
    

    常量表达式的语法是否表示常量 表达式和常量表达式是相同的概念吗?

  4. 感谢。

2 个答案:

答案 0 :(得分:2)

conditional-expressionassignment-expressionconstant-expression是语法中的非终结符。它们的名称不应被视为暗示conditional-expression必须实际涉及条件运算符,或者assignment-expression必须涉及赋值;它们以在优先级层次结构中的位置命名。

特别是,constant-expression扩展为conditional-expression,因为常量不是语法概念。排除需要常量的非常数不是语法的工作;该作业在编译器的其他部分中处理。语法将允许任何需要常量的conditional-expression

答案 1 :(得分:0)

第6.5章写得很差,很难理解。关键是:

C11 6.5 / 3

  

运算符和操作数的分组由语法表示。

这意味着在标准的任何地方实际上都没有明确指定运算符优先级!相反,我们应该查看每个运算符的语法并找出它。

所有运算符都构建了一个运算符优先级链,因此对于6.5的每个子章节,提到前一组运算符只是为了表明它具有更高的优先级。

从顶部开始,我们有primary-expression postfix-expression ...(6.5.1),优先级最高。在下一组后缀运算符中,将提到前一个组以表明它具有更高的优先级:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JamiesWorldGenerator
{
    public partial class WorldGeneratorForm : Form
    {
        World world = null;

        public WorldGeneratorForm()
        {
            InitializeComponent();
        }

        private void runButton_Click(object sender, EventArgs e)
        {
            if (runButton.Text == "Run")
            {
                if (world == null) world = new World(this, 1000);
                world.run();

                runButton.Text = "Stop";
                statusLabel.Text = "Status: Active";
            }
            else if (runButton.Text == "Stop")
            {
                world.pause();

                runButton.Text = "Run";
                statusLabel.Text = "Status: Inactive";
            }
        }

        public void updateTimeElapsed(int i)
        {
            this.daysElapsedLabel.Text = "Days Elapsed: " + i;
        }

        public void updatePersonList(List<Human> list)
        {
            foreach (Human h in list) this.peopleList.Items.Add(h);
            peopleList.DisplayMember = "FullName";
        }
    }
}

这并不意味着主表达式是后缀表达式,而是后缀表达式的语法是namespace JamiesWorldGenerator { partial class WorldGeneratorForm { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.populationLabel = new System.Windows.Forms.Label(); this.runButton = new System.Windows.Forms.Button(); this.statusLabel = new System.Windows.Forms.Label(); this.panel1 = new System.Windows.Forms.Panel(); this.countriesList = new System.Windows.Forms.ListBox(); this.panel2 = new System.Windows.Forms.Panel(); this.peopleList = new System.Windows.Forms.ListBox(); this.panel3 = new System.Windows.Forms.Panel(); this.worldNews = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.daysElapsedLabel = new System.Windows.Forms.Label(); this.panel1.SuspendLayout(); this.panel2.SuspendLayout(); this.panel3.SuspendLayout(); this.SuspendLayout(); // // populationLabel // this.populationLabel.AutoSize = true; this.populationLabel.Location = new System.Drawing.Point(9, 9); this.populationLabel.Name = "populationLabel"; this.populationLabel.Size = new System.Drawing.Size(94, 13); this.populationLabel.TabIndex = 0; this.populationLabel.Text = "World Population: "; // // runButton // this.runButton.Location = new System.Drawing.Point(8, 320); this.runButton.Name = "runButton"; this.runButton.Size = new System.Drawing.Size(75, 23); this.runButton.TabIndex = 1; this.runButton.Text = "Run"; this.runButton.UseVisualStyleBackColor = true; this.runButton.Click += new System.EventHandler(this.runButton_Click); // // statusLabel // this.statusLabel.AutoSize = true; this.statusLabel.Location = new System.Drawing.Point(89, 325); this.statusLabel.Name = "statusLabel"; this.statusLabel.Size = new System.Drawing.Size(81, 13); this.statusLabel.TabIndex = 2; this.statusLabel.Text = "Status: Inactive"; // // panel1 // this.panel1.Controls.Add(this.countriesList); this.panel1.Location = new System.Drawing.Point(12, 25); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(197, 187); this.panel1.TabIndex = 3; // // countriesList // this.countriesList.Dock = System.Windows.Forms.DockStyle.Fill; this.countriesList.FormattingEnabled = true; this.countriesList.Location = new System.Drawing.Point(0, 0); this.countriesList.Name = "countriesList"; this.countriesList.Size = new System.Drawing.Size(197, 187); this.countriesList.TabIndex = 1; // // panel2 // this.panel2.Controls.Add(this.peopleList); this.panel2.Location = new System.Drawing.Point(215, 25); this.panel2.Name = "panel2"; this.panel2.Size = new System.Drawing.Size(197, 187); this.panel2.TabIndex = 4; // // peopleList // this.peopleList.Dock = System.Windows.Forms.DockStyle.Fill; this.peopleList.FormattingEnabled = true; this.peopleList.Location = new System.Drawing.Point(0, 0); this.peopleList.Name = "peopleList"; this.peopleList.Size = new System.Drawing.Size(197, 187); this.peopleList.TabIndex = 0; // // panel3 // this.panel3.Controls.Add(this.worldNews); this.panel3.Location = new System.Drawing.Point(12, 219); this.panel3.Name = "panel3"; this.panel3.Size = new System.Drawing.Size(400, 95); this.panel3.TabIndex = 5; // // worldNews // this.worldNews.Dock = System.Windows.Forms.DockStyle.Fill; this.worldNews.FormattingEnabled = true; this.worldNews.Location = new System.Drawing.Point(0, 0); this.worldNews.Name = "worldNews"; this.worldNews.Size = new System.Drawing.Size(400, 95); this.worldNews.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(336, 320); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 6; this.button1.Text = "Load World"; this.button1.UseVisualStyleBackColor = true; // // button2 // this.button2.Location = new System.Drawing.Point(255, 320); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(75, 23); this.button2.TabIndex = 7; this.button2.Text = "Save World"; this.button2.UseVisualStyleBackColor = true; // // daysElapsedLabel // this.daysElapsedLabel.AutoSize = true; this.daysElapsedLabel.Location = new System.Drawing.Point(212, 9); this.daysElapsedLabel.Name = "daysElapsedLabel"; this.daysElapsedLabel.Size = new System.Drawing.Size(84, 13); this.daysElapsedLabel.TabIndex = 8; this.daysElapsedLabel.Text = "Days Elapsed: 0"; // // WorldGeneratorForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(423, 351); this.Controls.Add(this.daysElapsedLabel); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.panel3); this.Controls.Add(this.panel2); this.Controls.Add(this.panel1); this.Controls.Add(this.statusLabel); this.Controls.Add(this.runButton); this.Controls.Add(this.populationLabel); this.Name = "WorldGeneratorForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Jamie\'s World Generator"; this.panel1.ResumeLayout(false); this.panel2.ResumeLayout(false); this.panel3.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label populationLabel; private System.Windows.Forms.Button runButton; private System.Windows.Forms.Label statusLabel; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.ListBox countriesList; private System.Windows.Forms.ListBox peopleList; private System.Windows.Forms.Panel panel3; private System.Windows.Forms.ListBox worldNews; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label daysElapsedLabel; } }

整个6.5章节都是这样的。它可能听起来很愚蠢而且仅仅是因为它 皇家愚蠢。

注85)尝试提供一些帮助:

  

语法指定运算符在表达式求值中的优先级,这是相同的   作为本条款主要子条款的顺序,首先是最高优先级。