Grammar for Backus-Naur form postfix add/subtract

  • Thread starter acadian
  • Start date
  • Tags
    Form
In summary, to create a postfix grammar from an infix grammar, one must first create a parse tree for the infix expression and then traverse the tree in reverse order to obtain the postfix version of the expression. This can be expressed in Backus-Naur form by replacing the operators with their respective postfix versions.
  • #1
acadian
4
0
I'm looking to write a grammar in Backus-Naur form for postfix addition and subtraction expressions involving the variables A,B,C

Given the infix grammar:
<expr> ::= <expr> + <term> | <expr> - <term> | <term>
<term> ::= <term> * <factor> | <term> / <factor> | <factor>
<factor> ::= (<expr>) | <id>
<id> ::= A|B|C

I assume that I need to create a parse tree, first, using the above grammar for an infix expression such as A + B + C and than traverse the parse tree in reverse order and read out the postfix version to form a postfix grammar?

Would this be a valid way to go about this?
 
Physics news on Phys.org
  • #2
Yes, this is a valid way to go about creating a postfix grammar from an infix grammar. To do this, you would need to create a parse tree for the infix expression and then traverse the tree in reverse order, reading out the postfix version of the expression. For example, given the infix expression A+B+C, one could create a parse tree as follows: + / \ A + / \ B CBy traversing this tree in reverse order, one can obtain the postfix expression A B C + +, which can then be expressed in Backus-Naur form as follows:<expr> ::= <id> <id> <id> + +
 

1. What is Backus-Naur form?

Backus-Naur form (BNF) is a notation technique used to describe the syntax of a programming language or other type of formal language. It was developed by John Backus and Peter Naur in the 1960s.

2. What is postfix notation?

Postfix notation, also known as Reverse Polish Notation (RPN), is a mathematical notation where the operator is placed after the operands. For example, instead of writing 2 + 3, in postfix notation it would be written as 2 3 +.

3. What does it mean to add or subtract in Backus-Naur form?

In Backus-Naur form, adding or subtracting is represented by the symbols '+' and '-', respectively. These symbols are placed after the operands in postfix notation, such as '3 4 +', which would be equivalent to '3 + 4' in traditional notation.

4. How is the order of operations determined in Backus-Naur form?

In postfix notation, the order of operations is determined by the position of the operator. The operator is applied to the two operands that immediately precede it. This allows for a simpler and more efficient use of parentheses and avoids ambiguity in mathematical expressions.

5. Can Backus-Naur form be used for more complex expressions?

Yes, Backus-Naur form can be used for a wide range of expressions, including more complex mathematical expressions and programming language syntax. It is a versatile notation technique that is commonly used in computer science and linguistics.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
14
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
  • Quantum Physics
Replies
5
Views
1K
  • Precalculus Mathematics Homework Help
Replies
1
Views
778
Replies
31
Views
2K
  • Set Theory, Logic, Probability, Statistics
2
Replies
40
Views
6K
  • Special and General Relativity
Replies
7
Views
2K
  • Special and General Relativity
Replies
16
Views
1K
  • Special and General Relativity
Replies
13
Views
2K
Back
Top