| English | 意味 |
|---|---|
| postfix/ˈpəʊstfɪks/ | 後置記法 |
| precedence/ˈpresɪdəns/ | 優先順位 |
| grammar/ˈɡræmə/ | 文法 |
| syntax diagram/ˈsɪntæks ˈdaɪəɡræm/ | 構文図 |
| Reverse Polish Notation/rɪˈvɜːs ˈpəʊlɪʃ nəʊˈteɪʃn/ | 逆ポーランド記法 |
| Backus-Naur Form/ˈbækəs nɔː fɔːm/ | バックス・ノー形式 |
| production rules/prəˈdʌkʃn ruːlz/ | 生成規則 |
| terminal/ˈtɜːmɪnl/ | 終端記号 |
| non-terminal/nɒn ˈtɜːmɪnl/ | 非終端記号 |
| infix/ˈɪnfɪks/ | 中置記法 |
The notation with no brackets, and no ambiguity
- Write
3 + 4 * 2and you are relying on a convention: that multiplication binds tighter than addition. Change the convention and the expression means something else. - A Polish logician, Jan Łukasiewicz, showed in the 1920s that if you put the operator before its operands the brackets become unnecessary. Reverse it, putting the operator after, and you get a form a machine can evaluate with nothing but a stack.
- That is why the Java Virtual Machine and most bytecode interpreters work in postfix. No precedence table, no brackets, no ambiguity.
- This lesson is how a language's grammar 文法 is written down, in BNF and as a syntax diagram, and how Reverse Polish Notation 逆波兰表示法 is converted and evaluated.
Backus-Naur Form
- A grammar says which sequences of tokens are valid programs. Backus-Naur Form 巴科斯-诺尔范式 (BNF) writes it as production rules 产生式:
<symbol> ::= alternative1 | alternative2 | ...
- A terminal 终结符 symbol is literal text that appears in the program. A non-terminal 非终结符 symbol is the name of another rule, written in angle brackets.
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<letter> ::= a | b | c | … | z
<identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>
BNF における終端記号とは:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
終端記号はリテラルトークンであり、非終端記号は他の生成ルールの名前に相当します。
Recursion is how BNF repeats
- BNF has no "repeat" symbol, so repetition is written by defining a rule in terms of itself.
- Read
<identifier> ::= <letter> | <identifier> <letter> | <identifier> <digit>as: an identifier is a single letter, or an identifier followed by a letter, or an identifier followed by a digit. - Together those alternatives mean "a letter followed by any number of letters or digits", which also explains why an identifier cannot start with a digit: no alternative allows it.
- A syntax diagram 语法图, or railroad diagram, expresses the same rules graphically, with a loop where BNF uses recursion. The two notations are equivalent.

The loop and the recursion say the same thing
各文法/記法の用語とその意味を対応させなさい。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
BNF は終端記号と非終端記号からルールを構築し(再帰により反復を実現)、RPN は式を並べ替えて括弧を削除する。
なぜルール
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
選択肢全体を合わせると、文字 followed by 任意の数の文字または数字を意味し、どの選択肢も数字で始めることは許容しない。
Worked example: test a string against the grammar
- Using the rules above, which of
count2,2countandmy_varare valid identifiers? count2: valid. Build it up:cis a<letter>, so an<identifier>; addo,u,n,tby the second alternative; add2by the third.2count: invalid. Every alternative starts from a<letter>or from another<identifier>, and no chain can begin with a digit.my_var: invalid, because_is not a terminal in any rule here. State the rule that fails, not just "it looks wrong".
これらのルールを用いて、valid な識別子となる文字列はどれか?すべて選択せよ。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
単一の文字は最初の選択肢により識別子となる。2count は数字で始められず、_ はこの文のいかなるルールにも終端記号として含まれていない。
Infix and postfix
- Infix 中缀 puts the operator between its operands,
3 + 4 * 2, and therefore needs precedence rules and brackets to be unambiguous. - Reverse Polish Notation, or postfix 后缀, puts the operator after its operands:
3 4 2 * +. It needs neither. - The order in which the operators appear in the postfix form is the order they are applied, which is exactly what a machine needs to be told.
Converting infix to postfix
- Use an operator stack. Scan left to right: send an operand straight to the output; for an operator, first pop to the output any stacked operators of higher or equal precedence 优先级, then push it.
- Push an opening bracket. On a closing bracket, pop to the output until the matching opening bracket, then discard the pair.
- At the end, pop everything left on the stack to the output.
演算子の優先順位 — 逆ポーランド記法が除去するもの
通常の中置数学では、× と ÷ は + と − よりも結合度が高いため、正しい順序で規則を適用する必要があります。逆ポーランド記法では操作子を後に書き(3 4 2 × + 1 −)、順序を固定するため優先順位の規則は不要です。
中置式 (3 + 4) * 2 の RPN(後置)形式は何か?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
括弧により 3+4 が先に行われる:3 4 +、その後 2 で掛け算する:3 4 + 2 *。
(A + B) * (C - D) を逆ポーランド記法に変換せよ。掛け算には * を用いる。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
各括弧は順次変換され、掛け算は最後にポップされるため末尾に来る。括弧は残らない。
Worked example: convert, then evaluate
- Convert $(A + B) \times (C - D)$ to RPN. Push
(; outputA; push+; outputB; on)pop back to the matching(, givingA B +. Push×. The second bracket behaves identically, givingC D -. At the end pop the×. Result:A B + C D - ×. - Now evaluate it for $A=3, B=4, C=5, D=2$. Push 3, push 4;
+pops both and pushes 7. Push 5, push 2;-pops both and pushes 3.×pops 7 and 3 and pushes 21. - The operator always takes the top two items, and the first popped is the right-hand operand. That matters for
-and/, where order changes the answer.
RPN 式 3 4 2 * + を評価せよ。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
3, 4, 2 をスタックにプッシュ;* は 4 と 2 をポップして → 8;+ は 3 と 8 をポップして → 11。
逆ポーランド記法には括弧や優先順位の規則が不要であり、スタックを使って直接評価できる。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
操作数をプッシュ;各演算子は操作数をポップして結果をプッシュする — これはまさにスタックマシンが動作する方法である。
Evaluating with a stack, step by step
- Scan left to right: push each operand; on an operator, pop the top two, apply it, and push the result. At the end the stack holds one value: the answer.
| Token | Stack after |
|---|---|
3 |
3 |
4 |
3, 4 |
2 |
3, 4, 2 |
* |
3, 8 |
+ |
11 |
- This is a stack machine: no brackets, no precedence table, no lookahead. It is how the JVM and many bytecode interpreters evaluate every expression.
RPN を評価する際、スタックから最初にポップされる値は演算子の左辺操作数である。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
最初にポップされるのは右辺操作数である。+ や * では問題ないが、引き算や割り算では順序を逆にすると結果が変わる。
スタックを用いて 3 4 2 * + を評価する手順を正しい順序で並べ替えなさい。
難易度 3/5 · 200 コイン + 今日初回完了時に小ボーナス付与
操作数は順にプッシュされ、各演算者はトップの2つを消費して結果を残す。括弧や優先順位の表は不要である。
Marks that slip away
- A terminal is literal text; a non-terminal names another rule. Do not swap them.
- BNF expresses repetition by recursion. If a rule refers to itself, say so and say what it means.
- In evaluation the operator takes the top two items, and the first one popped is the right operand. Getting that backwards breaks subtraction and division.
- RPN needs no brackets. Writing brackets into a postfix answer loses the mark it was testing.
You've got it
- BNF production rules combine terminals (literal text) and non-terminals (rule names), and express repetition by recursion; a syntax diagram is the equivalent graphical form
- test a string by building it from the rules, and name the rule that fails when it is invalid
- infix needs precedence and brackets; RPN (postfix) puts the operator after its operands and needs neither
- convert with an operator stack, and evaluate by pushing operands and applying each operator to the top two, the first popped being the right-hand operand